#P2069. Squirrel Jumping Fruits

    ID: 15351 Type: Default 1000ms 256MiB

Squirrel Jumping Fruits

Squirrel Jumping Fruits

A squirrel loves to eat fruits arranged in a vertical column. There are \(n\) fruits labeled from \(1\) to \(n\) starting from the bottom to the top. The squirrel starts at the bottom fruit. On the \(i\)th jump, it will jump over \( (i^3 \bmod 5) + 1 \) fruits in one go and lands on the next fruit, which it then eats. Immediately after eating, if there are any fruits above, they all fall down one position.

For example, during the first jump, the jump length is computed as \( (1^3 \bmod 5) + 1 = 2 \). Starting at the first fruit, the squirrel jumps over one fruit and lands on the third fruit, eating it. After this removal, all fruits above the eaten one drop down one position. In the second jump, with \( (2^3 \bmod 5) + 1 = 4 \), the squirrel jumps from its current position (which becomes the fruit that originally was the fourth fruit) and lands on the fruit that originally was labeled 8, eating it.

Sometimes the jump will carry the squirrel past the top fruit (i.e. there is no fruit to land on). In that case, the squirrel returns to the bottom fruit and repeats the same jump until it lands on a fruit and eats it.

Your task is: Given \(n\) (the initial number of fruits) and \(m\) (the number of fruits eaten), determine the label of the \(m\)th fruit that the squirrel eats.

Note: All formulas must be represented in \(\LaTeX\) format.

inputFormat

The input consists of two integers \(n\) and \(m\) on a single line, separated by a space:

  • \(n\): the total number of fruits (\(n \ge 1\)).
  • \(m\): the number of fruits eaten (\(1 \le m \le n\)).

outputFormat

Output a single integer representing the label of the \(m\)th fruit the squirrel eats.

sample

10 1
3