#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const long long INF = 1LL << 29;
const long long mod = 1e9 + 7;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  for (long long(i) = 0; (i) < (long long)(n); ++(i))
    os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
long long a, b, p, x;
int v[1000100];
long long pow_mod(long long a, long long r, long long m) {
  long long x = 1;
  while (r) {
    if (r & 1) (x *= a) %= m;
    (a *= a) %= m;
    r >>= 1;
  }
  return x;
}
int main() {
  cin >> a >> b >> p >> x;
  long long res = 0, c = -1;
  {
    memset((v), (-1), sizeof(v));
    long long a2 = 1;
    for (long long(i) = 0; (i) < (long long)(p); ++(i)) {
      if (v[a2] >= 0) {
        c = i;
        break;
      }
      v[a2] = i;
      a2 = a2 * a % p;
    }
  }
  long long x2 = x % (p * c);
  for (long long(k) = 1; (k) < (long long)(p - 1 + 1); ++(k)) {
    long long b2 = b * pow_mod(k, p - 2, p) % p;
    long long pos = v[b2];
    if (pos == -1) continue;
    res += x / (p * c);
    long long y = (k + (pos - k) * (p)) % (p * c);
    while (y < k) y += p * c;
    if (y <= x2) res++;
  }
  cout << res << endl;
  return 0;
}

### Prompt

In CPP, your task is to solve 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 double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const long long INF = 1LL << 29;
const long long mod = 1e9 + 7;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  for (long long(i) = 0; (i) < (long long)(n); ++(i))
    os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
long long a, b, p, x;
int v[1000100];
long long pow_mod(long long a, long long r, long long m) {
  long long x = 1;
  while (r) {
    if (r & 1) (x *= a) %= m;
    (a *= a) %= m;
    r >>= 1;
  }
  return x;
}
int main() {
  cin >> a >> b >> p >> x;
  long long res = 0, c = -1;
  {
    memset((v), (-1), sizeof(v));
    long long a2 = 1;
    for (long long(i) = 0; (i) < (long long)(p); ++(i)) {
      if (v[a2] >= 0) {
        c = i;
        break;
      }
      v[a2] = i;
      a2 = a2 * a % p;
    }
  }
  long long x2 = x % (p * c);
  for (long long(k) = 1; (k) < (long long)(p - 1 + 1); ++(k)) {
    long long b2 = b * pow_mod(k, p - 2, p) % p;
    long long pos = v[b2];
    if (pos == -1) continue;
    res += x / (p * c);
    long long y = (k + (pos - k) * (p)) % (p * c);
    while (y < k) y += p * c;
    if (y <= x2) res++;
  }
  cout << res << endl;
  return 0;
}

```