#include <bits/stdc++.h>
using namespace std;
int a, b, p, pp, ip, ipp;
long long x;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
int power(int a, int k, int mod) {
  int ret = 1;
  for (; k; k >>= 1, a = 1ll * a * a % mod)
    if (k & 1) ret = 1ll * ret * a % mod;
  return ret;
}
void ex_gcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  } else
    ex_gcd(b, a % b, x, y);
  int t = x;
  x = y, y = t - a / b * y;
}
int inv(int a, int p) {
  int x = 0, y = 0;
  ex_gcd(a, p, x, y);
  return (x % p + p) % p;
}
int main() {
  cin >> a >> b >> p >> x;
  pp = p - 1;
  if (p == 2) {
    printf("%lld\n", (x + 1) >> 1);
    return 0;
  }
  ip = inv(p, pp);
  ipp = power(pp, p - 2, p);
  a = power(a, p - 2, p);
  int res = b;
  long long ans = 0;
  for (int i = 1; i < p; i++) {
    res = 1ll * res * a % p;
    long long mod = 1ll * p * pp,
              ta = (1ll * i * p * ip + 1ll * res * pp * ipp) % mod;
    if (ta <= x) ans += (x - ta) / mod + 1;
  }
  cout << ans << endl;
  return 0;
}
