<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

455. Sequence analysis
Time limit per test: 1 second(s)
Memory limit: 4096 kilobytes
input: standard
output: standard

Due to the slow 'mod' and 'div' operations with int64 type, all Delphi solutions for the problem 455 (Sequence analysis) run much slower than the same code written in C++ or Java. We do not guarantee that Delphi solution exists.


You are given a sequence of signed 64-bit integers defined as follows:
x0 = 1,
,
where mod is a remainder operator. All arithmetic operations are evaluated without overflow checking. Use standard "remainder" operator for programming languages (it differs from the mathematical version; for example  in programming, while  in mathematics). Use "long long" type in C++, "long" in Java and "int64" in Delphi to store xi and all other values.

Let's call a sequence element xp repeatable if it occurs later in the sequence — meaning that there exists such q, q > p, that xq = xp. The first repeatable element M of the sequence is such an element xm that xm is repeatable, and none of the xp where p < m are repeatable.

Given A, B and C, your task is to find the index of the second occurence of the first repeatable element M in the sequence if the index is less or equal to 2 · 106. Per definition, the first element of the sequence has index 0.

Input
The only line of input contains three signed 64-bit integers: A, B and C (B > 0, C > 0).

Output
Print a single integer  — the index of the second occurence of the first repeatable member if it is less or equal to 2 · 106. Print -1 if the index is more than 2 · 106.

Example(s)
sample input
sample output
2 2 9
4

sample input
sample output
2305843009213693951 1 9223372036854775807
5

sample input
sample output
-2 1 5
4



Note
In the first sample test the sequence starts with the following numbers: 1, 3, 7, 6, 3, 7. The first repeatable element is 3. The second occurence of 3 has index 4.

In the second sample test the sequence starts with the following numbers: 1, 2305843009213693951, -4611686018427387903, 6917529027641081855, 0, 0, 0. The first repeatable element is 0. The second occurence of 0 has index 5.

In the third sample test the sequence starts with the following numbers: 1, -2, 4, -3, 1, -2, 4. The first repeatable element is 1. The second occurence of 1 has index 4.

<|response|>
1. Abridged Problem Statement  
You generate an infinite sequence {x₀, x₁, x₂, …} of signed 64-bit integers by:  
  • x₀ = 1  
  • xᵢ = (A·xᵢ₋₁ + (xᵢ₋₁ mod B)) mod C,   for i ≥ 1  
Here B>0 and C>0. An element xₚ is called “repeatable” if it appears again later in the sequence (i.e., x_q = xₚ for some q>p). The first repeatable element M is the one at the smallest index m with that property. We must output the index of its second occurrence q (the smallest q>m with x_q = x_m), provided q ≤ 2·10⁶; otherwise print –1.  

2. Key Observations  
- Any sequence defined by iterating a deterministic function f on a finite set must eventually enter a cycle.  
  - There is a “transient” (non-repeating prefix) of length μ ≥ 0, then a cycle of length λ ≥ 1 that repeats forever.  
- The first repeated value is xₘ where m = μ (the first point where the cycle begins). Its next occurrence is at index μ+λ.  
- Therefore the answer is μ+λ if μ+λ ≤ 2·10⁶, otherwise –1.  
- Floyd’s “tortoise and hare” algorithm finds μ and λ in O(μ+λ) time and O(1) extra space:  
  1. Phase 1: move tortoise by f(x) and hare by f(f(x)) until they meet—guaranteed inside the cycle.  
  2. Phase 2: reset tortoise to the start x₀; move both one step at a time until they meet again; that meeting index is μ.  
  3. Phase 3: from that meeting point, move hare one step at a time until it returns; the count of steps is λ.  

3. Full Solution Approach  
1. Read A, B, C as signed 64-bit integers.  
2. Define a function f(x):  
     res = A*x  
     res += x % B          (use the language’s remainder operator)  
     res %= C              (ensure result is in [0, C-1] if C>0)  
     return res  
3. Apply Floyd’s algorithm:  
   Phase 1 (find meeting point):  
     tortoise = f(1)  
     hare     = f(f(1))  
     repeat up to a safe step bound (>2·10⁶) until tortoise == hare.  
     If we exceed the bound, conclude the cycle’s second occurrence index >2·10⁶ and print –1.  
   Phase 2 (find μ):  
     reset tortoise = 1, keep hare where it met.  
     initialize mu = 0.  
     while tortoise != hare:  
       tortoise = f(tortoise)  
       hare     = f(hare)  
       mu++  
   Phase 3 (find λ):  
     initialize lambda = 1  
     hare = f(tortoise)  
     while tortoise != hare:  
       hare++ = f(hare)  
       lambda++  
   The second occurrence index = mu + lambda.  
4. If (mu + lambda) ≤ 2·10⁶, print that; otherwise print –1.  

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

// Global parameters
static long long A, B, C;

// Transition function f(x) = (A*x + (x mod B)) mod C
long long f(long long x) {
    // Compute A*x in 64-bit
    long long res = A * x;
    // Add the remainder of x divided by B (can be negative in C++ if x<0)
    res += x % B;
    // Reduce modulo C (C>0), result will be in [0, C-1] or negative, but still correct
    res %= C;
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Read input
    cin >> A >> B >> C;

    // Phase 1: Find a meeting inside the cycle
    long long tortoise = f(1);
    long long hare     = f(f(1));
    // Step counter to avoid infinite loops beyond our interest
    int steps = 0;
    const int LIMIT = 2000000 + 42; // a small buffer above 2e6
    while (tortoise != hare && steps <= LIMIT) {
        tortoise = f(tortoise);      // move 1 step
        hare     = f(f(hare));       // move 2 steps
        steps++;
    }
    // If we never met within the limit, second occurrence is >2e6
    if (steps > LIMIT) {
        cout << -1 << "\n";
        return 0;
    }

    // Phase 2: Find mu, the start index of the cycle
    int mu = 0;
    tortoise = 1;
    while (tortoise != hare) {
        tortoise = f(tortoise);
        hare     = f(hare);
        mu++;
    }

    // Phase 3: Find lambda, the cycle length
    int lambda = 1;
    hare = f(tortoise);
    while (tortoise != hare) {
        hare   = f(hare);
        lambda++;
    }

    // The second occurrence of x_mu is at index mu + lambda
    int answer = mu + lambda;
    if (answer > 2000000) answer = -1;
    cout << answer << "\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.readline().strip().split()
    A, B, C = map(int, data)

    # Define the transition function f(x)
    def f(x):
        # Compute A*x + (x mod B), then mod C
        return (A * x + (x % B)) % C

    # Phase 1: Detect cycle with tortoise and hare
    tortoise = f(1)        # moves 1 step
    hare     = f(f(1))     # moves 2 steps
    steps    = 0
    LIMIT    = 2_000_000 + 42
    while tortoise != hare and steps <= LIMIT:
        tortoise = f(tortoise)
        hare     = f(f(hare))
        steps += 1

    # If no meeting within limit, second occurrence index >2e6
    if steps > LIMIT:
        print(-1)
        return

    # Phase 2: Find mu (start index of the cycle)
    mu = 0
    tortoise = 1
    while tortoise != hare:
        tortoise = f(tortoise)
        hare     = f(hare)
        mu += 1

    # Phase 3: Find lambda (cycle length)
    lam = 1
    hare = f(tortoise)
    while tortoise != hare:
        hare = f(hare)
        lam += 1

    answer = mu + lam
    # If the second occurrence is beyond 2e6, print -1
    print(answer if answer <= 2_000_000 else -1)

if __name__ == "__main__":
    main()
```