#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 15;
const int inf = 0x3f3f3f3f;
int apow[N], invApow[N], inv[N], mp[N];
bool used[N];
int main() {
  int a, b, p;
  long long x;
  while (~scanf("%d%d%d%lld", &a, &b, &p, &x)) {
    memset(mp, 0x3f, sizeof(mp));
    memset(used, false, sizeof(used));
    inv[1] = 1;
    for (int i = 2; i < p; i++) {
      inv[i] = (((long long)-p / i * inv[p % i]) % p + p) % p;
    }
    apow[0] = invApow[0] = 1;
    mp[1] = 0;
    int y = p - 1;
    for (int i = 1; i < p - 1; i++) {
      apow[i] = (long long)apow[i - 1] * a % p;
      invApow[i] = inv[apow[i]];
      mp[invApow[i]] = min(i, mp[invApow[i]]);
      y = (!y && apow[i] == 1 ? i : y);
    }
    long long ans = 0;
    for (int n = 0; n < p - 1; n++) {
      int k = ((n - (long long)b * invApow[n]) % p + p) % p;
      if (n + (long long)k * (p - 1) > x) {
        continue;
      }
      ans += ((x - n) - (long long)(p - 1) * k) / (p - 1) / p + 1;
    }
    printf("%lld\n", ans);
  }
}
