#include <bits/stdc++.h>
using namespace std;
long long a, b, p, x;
long long inv[1000005];
int main() {
  scanf("%lld%lld%lld%lld", &a, &b, &p, &x);
  inv[1] = 1;
  for (int i = 2; i < 1000005; i++) inv[i] = (p - p / i) * inv[p % i] % p;
  long long ans = 0, ret = a;
  for (long long i = 1; i < p - 1 && i <= x; i++) {
    if (ret * i % p == b) ans++;
    ret = ret * a % p;
  }
  ret = 1;
  for (long long i = 0; i < p - 1; i++) {
    long long mx = (x - i) / (p - 1);
    long long xx = ((b * inv[ret] - i + p) % p) * inv[p - 1] % p;
    ans += (mx - xx) / p + (xx > 0 && xx * (p - 1) + i <= x);
    ret = ret * a % p;
  }
  return 0 * printf("%lld\n", ans);
}
