#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const long long LINF = 1LL << 60;
const double inf = 1e15;
long long mod = 1e9 + 7;
char READ_DATA;
int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v) {
  SIGNAL_INPUT = 1;
  while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
    if (READ_DATA == '-')
      SIGNAL_INPUT = -1;
    else if (READ_DATA == EOF)
      return EOF;
  v = READ_DATA - '0';
  while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9')
    v = v * 10 + READ_DATA - '0';
  v *= SIGNAL_INPUT;
  return v;
}
inline long long modru(long long &v) {
  long long p = 0;
  SIGNAL_INPUT = 1;
  while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
    if (READ_DATA == '-')
      SIGNAL_INPUT = -1;
    else if (READ_DATA == EOF)
      return EOF;
  p = v = READ_DATA - '0';
  while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9') {
    v = (v * 10 + READ_DATA - '0') % mod;
    p = (p * 10 + READ_DATA - '0') % (mod - 1);
  }
  v *= SIGNAL_INPUT;
  return p;
}
template <typename A, typename B>
inline char ru(A &x, B &y) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  return 2;
}
template <typename A, typename B, typename C>
inline char ru(A &x, B &y, C &z) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  return 3;
}
template <typename A, typename B, typename C, typename D>
inline char ru(A &x, B &y, C &z, D &w) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  ru(w);
  return 4;
}
struct Edge {
  int u, v, next;
  long long w, cap, flow;
  Edge(int _u = 0, int _v = 0, int nxt = -1, long long _w = 1,
       long long _cap = 0) {
    u = _u;
    v = _v;
    w = _w;
    cap = _cap;
    flow = 0;
    next = nxt;
  }
  int operator<(const Edge &b) const { return w < b.w; }
};
const int maxn = 2e6 + 3, N = 2e7, SZ = 21;
double eps = 1e-7, pi = acos(-1.0);
long long seed = 131, smod = (1LL << 32) - 267;
long long a, b, p, n;
long long sp(long long a, long long b, long long mod) {
  long long ans = 1, x = a;
  while (b) {
    if (b & 1) (ans *= x) %= mod;
    (x *= x) %= mod;
    b >>= 1;
  }
  return ans;
}
long long c[maxn];
long long ans1, ans2;
int main() {
  cin >> a >> b >> p >> n;
  c[0] = 1;
  for (register int j = 1; j < p; ++j) c[j] = c[j - 1] * a % p;
  for (register int j = 1; j < p; ++j) {
    long long y = c[j * (p - 2) % (p - 1)] * b % p;
    long long t = (j - y + p) % p;
    if (t * (p - 1) + j > n) continue;
    long long u = ((n - j) / (p - 1) - t) / p;
    ans2 += (u + 1);
  }
  cout << ans2;
}

### Prompt

Please formulate a cpp solution to the following 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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const long long LINF = 1LL << 60;
const double inf = 1e15;
long long mod = 1e9 + 7;
char READ_DATA;
int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v) {
  SIGNAL_INPUT = 1;
  while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
    if (READ_DATA == '-')
      SIGNAL_INPUT = -1;
    else if (READ_DATA == EOF)
      return EOF;
  v = READ_DATA - '0';
  while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9')
    v = v * 10 + READ_DATA - '0';
  v *= SIGNAL_INPUT;
  return v;
}
inline long long modru(long long &v) {
  long long p = 0;
  SIGNAL_INPUT = 1;
  while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
    if (READ_DATA == '-')
      SIGNAL_INPUT = -1;
    else if (READ_DATA == EOF)
      return EOF;
  p = v = READ_DATA - '0';
  while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9') {
    v = (v * 10 + READ_DATA - '0') % mod;
    p = (p * 10 + READ_DATA - '0') % (mod - 1);
  }
  v *= SIGNAL_INPUT;
  return p;
}
template <typename A, typename B>
inline char ru(A &x, B &y) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  return 2;
}
template <typename A, typename B, typename C>
inline char ru(A &x, B &y, C &z) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  return 3;
}
template <typename A, typename B, typename C, typename D>
inline char ru(A &x, B &y, C &z, D &w) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  ru(w);
  return 4;
}
struct Edge {
  int u, v, next;
  long long w, cap, flow;
  Edge(int _u = 0, int _v = 0, int nxt = -1, long long _w = 1,
       long long _cap = 0) {
    u = _u;
    v = _v;
    w = _w;
    cap = _cap;
    flow = 0;
    next = nxt;
  }
  int operator<(const Edge &b) const { return w < b.w; }
};
const int maxn = 2e6 + 3, N = 2e7, SZ = 21;
double eps = 1e-7, pi = acos(-1.0);
long long seed = 131, smod = (1LL << 32) - 267;
long long a, b, p, n;
long long sp(long long a, long long b, long long mod) {
  long long ans = 1, x = a;
  while (b) {
    if (b & 1) (ans *= x) %= mod;
    (x *= x) %= mod;
    b >>= 1;
  }
  return ans;
}
long long c[maxn];
long long ans1, ans2;
int main() {
  cin >> a >> b >> p >> n;
  c[0] = 1;
  for (register int j = 1; j < p; ++j) c[j] = c[j - 1] * a % p;
  for (register int j = 1; j < p; ++j) {
    long long y = c[j * (p - 2) % (p - 1)] * b % p;
    long long t = (j - y + p) % p;
    if (t * (p - 1) + j > n) continue;
    long long u = ((n - j) / (p - 1) - t) / p;
    ans2 += (u + 1);
  }
  cout << ans2;
}

```