#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   int f[MAXN + 5];   int main() {     memset(f, 0, sizeof(f));     int m = 0, a = 0, r0 = 0, b = 0, ans = 1;     cin >> a >> b >> m >> r0;     f[r0] = 1;     for (int i = 2;; ++i) {       r0 = (a * r0 + b) % m;       ++ans;       if (f[r0] != 0) {         ans = ans - f[r0];         break;       }       f[r0] = ans;     }     cout << ans << endl;   }   