#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 100;
long long dp[N];
long long bm(long long a, long long b, long long md) {
  if (b == 0) return 1 % md;
  long long tmp = bm(a, b / 2, md);
  tmp = tmp * tmp;
  tmp %= md;
  if (b & 1) {
    return (tmp * a) % md;
  }
  return tmp;
}
int main() {
  long long a, b, p, X;
  cin >> a >> b >> p >> X;
  long long mul = 1;
  long long cyc = 0;
  for (long long k = 1; k <= p; k++) {
    mul = mul * a;
    mul %= p;
    if (mul == 1) {
      cyc = k;
      break;
    }
  }
  mul = 1;
  memset(dp, -1, sizeof dp);
  for (long long k = 0; k < cyc; k++) {
    if (k == 0)
      mul = 1;
    else
      mul = mul * a;
    mul %= p;
    dp[mul] = k;
  }
  long long aq = 1;
  long long ans = 0;
  for (long long q = 1; q < p; q++) {
    aq = aq * a;
    aq %= p;
    long long x = aq * q;
    x %= p;
    x = bm(x, p - 2, p);
    x = x * b;
    x %= p;
    if (dp[x] != -1) {
      long long st = dp[x];
      if (st * p + q > X) continue;
      long long lo = 0, hi = (X + p - 1) / p;
      while (hi > lo) {
        long long mid = (hi + lo + 1) / 2;
        if ((st + cyc * mid) * p + q <= X) {
          lo = mid;
        } else {
          hi = mid - 1LL;
        }
      }
      ans += lo + 1LL;
    }
  }
  cout << ans << endl;
}

### 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 long long N = 1e6 + 100;
long long dp[N];
long long bm(long long a, long long b, long long md) {
  if (b == 0) return 1 % md;
  long long tmp = bm(a, b / 2, md);
  tmp = tmp * tmp;
  tmp %= md;
  if (b & 1) {
    return (tmp * a) % md;
  }
  return tmp;
}
int main() {
  long long a, b, p, X;
  cin >> a >> b >> p >> X;
  long long mul = 1;
  long long cyc = 0;
  for (long long k = 1; k <= p; k++) {
    mul = mul * a;
    mul %= p;
    if (mul == 1) {
      cyc = k;
      break;
    }
  }
  mul = 1;
  memset(dp, -1, sizeof dp);
  for (long long k = 0; k < cyc; k++) {
    if (k == 0)
      mul = 1;
    else
      mul = mul * a;
    mul %= p;
    dp[mul] = k;
  }
  long long aq = 1;
  long long ans = 0;
  for (long long q = 1; q < p; q++) {
    aq = aq * a;
    aq %= p;
    long long x = aq * q;
    x %= p;
    x = bm(x, p - 2, p);
    x = x * b;
    x %= p;
    if (dp[x] != -1) {
      long long st = dp[x];
      if (st * p + q > X) continue;
      long long lo = 0, hi = (X + p - 1) / p;
      while (hi > lo) {
        long long mid = (hi + lo + 1) / 2;
        if ((st + cyc * mid) * p + q <= X) {
          lo = mid;
        } else {
          hi = mid - 1LL;
        }
      }
      ans += lo + 1LL;
    }
  }
  cout << ans << endl;
}

```