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;
const int N = (int)1e6 + 15;
const int inf = 0x3f3f3f3f;
int apow[N], invApow[N], inv[N], mp[N];
bool used[N];
int main() {
  int a, b, p;
  long long x;
  while (~scanf("%d%d%d%lld", &a, &b, &p, &x)) {
    memset(mp, 0x3f, sizeof(mp));
    memset(used, false, sizeof(used));
    inv[1] = 1;
    for (int i = 2; i < p; i++) {
      inv[i] = (((long long)-p / i * inv[p % i]) % p + p) % p;
    }
    apow[0] = invApow[0] = 1;
    mp[1] = 0;
    int y = p - 1;
    for (int i = 1; i < p - 1; i++) {
      apow[i] = (long long)apow[i - 1] * a % p;
      invApow[i] = inv[apow[i]];
      mp[invApow[i]] = min(i, mp[invApow[i]]);
      y = (!y && apow[i] == 1 ? i : y);
    }
    long long ans = 0;
    for (int n = 0; n < p - 1; n++) {
      int k = ((n - (long long)b * invApow[n]) % p + p) % p;
      if (n + (long long)k * (p - 1) > x) {
        continue;
      }
      ans += ((x - n) - (long long)(p - 1) * k) / (p - 1) / p + 1;
    }
    printf("%lld\n", ans);
  }
}