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 maxn = 1000005;
const int maxm = 2000005;
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-18;
const int MOD = 1000000007;
template <class T>
inline void read(T &x) {
  x = 0;
  T flag = 1;
  char c;
  do {
    c = (char)getchar();
    if (c == '-') break;
  } while (c < '0' || c > '9');
  if (c == '-') c = '0', flag = -1;
  do {
    x = x * 10 + c - '0';
    c = (char)getchar();
  } while (c >= '0' && c <= '9');
  x *= flag;
}
char str[1100];
string scf() {
  scanf("%s", str);
  return str;
}
template <class T>
inline void out(T x) {
  if (x > 9) out(x / 10);
  x = x % 10;
  putchar('0' + x);
}
int qpow(int x, int n, int p) {
  int ret = 1;
  while (n) {
    if (n & 1) ret = 1LL * ret * x % p;
    x = 1LL * x * x % p;
    n >>= 1;
  }
  return ret;
}
int main() {
  int a, b, p;
  long long x, c, e;
  read(a);
  read(b);
  read(p);
  read(x);
  int invbj = qpow(a, p - 2, p);
  int invb1 = invbj;
  long long ans = 0;
  for (int j = 1; j < p && j <= x; ++j) {
    c = (x - j) / (p - 1);
    e = ((j - 1LL * b * invbj % p) % p + p) % p;
    if (c >= e) ans += (c - e) / p + 1;
    invbj = 1LL * invbj * invb1 % p;
  }
  out(ans);
  return 0;
}