#include <bits/stdc++.h>
using namespace std;
const int MM = 2e6;
long long int mod;
long long int powmod(long long int a1, long long int b1,
                     long long int modx = mod) {
  long long int res = 1;
  a1 %= modx;
  for (; b1; b1 >>= 1) {
    if (b1 & 1) res = res * a1 % modx;
    a1 = (a1 * a1) % modx;
  }
  return res;
}
long long int inv(long long int ax, long long int modx = mod) {
  return powmod(ax, modx - 2, modx);
}
long long int X, A, B;
long long int val[MM];
template <class T>
struct ChineseRemainder {
  int n;
  T p[5], r[5];
  T b[5], x[5];
  void init() { n = 0; }
  void add(T pr, T rm) { p[n] = pr, r[n++] = rm; }
  T mulmod(T a, T b, T p) {
    a %= p, b %= p;
    T q = (T)((long double)a * b / p);
    T r = a * b - q * p;
    while (r < 0) r += p;
    while (r >= p) r -= p;
    return r;
  }
  pair<T, T> euclid(T a, T b) {
    if (!b) return make_pair(1, 0);
    pair<T, T> r = euclid(b, a % b);
    return make_pair(r.second, r.first - a / b * r.second);
  }
  T calc() {
    T M = 1;
    for (int i = 0; i < n; i++) M *= p[i];
    T N = 0;
    for (int i = 0; i < n; i++) {
      b[i] = M / p[i];
      x[i] = (euclid(b[i], p[i]).first % p[i] + p[i]) % p[i];
      N += mulmod(r[i] * b[i], x[i], M);
      if (N >= M) N -= M;
    }
    return N;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> A >> B >> mod >> X;
  val[0] = 1;
  for (int i = 1; i < mod - 1; i++) {
    val[i] = (val[i - 1] * A) % mod;
  }
  for (int i = 0; i < mod - 1; i++) {
    val[i] = (B * inv(val[i])) % mod;
  }
  long long int ans = 0;
  ChineseRemainder<long long int> crt;
  for (long long int i = 0; i < mod - 1; i++) {
    long long int wrt2 = i;
    long long int wrt1 = val[i];
    crt.init();
    crt.add(mod - 1, i);
    crt.add(mod, val[i]);
    long long int boom = crt.calc();
    if (boom <= X % ((mod * (mod - 1)))) {
      ans++;
    }
    ans += X / (mod * (mod - 1));
  }
  cout << ans;
  return 0;
}

### Prompt

Create a solution in cpp for the following problem:

Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b   (mod\;p), where a, b, p$$$ are all known constants.

Input

The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.

Output

Print a single integer: the number of possible answers n.

Examples

Input

2 3 5 8


Output

2


Input

4 6 7 13


Output

1


Input

233 233 10007 1


Output

1

Note

In the first sample, we can see that n=2 and n=8 are possible answers.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MM = 2e6;
long long int mod;
long long int powmod(long long int a1, long long int b1,
                     long long int modx = mod) {
  long long int res = 1;
  a1 %= modx;
  for (; b1; b1 >>= 1) {
    if (b1 & 1) res = res * a1 % modx;
    a1 = (a1 * a1) % modx;
  }
  return res;
}
long long int inv(long long int ax, long long int modx = mod) {
  return powmod(ax, modx - 2, modx);
}
long long int X, A, B;
long long int val[MM];
template <class T>
struct ChineseRemainder {
  int n;
  T p[5], r[5];
  T b[5], x[5];
  void init() { n = 0; }
  void add(T pr, T rm) { p[n] = pr, r[n++] = rm; }
  T mulmod(T a, T b, T p) {
    a %= p, b %= p;
    T q = (T)((long double)a * b / p);
    T r = a * b - q * p;
    while (r < 0) r += p;
    while (r >= p) r -= p;
    return r;
  }
  pair<T, T> euclid(T a, T b) {
    if (!b) return make_pair(1, 0);
    pair<T, T> r = euclid(b, a % b);
    return make_pair(r.second, r.first - a / b * r.second);
  }
  T calc() {
    T M = 1;
    for (int i = 0; i < n; i++) M *= p[i];
    T N = 0;
    for (int i = 0; i < n; i++) {
      b[i] = M / p[i];
      x[i] = (euclid(b[i], p[i]).first % p[i] + p[i]) % p[i];
      N += mulmod(r[i] * b[i], x[i], M);
      if (N >= M) N -= M;
    }
    return N;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> A >> B >> mod >> X;
  val[0] = 1;
  for (int i = 1; i < mod - 1; i++) {
    val[i] = (val[i - 1] * A) % mod;
  }
  for (int i = 0; i < mod - 1; i++) {
    val[i] = (B * inv(val[i])) % mod;
  }
  long long int ans = 0;
  ChineseRemainder<long long int> crt;
  for (long long int i = 0; i < mod - 1; i++) {
    long long int wrt2 = i;
    long long int wrt1 = val[i];
    crt.init();
    crt.add(mod - 1, i);
    crt.add(mod, val[i]);
    long long int boom = crt.calc();
    if (boom <= X % ((mod * (mod - 1)))) {
      ans++;
    }
    ans += X / (mod * (mod - 1));
  }
  cout << ans;
  return 0;
}

```