#include <bits/stdc++.h>
using namespace std;
long long int pows[1000100];
long long int phi[1000010];
void init() {
  for (int i = 0; i < 1000010; ++i) {
    phi[i] = i;
  }
  for (int i = 2; i < 1000010; ++i) {
    if (phi[i] == i) {
      for (int j = i; j < 1000010; j += i) {
        phi[j] -= phi[j] / i;
      }
    }
  }
}
long long int modp(long long int a, long long int n, long long int m) {
  long long int r = 1;
  while (n) {
    if (n & 1) r = (r * a) % m;
    a = (a * a) % m;
    n >>= 1;
  }
  return r;
}
map<long long int, int> mark;
int main(void) {
  init();
  long long int a, b, p, x;
  cin >> a >> b >> p >> x;
  pows[0] = 1;
  for (int i = 1; i < p; ++i) {
    pows[i] = (pows[i - 1] * a);
    if (pows[i] >= p) pows[i] %= p;
  }
  long long int ans = 0;
  long long int N = p * (p - 1);
  long long int n1 = p, n2 = p - 1;
  long long int y1 = p - 1, y2 = p;
  long long int z1 = modp(y1, phi[n1] - 1, n1), z2 = modp(y2, phi[n2] - 1, n2);
  for (int i = 0; i < p; ++i) {
    long long int a1 = (b * modp(pows[i], p - 2, p)) % p;
    long long int a2 = i;
    long long int an = (a1 * y1 * z1 + a2 * y2 * z2) % N;
    if (mark[an]) continue;
    mark[an] = 1;
    if (an > x) continue;
    ans += x / N;
    if (an <= x % N) ++ans;
  }
  cout << ans << '\n';
}

### 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;
long long int pows[1000100];
long long int phi[1000010];
void init() {
  for (int i = 0; i < 1000010; ++i) {
    phi[i] = i;
  }
  for (int i = 2; i < 1000010; ++i) {
    if (phi[i] == i) {
      for (int j = i; j < 1000010; j += i) {
        phi[j] -= phi[j] / i;
      }
    }
  }
}
long long int modp(long long int a, long long int n, long long int m) {
  long long int r = 1;
  while (n) {
    if (n & 1) r = (r * a) % m;
    a = (a * a) % m;
    n >>= 1;
  }
  return r;
}
map<long long int, int> mark;
int main(void) {
  init();
  long long int a, b, p, x;
  cin >> a >> b >> p >> x;
  pows[0] = 1;
  for (int i = 1; i < p; ++i) {
    pows[i] = (pows[i - 1] * a);
    if (pows[i] >= p) pows[i] %= p;
  }
  long long int ans = 0;
  long long int N = p * (p - 1);
  long long int n1 = p, n2 = p - 1;
  long long int y1 = p - 1, y2 = p;
  long long int z1 = modp(y1, phi[n1] - 1, n1), z2 = modp(y2, phi[n2] - 1, n2);
  for (int i = 0; i < p; ++i) {
    long long int a1 = (b * modp(pows[i], p - 2, p)) % p;
    long long int a2 = i;
    long long int an = (a1 * y1 * z1 + a2 * y2 * z2) % N;
    if (mark[an]) continue;
    mark[an] = 1;
    if (an > x) continue;
    ans += x / N;
    if (an <= x % N) ++ans;
  }
  cout << ans << '\n';
}

```