1. Abridged Problem Statement
You generate a sequence {x₀, x₁, …} by
• x₀ = 1
• xᵢ = (A·xᵢ₋₁ + (xᵢ₋₁ mod B)) mod C
Find the smallest index m such that x_m appears again later, and output the index of its second occurrence. If that second occurrence exceeds 2·10⁶, print –1.

2. Detailed Editorial
We seek the first "repeatable" element in an infinite sequence defined by x₀=1 and x→f(x) where
  f(x) = (A·x + (x mod B)) mod C.
By definition, the first repeatable element M is the earliest xₘ for which there exists q>m with x_q = xₘ; we must output that q if q≤2·10⁶, otherwise –1.

Observation: Any sequence defined by iterating a deterministic function on a finite set must eventually enter a cycle:
  1) A transient of length μ (possibly zero) before the cycle starts.
  2) A cycle of length λ that repeats indefinitely.
The first repeated value is x_μ (the first element of the cycle), and its next occurrence is at index μ+λ. Thus the answer is μ+λ, unless μ+λ>2·10⁶.

To compute μ and λ in O(μ+λ) time and O(1) memory, use Floyd's "tortoise and hare" algorithm:
  Phase 1: Find a meeting point inside the cycle.
   – Initialize tortoise = f(1), hare = f(f(1)).
   – Move tortoise one step, hare two steps, until they meet or until the step count exceeds a safe bound (>2·10⁶).
  Phase 2: Find μ.
   – Reset tortoise = x₀ = 1, keep hare at meeting point.
   – Move both one step at a time; the index μ where they meet is the start of the cycle.
  Phase 3: Find λ.
   – From x_μ, move hare = f(tortoise) and count steps until it returns to tortoise; that count is λ.
  Compute ans = μ + λ; if ans>2·10⁶, print –1, else print ans.

Complexity: Each function application f(x) is O(1). Phase 1 and 2 each take at most μ+λ steps, Phase 3 takes λ. Total O(μ+λ). As μ+λ≤C (and we abort above 2·10⁶), it's efficient in 1 s.

3. C++ Solution
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int64_t A, B, C;

int64_t f(int64_t x) {
    int64_t res = A;
    res *= x;
    res += x % B;
    res %= C;
    return res;
}

void read() { cin >> A >> B >> C; }

int hare_tortoise() {
    int t = 0;
    int64_t hare = f(f(1));
    int64_t tortoise = f(1);
    do {
        tortoise = f(tortoise);
        hare = f(f(hare));
        t++;
        if(t > 2000042) {
            return -1;
        }
    } while(tortoise != hare);

    int mu = 0;
    tortoise = 1;
    while(tortoise != hare) {
        tortoise = f(tortoise);
        hare = f(hare);
        mu++;
    }

    int lambda = 1;
    hare = f(tortoise);
    while(tortoise != hare) {
        hare = f(hare);
        lambda++;
    }

    return mu + lambda;
}

void solve() {
    // The sequence x_{i+1} = (A * x_i + x_i % B) % C is a functional graph
    // iteration f, so it eventually enters a cycle (rho shape). The first
    // repeatable element is the first one that lies on the cycle, i.e. the
    // entry point of the tail of length mu into the cycle of length lambda. Its
    // second occurrence is exactly mu + lambda steps from the start.
    //
    // We run Floyd's tortoise and hare: advance until they meet inside the
    // cycle, then find mu by walking both one step at a time from x_0 and the
    // meeting point until they coincide, and find lambda by walking one pointer
    // around the cycle. If no meeting happens within the search bound the index
    // exceeds 2 * 10^6, so we report -1.

    int ans = hare_tortoise();
    if(ans > 2000000) {
        ans = -1;
    }
    cout << ans << '\n';
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

4. Python Solution
```python
import sys
sys.setrecursionlimit(10**7)

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

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

    # Phase 1: detect a meeting in the cycle
    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 meet within our limit, no repeat <=2e6
    if steps > limit:
        print(-1)
        return

    # Phase 2: find mu (start index of cycle)
    mu = 0
    tortoise = 1             # reset tortoise to x0
    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

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

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

5. Compressed Editorial
- The sequence x₀=1, xᵢ=f(xᵢ₋₁) must eventually cycle.
- The first repeated value is at the cycle start index μ, and its next occurrence is at μ+λ, where λ is the cycle length.
- Use Floyd's tortoise-and-hare:
  Phase 1: find a meeting point inside the cycle.
  Phase 2: find μ by resetting one pointer to start and advancing both by one until they meet.
  Phase 3: find λ by counting steps to return to the cycle start.
- Answer = μ+λ if ≤2·10⁶, else –1.
