#include <bits/stdc++.h>
long long n, a, b, mod, ai[10005], num;
bool vis[1000005];
inline long long poww(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res;
}
namespace CRT {
void gcd(long long a, long long b, long long &d, long long &x, long long &y) {
  if (b == 0) {
    d = a;
    x = 1, y = 0;
  } else {
    gcd(b, a % b, d, y, x);
    y -= (a / b) * x;
  }
}
long long China(int n, long long *m, long long *a) {
  long long M = 1, d, y, x = 0;
  for (int i = 0; i < n; i++) M *= m[i];
  for (int i = 0; i < n; i++) {
    long long w = M / m[i];
    gcd(m[i], w, d, d, y);
    x = (x + y * w * a[i]) % M;
  }
  return (x + M) % M;
}
}  // namespace CRT
long long m2[15], a2[15];
int main() {
  std::cin >> a >> b >> mod >> n;
  long long tmp, ans = 0;
  m2[0] = mod - 1;
  m2[1] = mod;
  long long ba = 1;
  for (int i = 0; i < mod - 1; i++, (ba *= a) %= mod) {
    tmp = poww(ba, mod - 2) * b % mod;
    a2[0] = i;
    a2[1] = tmp;
    long long to = CRT::China(2, m2, a2);
    if (to <= n) ans += 1;
    ans += (n - to) / (m2[0] * m2[1]);
  }
  std::cout << ans;
  return 0;
}

### Prompt

Please create a solution in cpp 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>
long long n, a, b, mod, ai[10005], num;
bool vis[1000005];
inline long long poww(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res;
}
namespace CRT {
void gcd(long long a, long long b, long long &d, long long &x, long long &y) {
  if (b == 0) {
    d = a;
    x = 1, y = 0;
  } else {
    gcd(b, a % b, d, y, x);
    y -= (a / b) * x;
  }
}
long long China(int n, long long *m, long long *a) {
  long long M = 1, d, y, x = 0;
  for (int i = 0; i < n; i++) M *= m[i];
  for (int i = 0; i < n; i++) {
    long long w = M / m[i];
    gcd(m[i], w, d, d, y);
    x = (x + y * w * a[i]) % M;
  }
  return (x + M) % M;
}
}  // namespace CRT
long long m2[15], a2[15];
int main() {
  std::cin >> a >> b >> mod >> n;
  long long tmp, ans = 0;
  m2[0] = mod - 1;
  m2[1] = mod;
  long long ba = 1;
  for (int i = 0; i < mod - 1; i++, (ba *= a) %= mod) {
    tmp = poww(ba, mod - 2) * b % mod;
    a2[0] = i;
    a2[1] = tmp;
    long long to = CRT::China(2, m2, a2);
    if (to <= n) ans += 1;
    ans += (n - to) / (m2[0] * m2[1]);
  }
  std::cout << ans;
  return 0;
}

```