#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long m) {
  long long ans = 1;
  while (y) {
    if (y & 1) ans = (ans * x) % m;
    x = (x * x) % m;
    y /= 2;
  }
  return ans;
}
int main() {
  long long a, b, p, x;
  cin >> a >> b >> p >> x;
  long long ans = 0;
  map<long long, bool> m;
  for (long long i = 0; i < p; i++) {
    long long temp = (power(power(a, i, p), p - 2, p) * b) % p;
    long long j = ((temp - i + p) * power(p - 1, p - 2, p)) % p;
    long long d = i + j * (p - 1);
    if (m[d] == 0 && d <= x) {
      m[d] = 1;
      ans += ((x - d) / (p * (p - 1LL)) + 1LL);
    }
  }
  cout << ans << '\n';
}
