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;
inline long long readint() {
  long long a = 0;
  char c = getchar(), f = 1;
  for (; c < '0' or c > '9'; c = getchar())
    if (c == '-') f = -f;
  for (; '0' <= c and c <= '9'; c = getchar())
    a = (a << 3) + (a << 1) + (c ^ 48);
  return a * f;
}
inline void writeint(long long x) {
  if (x > 9) writeint(x / 10);
  putchar((x % 10) ^ 48);
}
template <class T>
void getMax(T &a, const T &b) {
  if (a < b) a = b;
}
template <class T>
void getMin(T &a, const T &b) {
  if (b < a) a = b;
}
const int MaxP = 1000005;
int cnt[MaxP], inv[MaxP] = {1, 1};
int main() {
  int a = readint(), b = readint(), p = readint();
  int rp = p;
  p = p * (MaxP / p);
  long long x = readint();
  if (x % p == 0) --x;
  for (int i = (inv[1] = 1) + 1; i < rp; ++i)
    inv[i] = (0ll + rp - rp / i) * inv[rp % i] % rp;
  int a_p = 1;
  long long ans = 0;
  for (int i = 1; i <= p; ++i) a_p = 1ll * a_p * a % rp;
  for (int r = 1, now = rp - b; r < p; ++r)
    now = 1ll * now * a % rp,
    ((r % rp != 0) && ++cnt[1ll * now * inv[r % rp] % rp]);
  for (int m = 1, now = 1; 1ll * m * p - (p - 1) <= x; ++m)
    now = 1ll * now * a_p % rp, ans += cnt[now];
  for (int i = 0; i < MaxP; ++i) cnt[i] = 0;
  for (int r = 1, now = rp - b; r < p - (x % p); ++r)
    now = 1ll * now * a % rp,
    ((r % rp != 0) && ++cnt[1ll * now * inv[r % rp] % rp]);
  for (int m = 1, now = 1; true; ++m)
    if (1ll * m * p - (p - 1) > x) {
      ans -= cnt[now];
      break;
    } else
      now = 1ll * now * a_p % rp;
  printf("%lld\n", ans);
  return 0;
}