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;
int a, b, p, pp, ip, ipp;
long long x;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
int power(int a, int k, int mod) {
  int ret = 1;
  for (; k; k >>= 1, a = 1ll * a * a % mod)
    if (k & 1) ret = 1ll * ret * a % mod;
  return ret;
}
void ex_gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  } else
    ex_gcd(b, a % b, x, y);
  int t = x;
  x = y, y = t - a / b * y;
}
int inv(int a, int p) {
  int x = 0, y = 0;
  ex_gcd(a, p, x, y);
  return (x % p + p) % p;
}
int main() {
  cin >> a >> b >> p >> x;
  pp = p - 1;
  if (p == 2) {
    printf("%lld\n", (x + 1) >> 1);
    return 0;
  }
  ip = inv(p, pp);
  ipp = power(pp, p - 2, p);
  a = power(a, p - 2, p);
  int res = b;
  long long ans = 0;
  for (int i = 1; i < p; i++) {
    res = 1ll * res * a % p;
    long long mod = 1ll * p * pp,
              ta = (1ll * i * p * ip + 1ll * res * pp * ipp) % mod;
    if (ta <= x) ans += (x - ta) / mod + 1;
  }
  cout << ans << endl;
  return 0;
}