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;
using ll = long long;
using ld = long double;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lca(ll a, ll b) { return a / gcd(a, b) * b; }
ld const pi = acos(-1.0);
ld const eps = 0.000000001;
template <class T>
bool mi(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool ma(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
int a, b, p;
ll x;
int bp(int n, int pow) {
  if (pow == 0) return 1;
  int e = bp(n, pow / 2);
  if (pow & 1) {
    return (((n * 1LL * e) % p) * e) % p;
  }
  return (e * 1LL * e) % p;
}
int rev(int v) { return bp(v, p - 2); }
set<int> pbyn[1000005];
int main() {
  ll re = 0;
  scanf("%d %d %d %lld", &a, &b, &p, &x);
  if (a == 1) {
    printf("%lld", (x + 2 * p - b) / p - (1 + 2 * p - b + p - 1) / p + 1);
    return 0;
  }
  pbyn[1].insert(0);
  int aa = 1;
  for (int i = 1; i < p - 1; i++) {
    aa = (aa * 1LL * a) % p;
    pbyn[aa].insert(i);
  }
  for (int q = 1; q < p; q++) {
    int c = (rev(q) * 1LL * b) % p;
    if ((int)pbyn[c].size() == 0) continue;
    for (auto &xx : pbyn[c]) {
      ll bo = 1 + q * 1LL * p - xx * 1LL * p - q;
      ll to = x - q - xx * 1LL * p + q * 1LL * p;
      while (bo < 0) {
        bo += p * 1LL * p - p;
        to += p * 1LL * p - p;
      }
      re += to / (p * 1LL * p - p) -
            (bo + p * 1LL * p - p - 1) / (p * 1LL * p - p) + 1;
    }
  }
  printf("%lld", re);
  return 0;
}