#P1662. Circle Counting Reversal

    ID: 14948 Type: Default 1000ms 256MiB

Circle Counting Reversal

Circle Counting Reversal

There are 1337 people standing in a circle. They are numbered from 1 to 1337 in clockwise order. Starting from person 1, a number is announced sequentially as 1, 2, 3, ... with the initial direction proceeding clockwise (i.e. increasing order). However, whenever the current number is either a multiple of 7 or contains the digit 7, the direction of counting reverses immediately.

For example, the first 20 numbers and the corresponding person who announces the number are shown in the table below:

Number X Person Number
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 6
9 5
10 4
11 3
12 2
13 1
14 1337
15 1
16 2
17 3
18 2
19 1
20 1337

Your task is: given a number X, determine which person (their number) will announce the number X.

Note: When moving in the circle, if the person number goes beyond 1337 it wraps around to 1, and if it falls below 1 it wraps around to 1337.

The reversal condition can be stated in LaTeX as follows:

\(\text{Reverse direction if } (X \bmod 7 = 0) \text{ or } (\text{'7' is a digit in } X)\)

inputFormat

The input consists of a single integer X (1 \leq X \leq 10^9), which indicates the count number for which you need to find the person that announces it.

outputFormat

Output a single integer representing the person number who announces the number X.

sample

1
1