
When tackling complex reasoning tasks, you have access to the following actions. Use them as needed to progress through your thought process.

[ASSESS]

[ADVANCE]

[VERIFY]

[SIMPLIFY]

[SYNTHESIZE]

[PIVOT]

[OUTPUT]

You should strictly follow the format below:

[ACTION NAME]

# Your action step 1

# Your action step 2

# Your action step 3

...

Next action: [NEXT ACTION NAME]


system
Consider the sequence $\left\{a_{n}\right\}$ defined by $a_{1}=1$ and for $n=1, 2, \ldots$, given by: 
\[ a_{n+1}=\begin{cases} 
a_{n}+n, & \text{if } a_{n} \leq n, \\ 
a_{n}-n, & \text{if } a_{n} > n.
\end{cases} \]
Determine the number of positive integers \( r \) such that \( a_{r} < r \leq 3^{2017} \).

Present the answer in LaTex format: \boxed{Your answer}
user