#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m) {
  long long x = 1 % m;
  a %= m;
  while (b) {
    if ((1 & b)) x = x * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return x;
}
vector<long long> sol[1000005];
int main() {
  long long a, b, p, x;
  cin >> a >> b >> p >> x;
  long long aa = 1;
  for (int q = 0; q < p - 1; q++) {
    sol[aa].push_back(q);
    aa = aa * a % p;
  }
  long long ans = 0;
  for (long long k = 1; k < p; k++) {
    if (k > x) break;
    long long d = b * power(k * power(a, k, p), p - 2, p) % p;
    if (sol[d].empty() == true) continue;
    long long lim = (x - k) / p;
    ans += ((int)sol[d].size()) * 1LL * (lim / (p - 1));
    long long upto = lim % (p - 1);
    ans += upper_bound(sol[d].begin(), sol[d].end(), upto) - sol[d].begin();
  }
  printf("%lld\n", ans);
}
