#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3fll;
const int MAX = 3e5 + 10;
const int MOD = 10007;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
inline void read(T &num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg) num = -num;
}
inline long long powMM(long long a, long long b, long long M) {
  long long ret = 1;
  a %= M;
  while (b) {
    if (b & 1) ret = ret * a % M;
    b >>= 1;
    a = a * a % M;
  }
  return ret;
}
void open() {
  freopen("in2.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
}
long long extend_gcd(long long a, long long b, long long &x, long long &y) {
  if (a == 0 && b == 0) return -1;
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extend_gcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
long long m[10], a[10];
bool solve(long long &m0, long long &a0, long long m, long long a) {
  long long y, x;
  long long g = extend_gcd(m0, m, x, y);
  if (abs(a - a0) % g) return false;
  x *= (a - a0) / g;
  x %= m / g;
  a0 = (x * m0 + a0);
  m0 *= m / g;
  a0 %= m0;
  if (a0 < 0) a0 += m0;
  return true;
}
bool MLES(long long &m0, long long &a0, long long n) {
  bool flag = true;
  m0 = 1;
  a0 = 0;
  for (long long i = 0; i < n; i++)
    if (!solve(m0, a0, m[i], a[i])) {
      flag = false;
      break;
    }
  return flag;
}
long long aa, b, p, x, now = 1;
long long ans, chu;
int main() {
  scanf("%I64d%I64d%I64d%I64d", &aa, &b, &p, &x);
  long long an, mo;
  m[0] = p - 1;
  m[1] = p;
  chu = p * (p - 1);
  for (long long i = 0; i < p - 1; i++) {
    a[0] = i;
    a[1] = b % p * powMM(now, p - 2, p) % p;
    if (MLES(mo, an, 2)) ans += (x - an + chu) / chu;
    now = now * aa % p;
  }
  printf("%I64d\n", ans);
}

### 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;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3fll;
const int MAX = 3e5 + 10;
const int MOD = 10007;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
inline void read(T &num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg) num = -num;
}
inline long long powMM(long long a, long long b, long long M) {
  long long ret = 1;
  a %= M;
  while (b) {
    if (b & 1) ret = ret * a % M;
    b >>= 1;
    a = a * a % M;
  }
  return ret;
}
void open() {
  freopen("in2.txt", "r", stdin);
  freopen("out2.txt", "w", stdout);
}
long long extend_gcd(long long a, long long b, long long &x, long long &y) {
  if (a == 0 && b == 0) return -1;
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extend_gcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
long long m[10], a[10];
bool solve(long long &m0, long long &a0, long long m, long long a) {
  long long y, x;
  long long g = extend_gcd(m0, m, x, y);
  if (abs(a - a0) % g) return false;
  x *= (a - a0) / g;
  x %= m / g;
  a0 = (x * m0 + a0);
  m0 *= m / g;
  a0 %= m0;
  if (a0 < 0) a0 += m0;
  return true;
}
bool MLES(long long &m0, long long &a0, long long n) {
  bool flag = true;
  m0 = 1;
  a0 = 0;
  for (long long i = 0; i < n; i++)
    if (!solve(m0, a0, m[i], a[i])) {
      flag = false;
      break;
    }
  return flag;
}
long long aa, b, p, x, now = 1;
long long ans, chu;
int main() {
  scanf("%I64d%I64d%I64d%I64d", &aa, &b, &p, &x);
  long long an, mo;
  m[0] = p - 1;
  m[1] = p;
  chu = p * (p - 1);
  for (long long i = 0; i < p - 1; i++) {
    a[0] = i;
    a[1] = b % p * powMM(now, p - 2, p) % p;
    if (MLES(mo, an, 2)) ans += (x - an + chu) / chu;
    now = now * aa % p;
  }
  printf("%I64d\n", ans);
}

```