#include <bits/stdc++.h>
using namespace std;
const long long MAXP = 1234567;
long long p;
long long pows[MAXP];
long long pos[MAXP];
long long bpow(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  long long ans = bpow(a, b / 2);
  return ans * ans % p * (b % 2 == 1 ? a : 1) % p;
}
int32_t main() {
  long long a, b, x;
  cin >> a >> b >> p >> x;
  memset(pos, -1, sizeof pos);
  long long len = -1;
  pows[0] = 1;
  pos[1] = 0;
  for (long long i = 1; i < p; ++i) {
    pows[i] = pows[i - 1] * a % p;
    if (len == -1 && pows[i] == 1) {
      len = i;
    }
    if (len == -1) {
      pos[pows[i]] = i;
    }
  }
  long long ans = 0;
  for (long long i = 1; i < p; ++i) {
    long long c = b * bpow(pows[i], p - 2) % p * bpow(i, p - 2) % p;
    if (pos[c] == -1) {
      continue;
    }
    if (x >= i && (x - i) / p - pos[c] >= 0) {
      ans += ((x - i) / p - pos[c]) / len + 1;
    }
  }
  cout << ans << '\n';
  return 0;
}

### Prompt

Your task is to create 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;
const long long MAXP = 1234567;
long long p;
long long pows[MAXP];
long long pos[MAXP];
long long bpow(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  long long ans = bpow(a, b / 2);
  return ans * ans % p * (b % 2 == 1 ? a : 1) % p;
}
int32_t main() {
  long long a, b, x;
  cin >> a >> b >> p >> x;
  memset(pos, -1, sizeof pos);
  long long len = -1;
  pows[0] = 1;
  pos[1] = 0;
  for (long long i = 1; i < p; ++i) {
    pows[i] = pows[i - 1] * a % p;
    if (len == -1 && pows[i] == 1) {
      len = i;
    }
    if (len == -1) {
      pos[pows[i]] = i;
    }
  }
  long long ans = 0;
  for (long long i = 1; i < p; ++i) {
    long long c = b * bpow(pows[i], p - 2) % p * bpow(i, p - 2) % p;
    if (pos[c] == -1) {
      continue;
    }
    if (x >= i && (x - i) / p - pos[c] >= 0) {
      ans += ((x - i) / p - pos[c]) / len + 1;
    }
  }
  cout << ans << '\n';
  return 0;
}

```