#include <bits/stdc++.h>
using namespace std;
int power(int a, unsigned int b, unsigned int mod) {
  if (b == 0) {
    return 1;
  }
  int p;
  p = power(a, b / 2, mod) % (mod);
  p = ((long long)p * (long long)p) % ((long long)mod);
  return (b % 2 == 0) ? (long long)p
                      : ((long long)a * (long long)p) % (long long)mod;
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int modInverse(int a, int m) {
  int g = gcd(a, m);
  if (g != 1)
    return 0;
  else {
    return power(a, m - 2, m);
  }
}
int modInverse2(int a, int m) {
  a = a % m;
  for (int x = 1; x < m; x++) {
    if (((long long)a * (long long)x) % (long long)m == 1) {
      return x;
    }
  }
}
void codeforces_460_E() {
  int a, b, p;
  long long x;
  scanf("%d %d %d %lld", &a, &b, &p, &x);
  vector<long long> v;
  long long j = (long long)a;
  while (j != 1) {
    v.push_back(((long long)b * modInverse(j, p)) % p);
    j = (j * a) % p;
  }
  v.push_back(((long long)b));
  int len = (int)v.size();
  vector<long long> w;
  for (int i = 0; i < len; i++) {
    w.push_back((((long long)p * modInverse2(p, len)) %
                     ((long long)p * (long long)len) * ((long long)i + 1) +
                 ((long long)len * modInverse(len, p)) %
                     ((long long)p * (long long)len) * v[i] +
                 (long long)p * (long long)len) %
                ((long long)p * (long long)len));
  }
  long long ans = 0;
  for (int i = 0; i < len; i++) {
    if (x >= w[i]) {
      ans += ((x - w[i]) / ((long long)p * (long long)len) + 1);
    }
    if (w[i] == 0) {
      ans--;
    }
  }
  printf("%lld", ans);
}
int main() {
  codeforces_460_E();
  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>
using namespace std;
int power(int a, unsigned int b, unsigned int mod) {
  if (b == 0) {
    return 1;
  }
  int p;
  p = power(a, b / 2, mod) % (mod);
  p = ((long long)p * (long long)p) % ((long long)mod);
  return (b % 2 == 0) ? (long long)p
                      : ((long long)a * (long long)p) % (long long)mod;
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int modInverse(int a, int m) {
  int g = gcd(a, m);
  if (g != 1)
    return 0;
  else {
    return power(a, m - 2, m);
  }
}
int modInverse2(int a, int m) {
  a = a % m;
  for (int x = 1; x < m; x++) {
    if (((long long)a * (long long)x) % (long long)m == 1) {
      return x;
    }
  }
}
void codeforces_460_E() {
  int a, b, p;
  long long x;
  scanf("%d %d %d %lld", &a, &b, &p, &x);
  vector<long long> v;
  long long j = (long long)a;
  while (j != 1) {
    v.push_back(((long long)b * modInverse(j, p)) % p);
    j = (j * a) % p;
  }
  v.push_back(((long long)b));
  int len = (int)v.size();
  vector<long long> w;
  for (int i = 0; i < len; i++) {
    w.push_back((((long long)p * modInverse2(p, len)) %
                     ((long long)p * (long long)len) * ((long long)i + 1) +
                 ((long long)len * modInverse(len, p)) %
                     ((long long)p * (long long)len) * v[i] +
                 (long long)p * (long long)len) %
                ((long long)p * (long long)len));
  }
  long long ans = 0;
  for (int i = 0; i < len; i++) {
    if (x >= w[i]) {
      ans += ((x - w[i]) / ((long long)p * (long long)len) + 1);
    }
    if (w[i] == 0) {
      ans--;
    }
  }
  printf("%lld", ans);
}
int main() {
  codeforces_460_E();
  return 0;
}

```