#include <bits/stdc++.h>
long long exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b)
    return x = 1, y = 0, a;
  else {
    long long d = exgcd(b, a % b, x, y);
    long long t = x;
    x = y;
    y = t - a / b * y;
    return d;
  }
}
long long china(int n, int *a, int *m) {
  long long M = 1, d, x = 0, y;
  for (int i = 0; i < n; i++) M *= m[i];
  for (int i = 0; i < n; i++) {
    long long w = m[i ^ 1];
    d = exgcd(m[i], w, d, y);
    x = (x + y * w * a[i]) % M;
  }
  while (x < 0) x += M;
  return x;
}
inline long long powermod(long long x, long long y, const long long &MOD) {
  long long res = 1;
  for (; y; y >>= 1, x = x * x % MOD)
    if (y & 1) res = res * x % MOD;
  return res;
}
int bsgs(int a, int b, int p) {
  if (b == 1) return 0;
  std::unordered_map<int, int> hash;
  int m = int(sqrt(1.0 * p) + 1);
  long long base = b;
  for (int i = 0; i != m; ++i) {
    hash[base] = i;
    base = base * a % p;
  }
  base = powermod(a, m, p);
  long long now = 1;
  for (int i = 1; i <= m + 1; ++i) {
    now = now * base % p;
    if (hash.count(now)) return i * m - hash[now];
  }
  return -1;
}
int a, b, p;
long long n;
void input() { std::cin >> a >> b >> p >> n; }
void solve() {
  int M[] = {p, p - 1};
  long long ans = 0;
  long long step = 1ll * p * (p - 1);
  for (int t = 0; t < p - 1; t++) {
    int r = 1ll * b * powermod(powermod(a, t, p), p - 2, p) % p;
    static int A[2];
    A[0] = r;
    A[1] = t;
    long long mn = china(2, A, M);
    if (mn <= n) {
      ans += (n - mn) / step + 1;
    }
  }
  std::cout << ans << "\n";
}
int main() {
  input();
  solve();
  return 0;
}

### 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>
long long exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b)
    return x = 1, y = 0, a;
  else {
    long long d = exgcd(b, a % b, x, y);
    long long t = x;
    x = y;
    y = t - a / b * y;
    return d;
  }
}
long long china(int n, int *a, int *m) {
  long long M = 1, d, x = 0, y;
  for (int i = 0; i < n; i++) M *= m[i];
  for (int i = 0; i < n; i++) {
    long long w = m[i ^ 1];
    d = exgcd(m[i], w, d, y);
    x = (x + y * w * a[i]) % M;
  }
  while (x < 0) x += M;
  return x;
}
inline long long powermod(long long x, long long y, const long long &MOD) {
  long long res = 1;
  for (; y; y >>= 1, x = x * x % MOD)
    if (y & 1) res = res * x % MOD;
  return res;
}
int bsgs(int a, int b, int p) {
  if (b == 1) return 0;
  std::unordered_map<int, int> hash;
  int m = int(sqrt(1.0 * p) + 1);
  long long base = b;
  for (int i = 0; i != m; ++i) {
    hash[base] = i;
    base = base * a % p;
  }
  base = powermod(a, m, p);
  long long now = 1;
  for (int i = 1; i <= m + 1; ++i) {
    now = now * base % p;
    if (hash.count(now)) return i * m - hash[now];
  }
  return -1;
}
int a, b, p;
long long n;
void input() { std::cin >> a >> b >> p >> n; }
void solve() {
  int M[] = {p, p - 1};
  long long ans = 0;
  long long step = 1ll * p * (p - 1);
  for (int t = 0; t < p - 1; t++) {
    int r = 1ll * b * powermod(powermod(a, t, p), p - 2, p) % p;
    static int A[2];
    A[0] = r;
    A[1] = t;
    long long mn = china(2, A, M);
    if (mn <= n) {
      ans += (n - mn) / step + 1;
    }
  }
  std::cout << ans << "\n";
}
int main() {
  input();
  solve();
  return 0;
}

```