#include <bits/stdc++.h>
using namespace std;
template <class T>
void mini(T &l, T r) {
  l = min(l, r);
}
template <class T>
void maxi(T &l, T r) {
  l = max(l, r);
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool a) { return a ? "T" : "F"; }
string to_string(const char *s) { return to_string((string)s); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
using ll = long long;
using ld = long double;
const int N = 1e6 + 10;
ll a, b, p, x;
ll pw[N];
ll nd[N];
ll mypw(ll a, ll b, ll p) {
  ll ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % p;
    b >>= 1;
    a = a * a % p;
  }
  return ret;
}
ll inv(ll a, ll p = ::p) { return mypw(a, p - 2, p); }
ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  } else {
    ll res = exgcd(b, a % b, x, y);
    ll t = x;
    x = y;
    y = t - (a / b) * y;
    return res;
  }
}
inline ll mod(ll x, ll y) {
  ll res = x % y;
  if (res < 0) res += y;
  return res;
}
ll solve(int n, ll m[], ll c[]) {
  for (int i = 1; i <= n; i++)
    if (c[i] >= m[i]) return -1;
  ll ans = c[1], LCM = m[1], x, y, g, mm;
  for (int i = 2; i <= n; i++) {
    g = exgcd(LCM, m[i], x, y);
    if ((c[i] - ans) % g) return -1;
    ans = mod(ans + LCM * mod((c[i] - ans) / g * x, m[i] / g), LCM / g * m[i]);
    LCM = LCM / g * m[i];
  }
  return mod(ans, LCM);
}
ll calc(int a, int p, int b, int q) {
  ll m[] = {0, p, q};
  ll c[] = {0, a, b};
  ll tmp = solve(2, m, c);
  42;
  if (x < tmp) return 0;
  return (x - tmp) / (1ll * p * q) + 1 - (tmp == 0);
}
void run() {
  cin >> a >> b >> p >> x;
  pw[0] = 1;
  for (int i = 1; i < p - 1; ++i) {
    pw[i] = pw[i - 1] * a % p;
  }
  for (int i = 0; i < p - 1; ++i) {
    nd[i] = b * inv(pw[i]) % p;
  }
  ll ans = 0;
  for (int i = 0; i < p - 1; ++i) {
    ans += calc(i, p - 1, nd[i], p);
    42;
  }
  cout << ans << endl;
}
int main() {
  run();
  return 0;
}

### Prompt

Please create a solution in cpp 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;
template <class T>
void mini(T &l, T r) {
  l = min(l, r);
}
template <class T>
void maxi(T &l, T r) {
  l = max(l, r);
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool a) { return a ? "T" : "F"; }
string to_string(const char *s) { return to_string((string)s); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
using ll = long long;
using ld = long double;
const int N = 1e6 + 10;
ll a, b, p, x;
ll pw[N];
ll nd[N];
ll mypw(ll a, ll b, ll p) {
  ll ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % p;
    b >>= 1;
    a = a * a % p;
  }
  return ret;
}
ll inv(ll a, ll p = ::p) { return mypw(a, p - 2, p); }
ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  } else {
    ll res = exgcd(b, a % b, x, y);
    ll t = x;
    x = y;
    y = t - (a / b) * y;
    return res;
  }
}
inline ll mod(ll x, ll y) {
  ll res = x % y;
  if (res < 0) res += y;
  return res;
}
ll solve(int n, ll m[], ll c[]) {
  for (int i = 1; i <= n; i++)
    if (c[i] >= m[i]) return -1;
  ll ans = c[1], LCM = m[1], x, y, g, mm;
  for (int i = 2; i <= n; i++) {
    g = exgcd(LCM, m[i], x, y);
    if ((c[i] - ans) % g) return -1;
    ans = mod(ans + LCM * mod((c[i] - ans) / g * x, m[i] / g), LCM / g * m[i]);
    LCM = LCM / g * m[i];
  }
  return mod(ans, LCM);
}
ll calc(int a, int p, int b, int q) {
  ll m[] = {0, p, q};
  ll c[] = {0, a, b};
  ll tmp = solve(2, m, c);
  42;
  if (x < tmp) return 0;
  return (x - tmp) / (1ll * p * q) + 1 - (tmp == 0);
}
void run() {
  cin >> a >> b >> p >> x;
  pw[0] = 1;
  for (int i = 1; i < p - 1; ++i) {
    pw[i] = pw[i - 1] * a % p;
  }
  for (int i = 0; i < p - 1; ++i) {
    nd[i] = b * inv(pw[i]) % p;
  }
  ll ans = 0;
  for (int i = 0; i < p - 1; ++i) {
    ans += calc(i, p - 1, nd[i], p);
    42;
  }
  cout << ans << endl;
}
int main() {
  run();
  return 0;
}

```