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 crt(long long, long long, long long, long long, long long &, long long &);
long long zco(long long, long long);
long long val1, vals, cmp, plj, prec;
void SieveOfErat() {
  bool prime[1000001];
  memset(prime, true, sizeof(prime));
  for (long long p = 2; p * p <= 1000000; p++) {
    if (prime[p] == true) {
      for (long long i = p * 2; i <= 1000000; i += p) prime[i] = false;
    }
  }
}
long long gcd(long long x, long long y) {
  if (x == 0) {
    return y;
  } else
    return gcd(x % y, y);
}
struct vect {
  struct vect *foll;
  long long val;
  struct vect *succ;
};
void upd(struct vect **beg, struct vect **end, long long xd) {
  struct vect *c = (struct vect *)malloc(sizeof(struct vect));
  c->val = xd;
  c->succ = NULL;
  if (*beg == NULL) {
    c->foll = NULL;
    *beg = c;
    *end = c;
    return;
  }
  (*end)->succ = c;
  c->foll = *end;
  *end = c;
}
void change(struct vect **beg, struct vect **end) {
  if (*beg == *end) {
    return;
  }
  (*end)->succ = *beg;
  (*beg)->foll = *end;
  *beg = *end;
  *end = (*end)->foll;
  (*end)->succ = NULL;
  (*beg)->foll = NULL;
}
long long store(long long lo, long long cd, long long rem) {
  long long vec = 1;
  while (cd) {
    if (cd % 2) vec = (vec * lo) % rem;
    cd >>= 1;
    lo = (lo * lo) % rem;
  }
  return vec;
}
long long extendedgcd(long long val1, long long vals, long long &plj,
                      long long &y) {
  if (val1 == 0) {
    plj = 0;
    y = 1;
    return vals;
  }
  long long x1, y1;
  long long calc = extendedgcd(vals % val1, val1, x1, y1);
  plj = y1 - (vals / val1) * x1;
  y = x1;
  if (plj < 0) {
    plj += vals;
    y -= val1;
  }
  return calc;
}
signed main() {
  scanf("%lld", &val1);
  scanf("%lld", &vals);
  scanf("%lld", &cmp);
  scanf("%lld", &plj);
  prec = store(val1, cmp - 2, cmp);
  vector<long long> arr(cmp + 1);
  arr[0] = vals;
  long long xp = min(plj, cmp);
  long long vec = 0;
  for (long long i = 1; i < cmp; i++) {
    arr[i] = (arr[i - 1] * prec) % cmp;
    long long pit, vol;
    crt(cmp, arr[i], cmp - 1, i, pit, vol);
    if (vol == 0) {
      vol = pit;
    }
    long long ref = 0;
    if ((plj) >= vol) {
      ref = 1 + ((plj - vol) / pit);
    }
    vec += ref;
  }
  cout << vec << endl;
}
long long zco(long long lo, long long cd) {
  long long plj, y;
  long long calc = extendedgcd(lo, cd, plj, y);
  assert(calc == 1);
  return plj;
}
void crt(long long b1, long long m1, long long b2, long long m2, long long &pit,
         long long &vol) {
  pit = b1 * b2;
  vol = (b1 * ((zco(b1, b2) * (b2 + (m2 - m1) % b2)) % b2) + m1) % pit;
}