/*--------------------------------------------------------------*/
/* ACM ICPC 2009-2010, NEERC                                    */
/* Northern Subregional Contest                                 */
/* St Petersburg, October 17, 2009                              */
/*--------------------------------------------------------------*/
/* Problem J. Jealous Numbers                                   */
/*                                                              */
/* Original idea         Andrew Stankevich                      */
/* Problem statement     Andrew Stankevich                      */
/* Testset               Andrew Stankevich                      */
/*--------------------------------------------------------------*/
/* Solution                                                     */
/*                                                              */
/* Author                Andrew Stankevich                      */
/*--------------------------------------------------------------*/
 
import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class jealous_as implements Runnable {
    private static final long MAX = 1000000000000000000L;

    private static final int MAXP = 1000000000;

    private static final int MAXD = 100;

    long calc(long a, long p, long q) {
        long[][] c = new long[MAXD][MAXD];
        for (int i = 0; i < MAXD; i++) {
            for (int j = 0; j < MAXD; j++) {
                long b = a;
                for (int k = 0; k < i; k++) {
                    b /= p;
                }
                for (int k = 0; k < j; k++) {
                    b /= q;
                }
                c[i][j] = b;
            }
        }
        for (int i = 0; i < MAXD - 1; i++) {
            for (int j = 0; j < MAXD - 1; j++) {
                c[i][j] = c[i][j] - c[i + 1][j] - c[i][j + 1] + c[i + 1][j + 1];
            }
        }
        long r = 0;
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < i; j++) {
                r += c[i][j];
            }
        }
        return r;
    }

    public void solve() throws IOException {
        Scanner in = new Scanner(new File("jealous.in"));
        PrintWriter out = new PrintWriter(new File("jealous.out"));

        long a = in.nextLong();
        assert 1 <= a && a <= MAX;
        long b = in.nextLong();
        assert a <= b && b <= MAX;
        int p = in.nextInt();
        assert 2 <= p && p <= MAXP;
        assert BigInteger.valueOf(p).isProbablePrime(200);
        int q = in.nextInt();
        assert 2 <= q && q <= MAXP;
        assert BigInteger.valueOf(q).isProbablePrime(200);
        assert p != q;

        out.println(calc(b, p, q) - calc(a - 1, p, q));

        in.close();
        out.close();
    }

    public void run() {
        try {
            solve();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(64);
        }
    }

    public static void main(String[] arg) throws IOException {
        new Thread(new jealous_as()).start();
    }
}
