#include <bits/stdc++.h>
using namespace std;
long long x, ans, P, a, b, p, t, k;
long long bin(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return ans;
}
int main() {
  cin >> a >> b >> p >> x;
  P = p * (p - 1);
  for (int j = 1; j < p; j++) {
    t = b * bin(a, p - j - 1) % p, k = (p * j % P + (p - t) * (p - 1) % P) % P;
    ans += x / P;
    if (x % P >= k) ans++;
  }
  cout << ans;
}
