Problem: Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b   (mod\;p), where a, b, p$$$ are all known constants.

Input

The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.

Output

Print a single integer: the number of possible answers n.

Examples

Input

2 3 5 8


Output

2


Input

4 6 7 13


Output

1


Input

233 233 10007 1


Output

1

Note

In the first sample, we can see that n=2 and n=8 are possible answers.

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        r460E solver = new r460E();
        solver.solve(1, in, out);
        out.close();
    }

    static class r460E {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int a = in.ni(), b = in.ni(), p = in.ni();
            long x = in.nl();
            long[] invs = IntegerUtils.invs(p, p);
            long ainv = BigInteger.valueOf(a).modPow(BigInteger.valueOf(p - 2), BigInteger.valueOf(p)).longValue();
            long ar = 1;
            long[] cnt = new long[p];
            long ans = 0;
            int am = BigInteger.valueOf(a).modPow(BigInteger.valueOf(x / p), BigInteger.valueOf(p)).intValue();
            for (int r = 0; r < p; r++) {
                int num = (int) (invs[r] * ar % p * b % p);
                cnt[num]++;
                if (r <= x % p && am == num) {
                    ans++;
                }
                ar = ar * ainv % p;
            }
            long m = x / p;
            if (m > 0) {
                m--;
                long k = m / (p - 1);
                int t = (int) (m % (p - 1));
                ans += k * (p - 1);
                long at = 1;
                for (int i = 0; i <= t; i++) {
                    ans += cnt[(int) (at)];
                    at = at * a % p;
                }
            }
            out.println(ans);

        }

    }

    static class IntegerUtils {
        public static long[] invs(int n, long p) {
            long[] res = new long[n];
            res[1] = 1;
            for (int i = 2; i < n; i++) {
                res[i] = (p - (p / i) * res[(int) (p % i)] % p) % p;
            }
            return res;
        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String ns() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String rl = in.readLine();
                    if (rl == null) {
                        return null;
                    }
                    st = new StringTokenizer(rl);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int ni() {
            return Integer.parseInt(ns());
        }

        public long nl() {
            return Long.parseLong(ns());
        }

    }
}