def gcd(x, y):
while y != 0:
(x, y) = (y, x % y)
return x
l,r,p,q=map( int , raw_input().split() )
t=1
ans=0
while p**t <= r :
z=t+1
tavanp=p ** t
tavanp1=p ** z
tavanq=q ** t
lcm=tavanp*tavanq/(gcd(tavanp,tavanq))
lcm3=(tavanp1*tavanq)/(gcd(tavanp1,tavanq))
plus=(r/tavanp) - ( (l-1)/tavanp )
minus=(r/lcm) - ( (l-1)/lcm )
minus1=(r/tavanp1) - ( (l-1)/tavanp1 )
plus1=(r/lcm3) - ( (l-1)/lcm3 )
ans=ans+plus-minus-minus1+plus1
t=t+1;
print(ans);
ZGVmIGdjZCh4LCB5KToKICAgIHdoaWxlIHkgIT0gMDoKICAgICAgICAoeCwgeSkgPSAoeSwgeCAlIHkpCiAgICByZXR1cm4geApsLHIscCxxPW1hcCggaW50ICwgcmF3X2lucHV0KCkuc3BsaXQoKSApCnQ9MQphbnM9MAp3aGlsZSBwKip0IDw9IHIgOgoJej10KzEKCXRhdmFucD1wICoqIHQKCXRhdmFucDE9cCAqKiB6Cgl0YXZhbnE9cSAqKiB0CglsY209dGF2YW5wKnRhdmFucS8oZ2NkKHRhdmFucCx0YXZhbnEpKQoJbGNtMz0odGF2YW5wMSp0YXZhbnEpLyhnY2QodGF2YW5wMSx0YXZhbnEpKQoJcGx1cz0oci90YXZhbnApIC0gKCAobC0xKS90YXZhbnAgKQoJbWludXM9KHIvbGNtKSAtICggKGwtMSkvbGNtICkKCW1pbnVzMT0oci90YXZhbnAxKSAtICggKGwtMSkvdGF2YW5wMSApCglwbHVzMT0oci9sY20zKSAtICggKGwtMSkvbGNtMyApCglhbnM9YW5zK3BsdXMtbWludXMtbWludXMxK3BsdXMxCgl0PXQrMTsKcHJpbnQoYW5zKTsKCg==