#include <bits/stdc++.h>
using namespace std;
int mi[1000100], fac[1000100], facinv[1000100], inv[1000100], k[1000100];
int fast_pow(int x, int k, int p) {
  if (k == 0) return 1;
  if (k == 1) return x;
  int ans = fast_pow(x, k >> 1, p);
  ans = (long long)ans * ans % p;
  if (k & 1) ans = (long long)ans * x % p;
  return ans;
}
int main() {
  int a, b, p;
  long long x, ans = 0;
  cin >> a >> b >> p >> x;
  for (int i = mi[0] = 1; i <= p; i++) mi[i] = (long long)mi[i - 1] * a % p;
  for (int i = fac[0] = 1; i < p; i++) fac[i] = (long long)i * fac[i - 1] % p;
  facinv[p - 1] = fast_pow(fac[p - 1], p - 2, p);
  for (int i = p - 2; i >= 0; i--)
    facinv[i] = (long long)facinv[i + 1] * (i + 1) % p;
  for (int i = 1; i < p; i++) inv[i] = (long long)fac[i - 1] * facinv[i] % p;
  for (int i = 0, t = 1; i < p - 1; i++, t = (long long)t * a % p)
    k[i] = (long long)b * inv[t] % p;
  for (int r = 0, t; r < p - 1; r++) {
    t = (long long)inv[p - 1] * (k[r] - r + p) % p;
    long long c;
    c = (x - r) / (p - 1) - t;
    if (x >= r && c >= 0) ans += c / p + 1;
  }
  return !printf("%lld\n", ans);
}

### Prompt

Your challenge is to write 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;
int mi[1000100], fac[1000100], facinv[1000100], inv[1000100], k[1000100];
int fast_pow(int x, int k, int p) {
  if (k == 0) return 1;
  if (k == 1) return x;
  int ans = fast_pow(x, k >> 1, p);
  ans = (long long)ans * ans % p;
  if (k & 1) ans = (long long)ans * x % p;
  return ans;
}
int main() {
  int a, b, p;
  long long x, ans = 0;
  cin >> a >> b >> p >> x;
  for (int i = mi[0] = 1; i <= p; i++) mi[i] = (long long)mi[i - 1] * a % p;
  for (int i = fac[0] = 1; i < p; i++) fac[i] = (long long)i * fac[i - 1] % p;
  facinv[p - 1] = fast_pow(fac[p - 1], p - 2, p);
  for (int i = p - 2; i >= 0; i--)
    facinv[i] = (long long)facinv[i + 1] * (i + 1) % p;
  for (int i = 1; i < p; i++) inv[i] = (long long)fac[i - 1] * facinv[i] % p;
  for (int i = 0, t = 1; i < p - 1; i++, t = (long long)t * a % p)
    k[i] = (long long)b * inv[t] % p;
  for (int r = 0, t; r < p - 1; r++) {
    t = (long long)inv[p - 1] * (k[r] - r + p) % p;
    long long c;
    c = (x - r) / (p - 1) - t;
    if (x >= r && c >= 0) ans += c / p + 1;
  }
  return !printf("%lld\n", ans);
}

```