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.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct __s {
  __s() {
    if (1) {
      ios_base::Init i;
      cin.sync_with_stdio(0);
      cin.tie(0);
    }
  }
  ~__s() {
    if (!1)
      fprintf(stderr, "Execution time: %.3lf s.\n",
              (double)clock() / CLOCKS_PER_SEC);
    long long n;
    cin >> n;
  }
} __S;
long long idx[1111111];
long long idx2[1111111];
bool u[1111111];
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long binpow(long long x, long long n, long long p) {
  if (n == 0) return 1;
  if (n == 1) return x % p;
  if (n & 1) {
    long long y = binpow(x, n - 1, p);
    return (x * y) % p;
  } else {
    long long y = binpow(x, n / 2, p);
    return (y * y) % p;
  }
}
long long inverse(long long x, long long p) { return binpow(x, p - 2, p); }
int main(void) {
  long long a, b, p, x;
  cin >> a >> b >> p >> x;
  vector<long long> v;
  v.push_back(a);
  idx[a] = 0;
  u[a] = true;
  for (long long i = 0; i < (long long)(p); i++) {
    long long res = (v.back() * a) % p;
    if (res == a) break;
    idx[res] = v.size();
    u[res] = true;
    v.push_back(res);
  }
  long long A = binpow(a, p + 1, p);
  long long dx = idx[A];
  vector<long long> w;
  for (long long i = 0, j = 0; i < v.size(); i++, j = (j + dx) % v.size()) {
    idx2[v[j]] = w.size();
    w.push_back(v[j]);
  }
  long long d = lcm(p, v.size());
  long long m = x / d;
  x -= m * d;
  long long ans = 0;
  for (long long n = 1; n < p; n++) {
    long long a = (b * inverse(n, p)) % p;
    if (!u[a]) continue;
    ans += m;
    long long A = v[(n - 1) % v.size()];
    long long l = idx2[A];
    long long r = idx2[a];
    if (l == r && n <= x) {
      ans++;
      continue;
    }
    long long m;
    if (l < r) {
      m = (r - l) * p + n;
    } else {
      m = (v.size() - (l - r)) * p + n;
    }
    if (m <= x) ans++;
  }
  cout << ans << '\n';
  return 0;
}