#include <bits/stdc++.h>
using namespace std;
long long int p;
long long int Power(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    b /= 2;
  }
  return res;
}
long long int apinv[1000005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int a, b, x;
  cin >> a >> b >> p >> x;
  apinv[0] = Power(1, p - 2ll);
  long long int cur = 1;
  for (long long int i = 1; i < p - 1ll; i++) {
    cur = (cur * a) % p;
    apinv[i] = Power(cur, p - 2ll);
  }
  long long int ans = 0;
  for (long long int R = 0; R < p - 1ll; R++) {
    long long int z = (b * apinv[R]) % p;
    long long int Q_mx = (x - R) / (p - 1ll);
    if (x - R < 0) {
      continue;
    }
    long long int t_mx = (Q_mx - R + z) / p;
    if (Q_mx - R + z < 0) {
      continue;
    }
    ans += t_mx + 1ll;
    if (R - z < 0) {
      ans--;
    }
  }
  cout << ans << "\n";
}

### Prompt

Develop a solution in cpp to the problem described below:

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 p;
long long int Power(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    b /= 2;
  }
  return res;
}
long long int apinv[1000005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int a, b, x;
  cin >> a >> b >> p >> x;
  apinv[0] = Power(1, p - 2ll);
  long long int cur = 1;
  for (long long int i = 1; i < p - 1ll; i++) {
    cur = (cur * a) % p;
    apinv[i] = Power(cur, p - 2ll);
  }
  long long int ans = 0;
  for (long long int R = 0; R < p - 1ll; R++) {
    long long int z = (b * apinv[R]) % p;
    long long int Q_mx = (x - R) / (p - 1ll);
    if (x - R < 0) {
      continue;
    }
    long long int t_mx = (Q_mx - R + z) / p;
    if (Q_mx - R + z < 0) {
      continue;
    }
    ans += t_mx + 1ll;
    if (R - z < 0) {
      ans--;
    }
  }
  cout << ans << "\n";
}

```