#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 1001005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
  freopen("filename.in", "r", stdin);
  freopen("filename.out", "w", stdout);
}
long long a, b, p, x, r[N], tt = 1, q[N], x0, ans = 0, T = 1, T_rev;
bool v[N];
long long Pow(long long x, long long k) {
  long long ans = 1;
  for (; k; k >>= 1, (x *= x) %= p)
    if (k & 1) (ans *= x) %= p;
  return ans;
}
int main() {
  scanf("%I64d%I64d%I64d%I64d", &a, &b, &p, &x);
  if (a == 1)
    if (x < b)
      return 0 * printf("0\n");
    else
      return 0 * printf("%I64d\n", (x - b) / p + 1);
  v[1] = 1;
  while (1) {
    (tt *= a) %= p;
    if (v[tt]) break;
    v[tt] = 1;
    T++;
  }
  r[1] = tt = 1;
  for (int i = (1); i <= (T - 1); ++i) q[(tt *= a) %= p] = i;
  q[1] = 0;
  T_rev = Pow(T, p - 2);
  for (int i = (2); i <= (p - 1); ++i) r[i] = (p - p / i) * r[p % i] % p;
  for (int i = (1); i <= (p - 1); ++i) {
    if (i > 1 && !q[i]) continue;
    long long t = i;
    x0 = (r[t] * b % p * T * T_rev + q[t] * p) % (p * T);
    if (x < x0) continue;
    ans += (x - x0) / (p * T) + 1;
  }
  printf("%I64d\n", ans);
  return 0;
}

### 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;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 1001005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
  freopen("filename.in", "r", stdin);
  freopen("filename.out", "w", stdout);
}
long long a, b, p, x, r[N], tt = 1, q[N], x0, ans = 0, T = 1, T_rev;
bool v[N];
long long Pow(long long x, long long k) {
  long long ans = 1;
  for (; k; k >>= 1, (x *= x) %= p)
    if (k & 1) (ans *= x) %= p;
  return ans;
}
int main() {
  scanf("%I64d%I64d%I64d%I64d", &a, &b, &p, &x);
  if (a == 1)
    if (x < b)
      return 0 * printf("0\n");
    else
      return 0 * printf("%I64d\n", (x - b) / p + 1);
  v[1] = 1;
  while (1) {
    (tt *= a) %= p;
    if (v[tt]) break;
    v[tt] = 1;
    T++;
  }
  r[1] = tt = 1;
  for (int i = (1); i <= (T - 1); ++i) q[(tt *= a) %= p] = i;
  q[1] = 0;
  T_rev = Pow(T, p - 2);
  for (int i = (2); i <= (p - 1); ++i) r[i] = (p - p / i) * r[p % i] % p;
  for (int i = (1); i <= (p - 1); ++i) {
    if (i > 1 && !q[i]) continue;
    long long t = i;
    x0 = (r[t] * b % p * T * T_rev + q[t] * p) % (p * T);
    if (x < x0) continue;
    ans += (x - x0) / (p * T) + 1;
  }
  printf("%I64d\n", ans);
  return 0;
}

```