C. Pisano period
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Many recurrent sequences acquire new interesting properties if they are calculated modulo of some number. Due to the finiteness of the division residues, sequences turn into cycles or loops. The Fibonacci numbers are a good example of a looping sequence. The Fibonacci sequence is defined by the recurrence relation $$$ F_i = F_{i-1} + F_{i-2} $$$, where $$$ F_0 = 0, F_1 = 1 $$$.

When calculating such a sequence taken modulo $$$ n $$$, after a certain number of steps, there is a return to the initial values of $$$ 0 $$$ and $$$ 1 $$$. The minimum number of steps required to loop the sequence is denoted as $$$ \pi(n) $$$ and is called the Pisano period. The number $$$ \pi(n) $$$ exists for any $$$ n $$$. For each $$$ \pi(n) $$$ is valid $$$ F_{i} \equiv F_{i+\pi(n)} (mod~n) $$$ with any $$$ i $$$.

The relationship between the length of the Pisano period $$$ \pi(n) $$$ and the number $$$ n $$$ itself is quite complex and comes down to calculating Pisano periods for prime numbers. For a prime number $$$ p $$$, the value of $$$ \pi(p) $$$ is calculated according to the following rules:

  1. Two special cases are distinguished: $$$ \pi(2) = 3 $$$ – the only period with an odd length, and $$$ \pi(5) = 20 $$$.
  2. If $$$ p \equiv \pm 1 (mod~5) $$$, then $$$ \pi(p) $$$ will be the divisor of the number $$$ (p - 1)$$$.
  3. If $$$ p \equiv \pm 2 (mod~5) $$$, then $$$ \pi(p) $$$ will be a divisor of $$$ (2p + 2)$$$.
It should be noted that for all primes, except 2, $$$ \pi(p) $$$ is always even. For example, $$$ 47~mod~5 = 2 $$$, and $$$ \pi(47) = 32 $$$, and the number $$$ 32 $$$ is a divisor of the number $$$ 2 \cdot 47 + 2 = 96 $$$. Or $$$ 89~mod~5 = 4 (-1) $$$, and $$$ \pi(89) = 44 $$$, because $$$ 44 $$$ is the divisor of $$$ 89 - 1= 88 $$$. Write a program that, given a prime number $$$ p $$$ ($$$ 2 \leq p \leq 10^9 $$$), calculates $$$ \pi(p) $$$ for it.
Input

A single line contains a prime number $$$ p $$$ ($$$ 2 \leq p \leq 10^9 $$$).

Output

In a single line print a number – the length of the Pisano period $$$ \pi(p) $$$.

Examples
Input
11
Output
10
Input
5
Output
20