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;
void read(long long &x) {
  x = 0;
  long long f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -f;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  x *= f;
}
void print(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (!x) return;
  print(x / 10), putchar(x % 10 + 48);
}
void write(long long x) {
  if (!x)
    putchar('0');
  else
    print(x);
  putchar('\n');
}
const long long maxn = 1e6 + 10;
const long long inf = 1e9;
const double eps = 1e-8;
long long a, b, p, x;
long long qpow(long long a, long long x) {
  long long res = 1;
  for (; x; x >>= 1, a = 1ll * a * a % p)
    if (x & 1) res = 1ll * res * a % p;
  return res;
}
long long check(long long x) { return 1ll * x * qpow(a, x) % p; }
signed main() {
  read(a), read(b), read(p), read(x);
  long long ans = 0;
  for (long long n = 1; n < p && n <= x; n++) {
    long long v = 1ll * qpow(a, n) % p;
    long long t = (b - 1ll * n * v % p + p) % p, s = 1ll * (p - 1) * v % p, k;
    k = 1ll * t * qpow(s, p - 2) % p;
    long long xx = (x - n) / (p - 1);
    ans += xx >= k ? 1 + (xx - k) / p : 0;
  }
  write(ans);
  return 0;
}