#include <bits/stdc++.h>
using namespace std;
long long a, b, p, x, ainv;
long long power(long long base, long long expo, long long mod) {
  long long ans = 1;
  while (expo) {
    if (expo % 2) ans = (ans * base) % mod;
    expo >>= 1;
    base = (base * base) % mod;
  }
  return ans;
}
long long xgcd(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = xgcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  if (x < 0) {
    x += b;
    y -= a;
  }
  return gcd;
}
long long inv(long long base, long long expo) {
  long long x, y;
  long long gcd = xgcd(base, expo, x, y);
  assert(gcd == 1);
  return x;
}
void crt(long long b1, long long m1, long long b2, long long m2, long long &nb,
         long long &nm) {
  nb = b1 * b2;
  nm = (b1 * ((inv(b1, b2) * (b2 + (m2 - m1) % b2)) % b2) + m1) % nb;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> a >> b >> p >> x;
  ainv = power(a, p - 2, p);
  vector<long long> v(p + 1);
  v[0] = b;
  long long xp = min(x, p);
  long long ans = 0;
  for (long long i = 1; i < p; i++) {
    v[i] = (v[i - 1] * ainv) % p;
    long long nb, nm;
    crt(p, v[i], p - 1, i, nb, nm);
    if (nm == 0) nm = nb;
    long long count = 0;
    if ((x) >= nm) {
      count = 1 + ((x - nm) / nb);
    }
    ans += count;
  }
  cout << ans;
}

### Prompt

Please formulate a Cpp solution to 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;
long long a, b, p, x, ainv;
long long power(long long base, long long expo, long long mod) {
  long long ans = 1;
  while (expo) {
    if (expo % 2) ans = (ans * base) % mod;
    expo >>= 1;
    base = (base * base) % mod;
  }
  return ans;
}
long long xgcd(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = xgcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  if (x < 0) {
    x += b;
    y -= a;
  }
  return gcd;
}
long long inv(long long base, long long expo) {
  long long x, y;
  long long gcd = xgcd(base, expo, x, y);
  assert(gcd == 1);
  return x;
}
void crt(long long b1, long long m1, long long b2, long long m2, long long &nb,
         long long &nm) {
  nb = b1 * b2;
  nm = (b1 * ((inv(b1, b2) * (b2 + (m2 - m1) % b2)) % b2) + m1) % nb;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> a >> b >> p >> x;
  ainv = power(a, p - 2, p);
  vector<long long> v(p + 1);
  v[0] = b;
  long long xp = min(x, p);
  long long ans = 0;
  for (long long i = 1; i < p; i++) {
    v[i] = (v[i - 1] * ainv) % p;
    long long nb, nm;
    crt(p, v[i], p - 1, i, nb, nm);
    if (nm == 0) nm = nb;
    long long count = 0;
    if ((x) >= nm) {
      count = 1 + ((x - nm) / nb);
    }
    ans += count;
  }
  cout << ans;
}

```