#include <bits/stdc++.h>
using namespace std;
long long extgcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  long long g = extgcd(b, a % b, y, x);
  y -= (a / b) * x;
  return g;
}
long long chinese_remainder(long long cnt, long long a[], long long m[]) {
  long long mc = 1;
  for (int i = 0; i < cnt; i++) mc *= m[i];
  long long ans = 0, x, y;
  for (int i = 0; i < cnt; i++) {
    extgcd(m[i], mc / m[i], x, y);
    y = (y + m[i]) % m[i];
    ans += a[i] * mc / m[i] % mc * y % mc;
    ans %= mc;
  }
  return ans;
}
long long powmod(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans *= a, ans %= c;
    b >>= 1;
    a *= a;
    a %= c;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  long long a, b, p, d;
  cin >> a >> b >> p >> d;
  long long i, j, x, y;
  long long ans = 0;
  long long t1[2], t2[2];
  long long t = p * (p - 1);
  t2[0] = p - 1, t2[1] = p;
  for (i = 0; i < p - 1; i++) {
    extgcd(p, powmod(a, i, p), x, y);
    y = (y + p) % p;
    j = b * y % p;
    t1[0] = i, t1[1] = j;
    long long tmp = chinese_remainder(2, t1, t2);
    ans += d / t + (d % t >= tmp);
  }
  cout << ans << endl;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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 extgcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  long long g = extgcd(b, a % b, y, x);
  y -= (a / b) * x;
  return g;
}
long long chinese_remainder(long long cnt, long long a[], long long m[]) {
  long long mc = 1;
  for (int i = 0; i < cnt; i++) mc *= m[i];
  long long ans = 0, x, y;
  for (int i = 0; i < cnt; i++) {
    extgcd(m[i], mc / m[i], x, y);
    y = (y + m[i]) % m[i];
    ans += a[i] * mc / m[i] % mc * y % mc;
    ans %= mc;
  }
  return ans;
}
long long powmod(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans *= a, ans %= c;
    b >>= 1;
    a *= a;
    a %= c;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  long long a, b, p, d;
  cin >> a >> b >> p >> d;
  long long i, j, x, y;
  long long ans = 0;
  long long t1[2], t2[2];
  long long t = p * (p - 1);
  t2[0] = p - 1, t2[1] = p;
  for (i = 0; i < p - 1; i++) {
    extgcd(p, powmod(a, i, p), x, y);
    y = (y + p) % p;
    j = b * y % p;
    t1[0] = i, t1[1] = j;
    long long tmp = chinese_remainder(2, t1, t2);
    ans += d / t + (d % t >= tmp);
  }
  cout << ans << endl;
}

```