#include <bits/stdc++.h>
using namespace std;
long long a, b, p, x, c;
vector<int> o[1000004];
vector<int> prime;
int power(int a, int b, int p) {
  if (b == 0) return 1;
  if (b == 1) return a % p;
  int x = power(a, b / 2, p);
  if (b % 2 == 0) {
    return 1LL * x * x % p;
  }
  return 1LL * x * x * a % p;
}
int find_first(int a, int b, int p, long long x) {
  for (int i = 1; i <= x; ++i) {
    if (1LL * i * power(a, i, p) % p == b) {
      return i;
    }
  }
}
vector<int> get_primes() {
  const int mx = 1000004;
  vector<int> res;
  for (int i = 2; i < mx; ++i) {
    bool fl = true;
    for (int q = 2; q * q <= i; ++q) {
      if (i % q == 0) {
        fl = false;
        break;
      }
    }
    if (fl) {
      res.push_back(i);
    }
  }
  return res;
}
long long euq(long long a, long long b, long long& x, long long& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long g = euq(b, a % b, x1, y1);
  x = y1;
  y = x1 - (a / b) * y1;
  return g;
}
long long get_such_n(int k) {
  int mod = 1LL * b * power(k, p - 2, p) % p;
  int kk = k;
  long long res = 0;
  for (int i = 0; i < min(size_t(1), o[kk].size()); ++i) {
    k = o[kk][i];
    long long new_mod = 1LL * p * c;
    long long xx, yy;
    euq(c, p, xx, yy);
    xx *= mod - k;
    long long first = ((1LL * xx * c + k) % new_mod + new_mod) % (new_mod);
    long long ans = x / new_mod;
    res += ans + ((x % new_mod) >= first);
  }
  return res;
}
int main() {
  prime = get_primes();
  cin >> a >> b >> p >> x;
  int tmp = 1;
  o[1].push_back(0);
  c = -1;
  for (int i = 1; i < p; ++i) {
    tmp = 1LL * tmp * a % p;
    if (o[tmp].size() > 0 && c == -1) {
      c = i;
    }
    o[tmp].push_back(i);
  }
  long long ans = 0;
  for (int i = 1; i < p; ++i) {
    ans += get_such_n(i);
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

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;
long long a, b, p, x, c;
vector<int> o[1000004];
vector<int> prime;
int power(int a, int b, int p) {
  if (b == 0) return 1;
  if (b == 1) return a % p;
  int x = power(a, b / 2, p);
  if (b % 2 == 0) {
    return 1LL * x * x % p;
  }
  return 1LL * x * x * a % p;
}
int find_first(int a, int b, int p, long long x) {
  for (int i = 1; i <= x; ++i) {
    if (1LL * i * power(a, i, p) % p == b) {
      return i;
    }
  }
}
vector<int> get_primes() {
  const int mx = 1000004;
  vector<int> res;
  for (int i = 2; i < mx; ++i) {
    bool fl = true;
    for (int q = 2; q * q <= i; ++q) {
      if (i % q == 0) {
        fl = false;
        break;
      }
    }
    if (fl) {
      res.push_back(i);
    }
  }
  return res;
}
long long euq(long long a, long long b, long long& x, long long& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long g = euq(b, a % b, x1, y1);
  x = y1;
  y = x1 - (a / b) * y1;
  return g;
}
long long get_such_n(int k) {
  int mod = 1LL * b * power(k, p - 2, p) % p;
  int kk = k;
  long long res = 0;
  for (int i = 0; i < min(size_t(1), o[kk].size()); ++i) {
    k = o[kk][i];
    long long new_mod = 1LL * p * c;
    long long xx, yy;
    euq(c, p, xx, yy);
    xx *= mod - k;
    long long first = ((1LL * xx * c + k) % new_mod + new_mod) % (new_mod);
    long long ans = x / new_mod;
    res += ans + ((x % new_mod) >= first);
  }
  return res;
}
int main() {
  prime = get_primes();
  cin >> a >> b >> p >> x;
  int tmp = 1;
  o[1].push_back(0);
  c = -1;
  for (int i = 1; i < p; ++i) {
    tmp = 1LL * tmp * a % p;
    if (o[tmp].size() > 0 && c == -1) {
      c = i;
    }
    o[tmp].push_back(i);
  }
  long long ans = 0;
  for (int i = 1; i < p; ++i) {
    ans += get_such_n(i);
  }
  cout << ans << endl;
  return 0;
}

```