AtCoder Beginner Contest 131 - Anti-Division

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

-----Constraints-----
 - 1\leq A\leq B\leq 10^{18}
 - 1\leq C,D\leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
A B C D

-----Output-----
Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

-----Sample Input-----
4 9 2 3

-----Sample Output-----
2

5 and 7 satisfy the condition.

Source: atcoder | Difficulty: EASY


Solutions:
import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(A, B, C, D) = list(map(int, input().split()))
l = lcm(C, D)
temp = l - (l // C + l // D - 1)
a = (A - 1) % l
b = B % l
aa = (A - 1) // l
bb = B // l
tempA = a - (a // C + a // D - 1)
tempB = b - (b // C + b // D - 1)
ans = temp * bb + tempB - (temp * aa + tempA)
print(ans)


(a, b, c, d) = map(int, input().split())
import fractions
a = a - 1
e = c * d // fractions.gcd(c, d)
cnt = b // c + b // d + a // e - a // c - a // d - b // e
print(b - a - cnt)


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(A, B, C, D) = map(int, input().split())
x = B // lcm(C, D) - (A - 1) // lcm(C, D)
y = B // C - (A - 1) // C
z = B // D - (A - 1) // D
ans = B - A + 1 - (y + z - x)
print(ans)


import math
(a, b, c, d) = map(int, input().split())
lcm = c * d // math.gcd(c, d)
print(b - (b // c + b // d - b // lcm) - (a - 1 - ((a - 1) // d + (a - 1) // c - (a - 1) // lcm)))


import fractions
(A, B, C, D) = map(int, input().split())
E = C * D // fractions.gcd(C, D)
X = 0
Y = 0
X = A - 1 - (A - 1) // C - (A - 1) // D + (A - 1) // E
Y = B - B // C - B // D + B // E
print(Y - X)


from fractions import gcd
(a, b, c, d) = map(int, input().split())

def not_c_d(x):
	return x - (x // c + x // d - x // (c * d // gcd(c, d)))
print(not_c_d(b) - not_c_d(a - 1))


import math
(A, B, C, D) = map(int, input().split())
E = C * D // math.gcd(C, D)
c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
e = B // E - (A - 1) // E
print(B - A + 1 - c - d + e)


import math
(A, B, C, D) = map(int, input().split())
if A % C == 0:
	divC = B // C - A // C + 1
else:
	divC = B // C - A // C
if A % D == 0:
	divD = B // D - A // D + 1
else:
	divD = B // D - A // D
lcm = C * D // math.gcd(C, D)
if A % lcm == 0:
	divlcm = B // lcm - A // lcm + 1
else:
	divlcm = B // lcm - A // lcm
print(B - A + 1 - divC - divD + divlcm)


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

def MI():
	return map(int, input().split())

def MF():
	return map(float, input().split())

def LI():
	return list(map(int, input().split()))

def TI():
	return tuple(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def gcd2(i, past):
	return i * past // math.gcd(i, past)

def main():
	(A, B, C, D) = MI()
	AC = (A - 1) // C
	BC = B // C
	AD = (A - 1) // D
	BD = B // D
	ACD = (A - 1) // gcd2(C, D)
	BCD = B // gcd2(C, D)
	ans = BC + BD - BCD - (AC + AD - ACD)
	print(B - A + 1 - ans)

def __starting_point():
	main()
__starting_point()


import math
[A, B, C, D] = [int(i) for i in input().split()]
u = int(C * D / math.gcd(C, D))
s = B // C - A // C + B // D - A // D - B // u + A // u + 1
t = 0
if A % C != 0:
	t -= 1
if A % D != 0:
	t -= 1
if A % u != 0:
	t += 1
s += t
ans = B - A + 1 - s
print(ans)


import fractions
(A, B, C, D) = map(int, input().split())
A -= 1
E = C * D // fractions.gcd(C, D)
print(B - B // C - B // D + B // E - A + A // C + A // D - A // E)


import fractions
(A, B, C, D) = map(int, input().split())
A -= 1
f = fractions.gcd(C, D)
f2 = C * D // f
ans = B - A - (B // C - A // C + B // D - A // D - B // f2 + A // f2)
print(ans)


import math
(A, B, C, D) = map(int, input().split())

def X(x):
	c = x // C
	d = x // D
	l = C * D // math.gcd(C, D)
	return x - c - d + x // l
print(X(B) - X(A - 1))


import math
(a, b, c, d) = list(map(int, input().split()))
gcd = c * d // math.gcd(c, d)
r = b // c + b // d - b // gcd
l = (a - 1) // c + (a - 1) // d - (a - 1) // gcd
print(b - a - (r - l) + 1)


import math
(a, b, c, d) = map(int, input().split())
l = c * d // math.gcd(c, d)

def cnt(x):
	return b // x - a // x + int(a % x == 0)
print(b - a + 1 - cnt(c) - cnt(d) + cnt(l))


(A, B, C, D) = map(int, input().split())
f = C
g = D
while f % g != 0:
	(f, g) = [g, f % g]
l = C * D // g
m = B // l
n = (A - 1) // l
print(B - B // C - B // D + m - (A - 1 - (A - 1) // C - (A - 1) // D + n))


from fractions import *
(a, b, c, d) = map(int, input().split())
f = lambda x: x - x // c - x // d + x // (c * d // gcd(c, d))
print(f(b) - f(a - 1))


import math
(a, b, c, d) = list(map(int, input().split()))
lcm = c * d // math.gcd(c, d)
a2 = a - (a // c + a // d - a // lcm)
b2 = b - (b // c + b // d - b // lcm)
print(b2 - a2 if a % c == 0 or a % d == 0 else b2 - a2 + 1)


from fractions import gcd
(A, B, C, D) = map(int, input().split())
A -= 1
CD = C * D // gcd(C, D)
print(B - A - B // C + A // C - B // D + A // D + B // CD - A // CD)


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

def I():
	return int(input())

def F():
	return float(input())

def SS():
	return input()

def LI():
	return [int(x) for x in input().split()]

def LI_():
	return [int(x) - 1 for x in input().split()]

def LF():
	return [float(x) for x in input().split()]

def LSS():
	return input().split()

def resolve():
	(A, B, C, D) = LI()

	def f(X):
		ret = X - X // C - X // D + X // (C * D // math.gcd(C, D))
		return ret
	ans = f(B) - f(A - 1)
	print(ans)

def __starting_point():
	resolve()
__starting_point()


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(A, B, C, D) = map(int, input().split())
Cres = B // C - (A - 1) // C
Dres = B // D - (A - 1) // D
CDres = B // lcm(C, D) - (A - 1) // lcm(C, D)
print(B - A + 1 - (Cres + Dres - CDres))


import math
import collections
import itertools
import copy

def YesNo(Bool):
	if Bool:
		print('Yes')
	else:
		print('No')
	return

def gcd(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)

def resolve():
	(A, B, C, D) = list(map(int, input().split()))
	Cmin = (A - 1) // C
	Dmin = (A - 1) // D
	Cmax = B // C
	Dmax = B // D
	LCM = lcm(C, D)
	LCMmin = (A - 1) // LCM
	LCMmax = B // LCM
	Cnum = Cmax - Cmin
	Dnum = Dmax - Dmin
	LCMnum = LCMmax - LCMmin
	ans = B - (A - 1) - Cnum - Dnum + LCMnum
	print(ans)
resolve()


(a, b, c, d) = map(int, input().split())
import fractions
lcm = c * d // fractions.gcd(c, d)
C = b // c - (a - 1) // c
D = b // d - (a - 1) // d
CD = b // lcm - (a - 1) // lcm
al = b - a + 1
print(al - C - D + CD)


import fractions
(a, b, c, d) = map(int, open(0).read().split())
e = c * d // fractions.gcd(c, d)
nc = b // c - (a - 1) // c
nd = b // d - (a - 1) // d
ne = b // e - (a - 1) // e
print(b - a + 1 - nc - nd + ne)


import math
(A, B, C, D) = map(int, input().split())
g = C * D // math.gcd(C, D)
print(B - A + 1 - (B // C - -(-A // C) + 1) - (B // D - -(-A // D) + 1) + (B // g - -(-A // g) + 1))


import fractions as f
(a, b, c, d) = map(int, input().split())
a -= 1
l = c // f.gcd(c, d) * d
print(b - a - b // c + a // c - b // d + a // d + b // l - a // l)


from math import gcd
(a, b, c, d) = map(int, input().split())
e = c // gcd(c, d) * d
cnt = lambda n: n // c + n // d - n // e
print(b - a + 1 - cnt(b) + cnt(a - 1))


import math
import fractions
(a, b, c, d) = map(int, input().split())
g = c * d // fractions.gcd(c, d)
k = 0
k += b // c + b // d - b // g - int(a - 1) // c - (a - 1) // d + (a - 1) // g
print(b - a + 1 - k)


(a, b, c, d) = map(int, input().split())
import math

def lcm(a, b):
	y = a * b / math.gcd(a, b)
	return int(y)
x = b - a + 1
cc = b // c - (a - 1) // c
dd = b // d - (a - 1) // d
cd = lcm(c, d)
cd = b // cd - (a - 1) // cd
wa = cc - cd + dd
print(x - wa)


import math
(a, b, c, d) = list(map(int, input().split()))
s1 = b // c - (a - 1) // c
s2 = b // d - (a - 1) // d
s12 = b // (c * d // math.gcd(c, d)) - (a - 1) // (c * d // math.gcd(c, d))
print(b - a + 1 - (s1 + s2 - s12))


from fractions import gcd
(A, B, C, D) = map(int, input().split())
E = C * D // gcd(C, D)
ans = 0
ans += B - B // C - B // D + B // E
ans -= A - 1 - (A - 1) // C - (A - 1) // D + (A - 1) // E
print(ans)


from fractions import gcd

def lcm(x, y):
	return x * y // gcd(x, y)
(A, B, C, D) = map(int, input().split())
Cwareru = B // C - (A - 1) // C
Dwareru = B // D - (A - 1) // D
CDwareru = B // lcm(C, D) - (A - 1) // lcm(C, D)
print(B - A + 1 - (Cwareru + Dwareru - CDwareru))


import math
(a, b, c, d) = list(map(int, input().split()))
GCD = math.gcd(c, d)
LCM = c * d // GCD
print(b - b // c - b // d + b // LCM - a + 1 + (a - 1) // c + (a - 1) // d - (a - 1) // LCM)


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)

def kazu(s, e, n):
	if s % n > 0:
		s2 = s - s % n
		s2 += n
	else:
		s2 = s
	e2 = e - e % n
	if s <= s2 <= e and s <= e2 <= e:
		return (e2 - s2 + n) // n
	return 0
(a, b, c, d) = list(map(int, input().split()))
nc = kazu(a, b, c)
nd = kazu(a, b, d)
ncd = kazu(a, b, lcm(c, d))
ans = b - a + 1
ans -= nc
ans -= nd
ans += ncd
print(ans)


import math
(A, B, C, D) = map(int, input().split())
lcm = C * D // math.gcd(C, D)
a = B - A + 1
b = B // C - (A - 1) // C
c = B // D - (A - 1) // D
d = B // lcm - (A - 1) // lcm
print(a - b - c + d)


import fractions
(a, b, c, d) = map(int, input().split())
cdgcd = c * d // fractions.gcd(c, d)
print(b - (a - 1) - b // c + (a - 1) // c - b // d + (a - 1) // d + b // cdgcd - (a - 1) // cdgcd)


import math
(A, B, C, D) = list(map(int, input().split()))
cd = C * D // math.gcd(C, D)
(pac, qac) = divmod(A, C)
(pad, qad) = divmod(A, D)
(pacd, qacd) = divmod(A, cd)
(pbc, qbc) = divmod(B, C)
(pbd, qbd) = divmod(B, D)
(pbcd, qbcd) = divmod(B, cd)
pac = pac - 1 if qac == 0 else pac
pad = pad - 1 if qad != 0 else pad
pacd = pacd - 1 if qacd != 0 else pacd
ans = pbc + pbd - pbcd - (pac + pad - pacd)
print(B - A - ans + 1)


from math import gcd

def f(x):
	return x - (x // C + x // D - x // lcm)
(A, B, C, D) = list(map(int, input().split()))
lcm = C * D // gcd(C, D)
print(f(B) - f(A - 1))


from fractions import gcd
(a, b, c, d) = map(int, input().split())
cd = c * d // gcd(c, d)
print(b - (b // c + b // d - b // cd) - (a - 1 - ((a - 1) // c + (a - 1) // d - (a - 1) // cd)))


def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def main():
	(A, B, C, D) = map(int, input().split())
	E = lcm(C, D)
	(a_c, a_d, a_cd) = ((A - 1) // C, (A - 1) // D, (A - 1) // E)
	(b_c, b_d, b_cd) = (B // C, B // D, B // E)
	(a, b) = (a_c + a_d - a_cd, b_c + b_d - b_cd)
	ans = B - A + 1 - (b - a)
	print(ans)
main()


import math
(a, b, c, d) = map(int, input().split())
C = b // c - (a - 1) // c
D = b // d - (a - 1) // d
x = c * d // math.gcd(c, d)
CD = b // x - (a - 1) // x
print(b - a + 1 - (C + D - CD))


import math
(A, B, C, D) = list(map(int, input().split()))

def lcm(x, y):
	return x * y // math.gcd(x, y)
lcm = lcm(C, D)
u_b_ans = B // C + B // D - B // lcm
u_a_ans = (A - 1) // C + (A - 1) // D - (A - 1) // lcm
print(B - A + 1 - u_b_ans + u_a_ans)


import math
(a, b, c, d) = map(int, input().split())
divc = b // c - (a - 1) // c
divd = b // d - (a - 1) // d
cd = b // (c * d // math.gcd(c, d)) - (a - 1) // (c * d // math.gcd(c, d))
print(b - a - divc - divd + cd + 1)


import fractions
(a, b, c, d) = map(int, input().split())
cd = c * d // fractions.gcd(c, d)
e = a - 1
f = b // c + b // d - b // cd - (e // c + e // d - e // cd)
print(b - e - f)


from fractions import gcd
(a, b, c, d) = map(int, input().split())
e = c * d // gcd(c, d)
A = (a - 1) // c + (a - 1) // d - (a - 1) // e
B = b // c + b // d - b // e
print(b - a + 1 + A - B)


import math
(a, b, c, d) = map(int, input().split())

def f(x, y):
	return x * y // math.gcd(x, y)
z = f(c, d)
print(b - a + 1 - (b // c - (a - 1) // c + (b // d - (a - 1) // d) - (b // z - (a - 1) // z)))


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(A, B, C, D) = list(map(int, input().split()))
bc = B // C
ac = (A - 1) // C
cnum = bc - ac
bd = B // D
ad = (A - 1) // D
dnum = bd - ad
lcmcd = lcm(C, D)
blcmcd = B // lcmcd
alcmcd = (A - 1) // lcmcd
lcmcdnum = blcmcd - alcmcd
print(B - A - cnum - dnum + lcmcdnum + 1)


(a, b, c, d) = map(int, input().split())
divc = b // c - (a - 1) // c
divd = b // d - (a - 1) // d
import math
l = c * d // math.gcd(c, d)
divl = b // l - (a - 1) // l
print(b - a + 1 - divc - divd + divl)


(A, B, C, D) = map(int, input().split())
E = B - A
import fractions
CD = C * D // fractions.gcd(C, D)
b = B - B // C - B // D + B // CD
A -= 1
a = A - A // C - A // D + A // CD
print(b - a)


from fractions import gcd
(A, B, C, D) = map(int, input().split())

def f(x, C, D):
	return x - x // C - x // D + x // (C * D // gcd(C, D))
print(f(B, C, D) - f(A - 1, C, D))


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(A, B, C, D) = map(int, input().split())
ans = 0
Ac = (A - 1) // C
Ad = (A - 1) // D
Acd = (A - 1) // lcm(C, D)
Bc = B // C
Bd = B // D
Bcd = B // lcm(C, D)
c = Bc - Ac
d = Bd - Ad
cd = Bcd - Acd
print(B - A + 1 - (c + d - cd))


from fractions import gcd
(A, B, C, D) = map(int, input().split())

def f(x):
	l = x // C
	m = x // D
	k = C * D // gcd(C, D)
	n = x // k
	return x - l - m + n
print(f(B) - f(A - 1))


(a, b, c, d) = list(map(int, input().split()))

def mul_count(v, w, n):
	if v % n == 0:
		s = v // n
	else:
		s = v // n + 1
	e = w // n
	if s > e:
		return 0
	else:
		return e - s + 1

def koyaku(x, y):
	big = max(x, y)
	small = min(x, y)
	amari = big % small
	if amari == 0:
		return small
	else:
		return koyaku(small, amari)
c_mul = mul_count(a, b, c)
d_mul = mul_count(a, b, d)
cd_common = koyaku(c, d)
cd_mul = c * d // cd_common
common_mul = mul_count(a, b, cd_mul)
yobun = c_mul + d_mul - common_mul
print(b - a + 1 - yobun)


(A, B, C, D) = map(int, input().split())
CC = B // C - (A - 1) // C
DD = B // D - (A - 1) // D
import fractions
F = C * D // fractions.gcd(C, D)
FF = B // F - (A - 1) // F
print(B - A + 1 - CC - DD + FF)


import math
(a, b, c, d) = map(int, input().split())
a -= 1
h = int(c * d / math.gcd(c, d))
e = b // c - a // c
f = b // d - a // d
g = b // h - a // h
print(b - a - e - f + g)


(A, B, C, D) = map(int, input().split())
from fractions import gcd
cd = C * D // gcd(D, C)
f = lambda x: B // x - (A - 1) // x
print(B - A + 1 - (f(C) + f(D) - f(cd)))


import math as m
(A, B, C, D) = map(int, input().split())
A -= 1
g = C * D // m.gcd(C, D)
x = B // C - A // C
y = B // D - A // D
z = B // g - A // g
print(B - A - x - y + z)


import fractions
(a, b, c, d) = map(int, input().split())

def cnt(x):
	return x - x // c - x // d + x // int(c * d / fractions.gcd(c, d))
print(cnt(b) - cnt(a - 1))


import math
(A, B, C, D) = list(map(int, input().split()))
lcm = C * D // math.gcd(C, D)

def num_multi(num, lb, ub):
	lR = lb // num
	lQ = lb % num
	uR = ub // num
	ans = uR - lR
	if lQ == 0:
		ans += 1
	return ans
print(B - A + 1 - num_multi(C, A, B) - num_multi(D, A, B) + num_multi(lcm, A, B))


from fractions import gcd
(a, b, c, d) = map(int, input().split())
E = b // c - (a - 1) // c
F = b // d - (a - 1) // d
LCM = c * d // gcd(c, d)
G = b // LCM - (a - 1) // LCM
print(b - a + 1 - (E + F - G))


def gcd(m, n):
	if n == 0:
		return m
	return gcd(n, m % n)

def main():
	(A, B, C, D) = list(map(int, input().split()))
	ans = B - A + 1
	ans -= B // C + B // D
	ans += (A - 1) // C + (A - 1) // D
	lcm = C // gcd(C, D) * D
	ans += B // lcm - (A - 1) // lcm
	print(ans)

def __starting_point():
	main()
__starting_point()


import math
(A, B, C, D) = map(int, input().split())

def F(N):
	x = N // C
	x += N // D
	lcm = C * D // math.gcd(C, D)
	x -= N // lcm
	return N - x
answer = F(B) - F(A - 1)
print(answer)


import fractions
(a, b, c, d) = map(int, input().split())

def t(tar, l1, l2):
	return tar - (tar // l1 + tar // l2) + tar // (l1 * l2 // fractions.gcd(l1, l2))
print(t(b, c, d) - t(a - 1, c, d))


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(a, b, c, d) = map(int, input().split())
c_div = b // c - (a - 1) // c
d_div = b // d - (a - 1) // d
cd_div = b // lcm(c, d) - (a - 1) // lcm(c, d)
ans = b - a + 1 - (c_div + d_div - cd_div)
print(ans)


import sys
import math
(a, b, c, d) = map(int, sys.stdin.readline().split())
lcm = c * d // math.gcd(c, d)
div_c = b // c - (a - 1) // c
div_d = b // d - (a - 1) // d
div_lcm = b // lcm - (a - 1) // lcm
print(b - a + 1 - div_c - div_d + div_lcm)


import math

def lcm(a):
	LCM = a[0]
	for i in range(1, len(a)):
		LCM = LCM * a[i] // math.gcd(LCM, a[i])
	return LCM

def f(k):
	return b // k - (a - 1) // k
(a, b, c, d) = map(int, input().split())
ALL = b - a + 1
ans = ALL - (f(c) + f(d) - f(lcm([c, d])))
print(ans)


import fractions
(a, b, c, d) = map(int, input().split())
k = c * d // fractions.gcd(c, d)
sc = b // c - (a - 1) // c
sd = b // d - (a - 1) // d
sk = b // k - (a - 1) // k
print(b - a + 1 - sc - sd + sk)


from math import gcd
(a, b, c, d) = map(int, input().split())
t = c * d // gcd(c, d)
x = a - 1 - ((a - 1) // c + (a - 1) // d) + (a - 1) // t
y = b - (b // c + b // d) + b // t
print(y - x)


import math
(a, b, c, d) = map(int, input().split())
l = c * d // math.gcd(c, d)
u = b // c + b // d - b // l
l = (a - 1) // c + (a - 1) // d - (a - 1) // l
print(b - a + 1 - u + l)


import fractions
(a, b, c, d) = map(int, input().split())
x = c * d // fractions.gcd(c, d)
print(b - ~-a - (b // c - ~-a // c) - (b // d - ~-a // d) + b // x - ~-a // x)


import fractions
(A, B, C, D) = map(int, input().split())
lcm = C * D // fractions.gcd(C, D)
A -= 1
a = A // C + A // D - A // lcm
b = B // C + B // D - B // lcm
print(B - A - b + a)


from math import gcd
(a, b, c, d) = map(int, input().split())
lcm = c * d // gcd(c, d)
a -= 1
p = b // c - a // c
q = b // d - a // d
r = b // lcm - a // lcm
a += 1
print(b - a + 1 - (p + q - r))


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)

def f(x, c, d):
	res = x
	res -= x // c
	res -= x // d
	res += x // lcm(c, d)
	return res
(a, b, c, d) = map(int, input().split())
ans = f(b, d, c) - f(a - 1, c, d)
print(ans)


import fractions
(A, B, C, D) = map(int, input().split(' '))

def calc1(X):
	return X - X // C - X // D + X // int(C * D / fractions.gcd(C, D))
print(calc1(B) - calc1(A - 1))


(a, b, c, d) = map(int, input().split())
from math import gcd
lcm = c * d // gcd(c, d)
b_div_c = b // c
a_div_c = (a - 1) // c
b_div_d = b // d
a_div_d = (a - 1) // d
b_div_cd = b // lcm
a_div_cd = (a - 1) // lcm
ans1 = b_div_c - a_div_c
ans2 = b_div_d - a_div_d
ans3 = b_div_cd - a_div_cd
ans = b - (a - 1) - ans1 - ans2 + ans3
print(ans)


import fractions
(a, b, c, d) = map(int, input().split())

def ans(b):
	return b - b // c - b // d + b // (c * d // fractions.gcd(c, d))
print(ans(b) - ans(a - 1))


import fractions
(a, b, c, d) = map(int, input().split())
g = fractions.gcd(c, d)
l = c * d // g
print(b - b // c - b // d + b // l - (a - 1 - (a - 1) // c - (a - 1) // d + (a - 1) // l))


import sys
import math
sys.setrecursionlimit(10 ** 8)
ini = lambda : int(sys.stdin.readline())
inl = lambda : [int(x) for x in sys.stdin.readline().split()]
ins = lambda : sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print('\x1b[33m', *a, '\x1b[0m', **dict(file=sys.stderr, **kw))

def solve():
	(a, b, c, d) = inl()
	cd = c * d // math.gcd(c, d)
	r1 = b // c + b // d - b // cd
	r2 = (a - 1) // c + (a - 1) // d - (a - 1) // cd
	ans = b - r1 - (a - 1 - r2)
	return ans
print(solve())


import fractions
(A, B, C, D) = map(int, input().split())
K = B - A + 1
E = C * D // fractions.gcd(C, D)
c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
e = B // E - (A - 1) // E
print(K - c - d + e)


import math
(A, B, C, D) = list(map(int, input().split()))
E = int(C * D / math.gcd(C, D))
print(B - A + 1 - (B // C - (A - 1) // C) - (B // D - (A - 1) // D) + B // E - (A - 1) // E)


from math import gcd
(A, B, C, D) = map(int, input().split())
lcm = C * D // gcd(C, D)
a = (A - 1) // C + (A - 1) // D - (A - 1) // lcm
b = B // C + B // D - B // lcm
print(B - A + 1 - b + a)


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(a, b, c, d) = map(int, input().split())
e = lcm(c, d)
print(b - a + 1 - (b // c - (a - 1) // c) - (b // d - (a - 1) // d) + (b // e - (a - 1) // e))


import math
(a, b, c, d) = map(int, input().split())
mc = b // c - (a - 1) // c
md = b // d - (a - 1) // d
cd = c * d // math.gcd(c, d)
mcd = b // cd - (a - 1) // cd
print(b - a + 1 - mc - md + mcd)


from fractions import gcd
(a, b, c, d) = map(int, input().split())

def fac(n):
	i = n - (n // c + n // d - n // (c * d // gcd(c, d)))
	return i
print(fac(b) - fac(a - 1))


import fractions
(A, B, C, D) = map(int, input().split())
g = fractions.gcd(C, D)
l = C * D // g
print(B - A + 1 - B // C - B // D + B // l + (A - 1) // C + (A - 1) // D - (A - 1) // l)


import sys
import math
(A, B, C, D) = map(int, sys.stdin.readline().split())
lcm = C * D // math.gcd(C, D)
bc = B // C
bd = B // D
bg = B // lcm
ac = (A - 1) // C
ad = (A - 1) // D
ag = (A - 1) // lcm
print(B - A + 1 - (bc - ac) - (bd - ad) + (bg - ag))


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)

def main():
	(A, B, C, D) = list(map(int, input().split()))
	tmpC = (A - 1) // C
	tmpC2 = B // C
	numC = tmpC2 - tmpC
	tmpD = (A - 1) // D
	tmpD2 = B // D
	numD = tmpD2 - tmpD
	if C == D:
		ans = B - A + 1 - numC
	else:
		tmpCD = (A - 1) // lcm(C, D)
		tmpCD2 = B // lcm(C, D)
		numCD = tmpCD2 - tmpCD
		ans = B - A + 1 - numC - numD + numCD
	return ans
print(main())


from fractions import gcd
(a, b, c, d) = map(int, input().split())
f = b // c - (a - 1) // c
g = b // d - (a - 1) // d
k = c * d // gcd(c, d)
h = f + g - (b // k - (a - 1) // k)
print(b + 1 - a - h)


from fractions import gcd
(A, B, C, D) = map(int, input().split())
A -= 1
g = gcd(C, D)
d = C // g * (D // g) * g
ansA = A - (A // C + A // D - A // d)
ansB = B - (B // C + B // D - B // d)
print(ansB - ansA)


import math
INT = lambda : int(input())
INTM = lambda : map(int, input().split())
STRM = lambda : map(str, input().split())
STR = lambda : str(input())
LIST = lambda : list(map(int, input().split()))
LISTS = lambda : list(map(str, input().split()))

def do():
	(a, b, c, d) = INTM()
	lcm = c * d // math.gcd(c, d)
	a -= 1
	ct_a = a - a // c - a // d + a // lcm
	ct_b = b - b // c - b // d + b // lcm
	print(ct_b - ct_a)

def __starting_point():
	do()
__starting_point()


import fractions
(a, b, c, d) = list(map(int, input().split()))
a -= 1
lcm = int(c * d / fractions.gcd(c, d))
print(b - b // c - b // d + b // lcm - (a - a // c - a // d + a // lcm))


(a, b, c, d) = map(int, input().split())
from fractions import gcd
e = c * d // gcd(c, d)
x = b // c - (a - 1) // c + b // d - (a - 1) // d - (b // e - (a - 1) // e)
print(b - a + 1 - x)


from fractions import gcd
(A, B, C, D) = map(int, input().split())
E = C * D // gcd(C, D)
a = B // C - (A - 1) // C
b = B // D - (A - 1) // D
c = B // E - (A - 1) // E
print(B - A + 1 - a - b + c)


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)
(A, B, C, D) = map(int, input().split())

def div(x):
	num = x
	num -= x // C
	num -= x // D
	num += x // lcm(C, D)
	return num
print(div(B) - div(A - 1))


import math
(a, b, c, d) = map(int, input().split())
cyaku = b // c
dyaku = b // d
cds = b // (c * d // math.gcd(c, d))
x = b - (cyaku + dyaku - cds)
cyaku2 = (a - 1) // c
dyaku2 = (a - 1) // d
cds2 = (a - 1) // (c * d // math.gcd(c, d))
y = a - 1 - (cyaku2 + dyaku2 - cds2)
print(x - y)


from fractions import gcd
(a, b, c, d) = map(int, input().split())
l = c * d // gcd(c, d)
f = lambda n: n - (n // c + n // d - n // l)
print(f(b) - f(a - 1))


from fractions import gcd
(a, b, c, d) = map(int, input().split())
ok1 = b - a + 1
a -= 1
ng1 = b // c - a // c
ng2 = b // d - a // d
lcm = c * d // gcd(c, d)
ok2 = b // lcm - a // lcm
print(ok1 - ng1 - ng2 + ok2)


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(A, B, C, D) = map(int, input().split())
CD_lcm = lcm(C, D)
B_s = B - B // C - B // D + B // CD_lcm
A_s = A - 1 - (A - 1) // C - (A - 1) // D + (A - 1) // CD_lcm
print(B_s - A_s)


import fractions
(A, B, C, D) = map(int, input().split())
m = C * D // fractions.gcd(C, D)
ans = B - A + 1 - (B // C - (A - 1) // C + (B // D - (A - 1) // D) - (B // m - (A - 1) // m))
print(ans)


from fractions import gcd
(A, B, C, D) = map(int, input().split())

def F(N):
	x = N // C
	x += N // D
	lcm = C * D // gcd(C, D)
	x -= N // lcm
	return N - x
print(F(B) - F(A - 1))


from fractions import gcd
(a, b, c, d) = map(int, input().split())
c0 = b // c - (a - 1) // c
d0 = b // d - (a - 1) // d
cd0 = b // (c * d // gcd(c, d)) - (a - 1) // (c * d // gcd(c, d))
print(b - a + 1 - c0 - d0 + cd0)


import math
(A, B, C, D) = list(map(int, input().split()))
nad = B // D
nac = B // C
nid = (A - 1) // D
nic = (A - 1) // C
nacd = B // (D * C // math.gcd(D, C))
nicd = (A - 1) // (D * C // math.gcd(D, C))
ans = B - A + 1 - (nad - nid) - (nac - nic) + (nacd - nicd)
print(int(ans))


from fractions import gcd
(a, b, c, d) = map(int, input().split())
l = c * d // gcd(c, d)
e = (a - 1) // c
f = b // c
(g, h) = ((a - 1) // d, b // d)
(i, j) = ((a - 1) // l, b // l)
print(b - a + 1 - f + e - h + g + j - i)


import math
(A, B, C, D) = map(int, input().split())
calc = lambda x: B // x - (A - 1) // x
print(B - A + 1 - calc(C) - calc(D) + calc(C * D // math.gcd(C, D)))


import fractions
(a, b, c, d) = map(int, input().split())
t = b // d - (a - 1) // d
s = b // c - (a - 1) // c
p = fractions.gcd(d, c)
q = c * d // p
u = b // q - (a - 1) // q
print(b - a + 1 - (t + s - u))


from math import *
(A, B, C, D) = map(int, input().split())
mc = B // C - (A - 1) // C
md = B // D - (A - 1) // D
cd = C * D // gcd(C, D)
mcd = B // cd - (A - 1) // cd
print(B - A + 1 - mc - md + mcd)


import math
(A, B, C, D) = map(int, input().split())
LCD = C * D // math.gcd(C, D)
ans = B - (B // C + B // D - B // LCD) - (A - 1 - ((A - 1) // C + (A - 1) // D - (A - 1) // LCD))
print(int(ans))


import fractions as fr
(a, b, c, d) = map(int, input().split())

def f(x):
	return b // x - (a - 1) // x
print(b - a + 1 - f(c) - f(d) + f(c * d // fr.gcd(c, d)))


import math
(a, b, c, d) = map(int, input().split())
l = c * d // math.gcd(c, d)
all = b - a + 1
can_c = b // c - (a - 1) // c
can_d = b // d - (a - 1) // d
can_cd = b // l - (a - 1) // l
ans = all - can_c - can_d + can_cd
print(ans)


import os
import sys

def gcd(a, b):
	while a % b != 0:
		r = a % b
		a = b
		b = r
	return b

def main():
	(a, b, c, d) = list(map(int, input().split()))
	if (a / c).is_integer():
		c_cnt = b // c - a // c + 1
	else:
		c_cnt = b // c - a // c
	if (a / d).is_integer():
		d_cnt = b // d - a // d + 1
	else:
		d_cnt = b // d - a // d
	lcm = c * d // gcd(max(c, d), min(c, d))
	if (a / lcm).is_integer():
		lcm_cnt = b // lcm - a // lcm + 1
	else:
		lcm_cnt = b // lcm - a // lcm
	print(b - a + 1 - c_cnt - d_cnt + lcm_cnt)

def __starting_point():
	main()
__starting_point()


def main():
	import fractions
	(a, b, c, d) = [int(i) for i in input().split()]
	d_c = b // c - (a - 1) // c
	d_d = b // d - (a - 1) // d
	cd = c * d // fractions.gcd(c, d)
	d_cd = b // cd - (a - 1) // cd
	print(b - a + 1 - (d_c + d_d - d_cd))

def __starting_point():
	main()
__starting_point()


import math
(a, b, c, d) = list(map(int, input().split()))

def calc(x):
	nonlocal c, d
	l = c // math.gcd(c, d) * d
	return x - x // c - x // d + x // l
print(calc(b) - calc(a - 1))


(a, b, c, d) = list(map(int, input().split()))
import math

def yakusuu(n):
	return n // c + n // d - n // (c * d // math.gcd(c, d))
print(int(b - a + 1 - (yakusuu(b) - yakusuu(a - 1))))


import fractions
(A, B, C, D) = map(int, input().split())
c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
lm = C * D // fractions.gcd(C, D)
cd = B // lm - (A - 1) // lm
print(B - A + 1 - c - d + cd)


import math
(a, b, c, d) = map(int, input().split())
e = c * d // math.gcd(c, d)
wa = 0
c_seki = b // c - (a - 1) // c
d_seki = b // d - (a - 1) // d
e_seki = b // e - (a - 1) // e
wa = c_seki + d_seki - e_seki
print(b - a + 1 - wa)


from fractions import gcd
(A, B, C, D) = map(int, input().split())
lcm = C // gcd(C, D) * D
bb = B - B // C - B // D + B // lcm
aa = A - 1 - (A - 1) // C - (A - 1) // D + (A - 1) // lcm
print(bb - aa)


import fractions
(a, b, c, d) = map(int, input().split())
a -= 1
y = int(c * d / fractions.gcd(c, d))
p = a - (a // c + a // d - a // y)
q = b - (b // c + b // d - b // y)
print(int(q - p))


(a, b, c, d) = map(int, input().split())
from math import gcd

def cnt(n):
	lcm = c // gcd(c, d) * d
	return n - n // c - n // d + n // lcm
print(cnt(b) - cnt(a - 1))


import fractions
(a, b, c, d) = map(int, input().split())
cdlcm = c * d // fractions.gcd(c, d)
a -= 1
tc = b // c - a // c
td = b // d - a // d
tlcm = b // cdlcm - a // cdlcm
print(b - a - tc - td + tlcm)


from fractions import gcd

def func(n):
	return n - (n // C + n // D - n // (C * D // gcd(C, D)))
(A, B, C, D) = map(int, input().split())
print(func(B) - func(A - 1))


from fractions import gcd
(aa, b, c, d) = map(int, input().split())
a = aa - 1
o = c * d // gcd(c, d)
print(b - a - (b // c + b // d - b // o - a // c - a // d + a // o))


import math
(a, b, c, d) = map(int, input().split())
split_b = b - b // c - b // d + b // (c * d // math.gcd(c, d))
split_a = a - 1 - (a - 1) // c - (a - 1) // d + (a - 1) // (c * d // math.gcd(c, d))
print(split_b - split_a)


(a, b, c, d) = map(int, input().split())
a -= 1
e = c
f = d
while f > 0:
	(e, f) = (f, e % f)
x = b // c + b // d - b // (c * d // e)
y = a // c + a // d - a // (c * d // e)
print(b - x - a + y)


import fractions
(A, B, C, D) = map(int, input().split())
CD = C * D // fractions.gcd(C, D)
A = A - 1
num_B = B - B // C - B // D + B // CD
num_A = A - A // C - A // D + A // CD
print(int(num_B - num_A))


from math import gcd as g
(a, b, c, d) = map(int, input().split())
count = 0
print(b - a + 1 - (b // c - (a - 1) // c + b // d - (a - 1) // d - b // (c * d // g(c, d)) + (a - 1) // (c * d // g(c, d))))


def gcd(x, y):
	if y == 0:
		return x
	r = x % y
	return gcd(y, r)
(A, B, C, D) = [int(x) for x in input().split()]
if C > D:
	E = C * D // gcd(C, D)
else:
	E = C * D // gcd(D, C)
divC = B // C - (A - 1) // C
divD = B // D - (A - 1) // D
divE = B // E - (A - 1) // E
div = divC + divD - divE
ans = B - A + 1 - div
print(ans)


from math import gcd

def lcm(a, b):
	return a // gcd(a, b) * b

def getExclude(x, c, d):
	return x - x // c - x // d + x // lcm(c, d)
(a, b, c, d) = (int(x) for x in input().split())
ans = getExclude(b, c, d) - getExclude(a - 1, c, d)
print(ans)


import fractions
(a, b, c, d) = map(int, input().split())
m = c * d // fractions.gcd(c, d)
ans = b - b // c - b // d + b // m
b = a - 1
ans -= b - b // c - b // d + b // m
print(ans)


import math
(a, b, c, d) = map(int, input().split())
m = b // c + -a // c
n = b // d + -a // d
g = c * d // math.gcd(c, d)
p = b // g + -a // g
print(b - a + 1 - (m + 1 + n + 1 - (p + 1)))


import fractions
(A, B, C, D) = map(int, input().split())
x = C * D // fractions.gcd(C, D)
p = B // C - (A - 1) // C
q = B // D - (A - 1) // D
r = B // x - (A - 1) // x
print(int(B - (A - 1) - (p + q - r)))


(a, b, c, d) = map(int, input().split())
import fractions as f
cc = b // c - (a - 1) // c
dd = b // d - (a - 1) // d
gcd = f.gcd(c, d)
lcm = c * d // gcd
cd = b // lcm - (a - 1) // lcm
print(b - a + 1 - (cc + dd - cd))


(A, B, C, D) = map(int, input().split())
A -= 1
import math
g = C * D // math.gcd(C, D)
a = A // C + A // D - A // g
b = B // C + B // D - B // g
ans = B - A - b + a
print(ans)


(a, b, c, d) = map(int, input().split())
import math
min = int(c * d / math.gcd(c, d))
x = b - b // c - b // d + b // min
y = a - 1 - (a - 1) // c - (a - 1) // d + (a - 1) // min
print(int(x - y))


import fractions
(a, b, c, d) = map(int, input().split())
f = fractions.gcd(c, d)
e = c * d // f
x1 = b - b // c - b // d + b // e
a -= 1
x2 = a - a // c - a // d + a // e
print(x1 - x2)


import math
(a, b, c, d) = list(map(int, input().split()))
a -= 1
e = c * d // math.gcd(c, d)
print(b - b // c - b // d + b // e - (a - a // c - a // d + a // e))


(a, b, c, d) = map(int, input().split())
dcou = b // d - (a - 1) // d
ccou = b // c - (a - 1) // c
from math import gcd
dc = d * c // gcd(d, c)
dccou = b // dc - (a - 1) // dc
ans = b - a - dcou - ccou + dccou + 1
print(int(ans))


import math
(a, b, c, d) = map(int, input().split())
x = (a - 1) // c
y = b // c
z = (a - 1) // d
w = b // d
e = c * d // math.gcd(c, d)
p = (a - 1) // e
q = b // e
print(b - a + 1 - y + x - w + z + q - p)


from math import gcd
(a, b, c, d) = map(int, input().split())
e = c * d // gcd(c, d)
cntC = b // c - (a - 1) // c
cntD = b // d - (a - 1) // d
cntE = b // e - (a - 1) // e
print(b - a - (cntC + cntD - cntE) + 1)


from fractions import gcd
(a, b, c, d) = map(int, input().split())
lcm = int(c * d / gcd(c, d))
A = a - 1
x1 = A - A // c - A // d + A // lcm
x2 = b - b // c - b // d + b // lcm
print(x2 - x1)


import math
(a, b, c, d) = map(int, input().split())
(n, m, k) = (0, 0, 0)
if a % c == 0:
	n = b // c - a // c + 1
else:
	n = b // c - a // c
if a % d == 0:
	m = b // d - a // d + 1
else:
	m = b // d - a // d
e = c * d // math.gcd(c, d)
if a % e == 0:
	k = b // e - a // e + 1
else:
	k = b // e - a // e
k = n + m - k
print(b - a - k + 1)


import fractions
(a, b, c, d) = map(int, input().split())
e = c * d // fractions.gcd(c, d)
a = a - 1
n1 = a - (a // c + a // d - a // e)
n2 = b - (b // c + b // d - b // e)
print(n2 - n1)


(a, b, c, d) = map(int, input().split())

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)
e = c * d // gcd(c, d)
print(b - a - b // c + (a + c - 1) // c - b // d + (a + d - 1) // d + b // e - (a + e - 1) // e)


import math

def solve(a, c, d):
	x = a + 1
	x -= a // c + 1
	x -= a // d + 1
	x += a // (c * d // math.gcd(c, d)) + 1
	return x
(A, B, C, D) = list(map(int, input().split()))
print(solve(B, C, D) - solve(A - 1, C, D))


from fractions import gcd
(A, B, C, D) = map(int, input().split())
A -= 1
S = B - B // C - B // D + B // (C * D // gcd(C, D))
T = A - A // C - A // D + A // (C * D // gcd(C, D))
print(S - T)


import fractions
(a, b, c, d) = map(int, input().split())
f1 = fractions.gcd(c, d)
f2 = c * d // f1
a = a - 1
aa = a - a // c - a // d + a // f2
bb = b - b // c - b // d + b // f2
print(bb - aa)


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)
(A, B, C, D) = map(int, input().split())
c_multiples = B // C - (A - 1) // C
d_multiples = B // D - (A - 1) // D
lcm_multiples = B // lcm(C, D) - (A - 1) // lcm(C, D)
print(B - A + 1 - c_multiples - d_multiples + lcm_multiples)


import math
(A, B, C, D) = map(int, input().split(' '))
CD_least_common = C * D // math.gcd(C, D)
except_rst = B // C - (A - 1) // C + B // D - (A - 1) // D - (B // CD_least_common - (A - 1) // CD_least_common)
rst = B - (A - 1) - except_rst
print(rst)


from fractions import gcd
(a, b, c, d) = map(int, input().split())
a = a - 1
l = int(c * d / gcd(d, c))
cn = b // c - a // c
dn = b // d - a // d
ln = b // l - a // l
ans = b - a - cn - dn + ln
print(ans)


from functools import reduce
from math import gcd

def lcm(n, c, d):
	g = gcd(c, d)
	l = c // g * d
	return n - n // c - n // d + n // l
(a, b, c, d) = map(int, input().split())
print(lcm(b, c, d) - lcm(a - 1, c, d))


import sys

def gcd(a, b):
	if a < b:
		(a, b) = (b, a)
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)
input = sys.stdin.readline
(a, b, c, d) = list(map(int, input().split()))
div_ac = (a - 1) // c
div_bc = b // c
div_ad = (a - 1) // d
div_bd = b // d
lcm_cd = lcm(c, d)
div_acd = (a - 1) // lcm_cd
div_bcd = b // lcm_cd
a_1_result = div_ac + div_ad - div_acd
b_result = div_bc + div_bd - div_bcd
print(b + 1 - a - (b_result - a_1_result))


import fractions
(a, b, c, d) = map(int, input().split())
l = c * d // fractions.gcd(c, d)
B = b // c + b // d - b // l
A = (a - 1) // c + (a - 1) // d - (a - 1) // l
print(b - B - (a - 1 - A))


import fractions
(A, B, C, D) = map(int, input().split())
lcm = C * D // fractions.gcd(C, D)
comp = B // C - (A - 1) // C + B // D - (A - 1) // D - (B // lcm - (A - 1) // lcm)
print(B - A + 1 - comp)


import math
(a, b, c, d) = map(int, input().split())
n = b - a + 1

def lcm(x, y):
	return x * y // math.gcd(x, y)
x = lcm(c, d)
cc = b // c - (a - 1) // c
dc = b // d - (a - 1) // d
xc = b // x - (a - 1) // x
print(n - cc - dc + xc)


from math import gcd

def count(x, c, d):
	lcm = c // gcd(c, d) * d
	return x - x // c - x // d + x // lcm
(a, b, c, d) = list(map(int, input().split()))
print(count(b, c, d) - count(a - 1, c, d))


from fractions import gcd
(a, b, c, d) = map(int, input().split())
count = 0
x = b - a + 1

def cal(x):
	return x // c + x // d - x // (c * d // gcd(c, d))
print(b - a + 1 - (cal(b) - cal(a - 1)))


import fractions
(a, b, c, d) = map(int, input().split())
C = b // c - (a - 1) // c
D = b // d - (a - 1) // d
e = c * d // fractions.gcd(c, d)
E = b // e - (a - 1) // e
ans = b - a - C - D + E + 1
print(ans)


from fractions import gcd
(a, b, c, d) = map(int, input().split())
nc = b // c - (a - 1) // c
nd = b // d - (a - 1) // d
cd = c * d // gcd(c, d)
ncd = b // cd - (a - 1) // cd
print(b - a + 1 - nc - nd + ncd)


(A, B, C, D) = map(int, input().split())
A -= 1

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
lCD = C * D // gcd(C, D)
print(B - B // C - B // D + B // lCD - (A - A // C - A // D + A // lCD))


(a, b, c, d) = map(int, input().split())
(x, y) = (c, d)
while y != 0:
	(x, y) = (y, x % y)
lcm = c * d // x
c_upper = b // c
c_lower = -(-a // c)
d_upper = b // d
d_lower = -(-a // d)
lcm_upper = b // lcm
lcm_lower = -(-a // lcm)
print(b - a + 1 - (c_upper - c_lower + 1) - (d_upper - d_lower + 1) + (lcm_upper - lcm_lower + 1))


(a, b, c, d) = map(int, input().split())
a -= 1

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)
aa = b // c - a // c
bb = b // d - a // d
cc = b // lcm(c, d) - a // lcm(c, d)
print(b - a - (aa + bb - cc))


import math
(a, b, c, d) = map(int, input().split())
e = c * d // math.gcd(c, d)
if a % c == 0:
	p = b // c - a // c + 1
else:
	p = b // c - a // c
if a % d == 0:
	q = b // d - a // d + 1
else:
	q = b // d - a // d
if a % e == 0:
	r = b // e - a // e + 1
else:
	r = b // e - a // e
print(b - a + 1 - (p + q - r))


import math
(a, b, c, d) = map(int, input().split())
x = c * d // math.gcd(c, d)
ans = b - b // c - b // d + b // x - (a - 1 - (a - 1) // c - (a - 1) // d + (a - 1) // x)
print(ans)


import fractions
(a, b, c, d) = map(int, input().split())
g = fractions.gcd(c, d)
e = c // g * d
ans = b // c - (a - 1) // c + (b // d - (a - 1) // d) - (b // e - (a - 1) // e)
print(b - a + 1 - ans)


import math

def lcm(x, y):
	return x * y // math.gcd(x, y)

def main():
	(a, b, c, d) = list(map(int, input().split()))
	a -= 1
	cd = lcm(c, d)
	ans = b - a - (b // c - a // c) - (b // d - a // d) + (b // cd - a // cd)
	print(ans)

def __starting_point():
	main()
__starting_point()


import math

def lcm(a, b):
	return a * b // math.gcd(a, b)
(a, b, c, d) = map(int, input().split())
l = lcm(c, d)
ans = b - a + 1 - (b // c + b // d - b // l - ((a - 1) // c + (a - 1) // d - (a - 1) // l))
print(ans)


(a, b, c, d) = map(int, input().split())
from math import gcd
ans = 0

def main(n):
	e = n // c + n // d - n // (c * d // gcd(c, d))
	return n - e
print(main(b) - main(a - 1))


import math
(A, B, C, D) = map(int, input().split())
c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
lcm = C * D // math.gcd(C, D)
cd = B // lcm - (A - 1) // lcm
x = c + d - cd
print(B - A + 1 - x)


import fractions
(a, b, c, d) = map(int, input().split())
a = a - 1
x = c * d // fractions.gcd(c, d)
n = b // c - a // c
m = b // d - a // d
o = b // x - a // x
ans = b - a - n - m + o
print(ans)


import fractions
(a, b, c, d) = map(int, input().split())
f = c * d // fractions.gcd(c, d)
num = b // c + b // d - b // f
num -= (a - 1) // c + (a - 1) // d - (a - 1) // f
print(b - a + 1 - num)


(a, b, c, d) = map(int, input().split())
from fractions import gcd
lcm = c * d // gcd(c, d)
n = b // c - (a - 1) // c
n += b // d - (a - 1) // d
print(b - a + 1 - n + (b // lcm - (a - 1) // lcm))


from math import gcd
(A, B, C, D) = map(int, input().split())
E = C * D // gcd(C, D)
ans = B - A + 1 - (B // C - (A - 1) // C + B // D - (A - 1) // D - B // E + (A - 1) // E)
print(ans)


(A, B, C, D) = map(int, input().split())
ans = B - A + 1
from fractions import gcd
lcm = int(C * D / gcd(C, D))
print(ans - (B // C - (A - 1) // C + (B // D - (A - 1) // D) - (B // lcm - (A - 1) // lcm)))


import fractions
(a, b, c, d) = map(int, input().split())
k = fractions.gcd(c, d)
lcm = c * d // k
x = b // c - (a - 1) // c
y = b // d - (a - 1) // d
z = b // lcm - (a - 1) // lcm
print(b - a + 1 - (x + y - z))


import math

def cnt(a: int, k: int):
	return a // k

def num(a, c, d):
	m_a_c = cnt(a, c)
	m_a_d = cnt(a, d)
	m_a_cd = cnt(a, c * d // math.gcd(c, d))
	return a - m_a_c - m_a_d + m_a_cd
(a, b, c, d) = list(map(int, input().split()))
print(num(b, c, d) - num(a - 1, c, d))


(a, b, c, d) = map(int, input().split())
a -= 1
from fractions import gcd
g = c * d // gcd(c, d)
cc = b // c - a // c
dc = b // d - a // d
gc = b // g - a // g
print(b - a - cc - dc + gc)


(A, B, C, D) = map(int, input().split())

def gcd(x, y):
	if x == 0:
		return y
	if y == 0:
		return x
	if x >= y:
		return gcd(y, x % y)
	if x < y:
		return gcd(y % x, x)
l = C * D // gcd(C, D)
p = B // C - (A - 1) // C
q = B // D - (A - 1) // D
r = B // l - (A - 1) // l
s = p + q - r
print(B - A + 1 - s)


def lcm(a):
	from math import gcd
	x = a[0]
	for i in range(1, len(a)):
		x = x * a[i] // gcd(x, a[i])
	return x
(A, B, C, D) = map(int, input().split())
num = []
num.append(C)
num.append(D)
a = A - 1
x_B = B - B // C - B // D + B // lcm(num)
x_A = a - a // C - a // D + a // lcm(num)
ans = x_B - x_A
print(ans)


import math
(a, b, c, d) = map(int, input().split())
base = c * d // math.gcd(c, d)
c_num = b // c - (a - 1) // c
d_num = b // d - (a - 1) // d
base_num = b // base - (a - 1) // base
x = int(c_num + d_num - base_num)
print(b - a + 1 - x)


import math
(a, b, c, d) = map(int, input().split())
x = b // c - (a - 1) // c
y = b // d - (a - 1) // d
g = math.gcd(c, d)
l = c * d // g
z = b // l - (a - 1) // l
print(b - a + 1 - x - y + z)


from fractions import gcd
(a, b, c, d) = map(int, input().split())
cd = gcd(c, d)
D = b // d - (a - 1) // d
C = b // c - (a - 1) // c
CD = b // (c * d // cd) - (a - 1) // (c * d // cd)
print(b - a + 1 - (D + C - CD))


import math
(A, B, C, D) = map(int, input().split(' '))
num_C_baisu = B // C - (A - 1) // C
num_D_baisu = B // D - (A - 1) // D
CD = int(C * D / math.gcd(C, D))
num_CD_baisu = B // CD - (A - 1) // CD
baisu_num = num_C_baisu + num_D_baisu - num_CD_baisu
print(B - A + 1 - baisu_num)


(A, B, C, D) = map(int, input().split())

def f(x):
	return B // x - (A - 1) // x
import math
E = int(C * D / math.gcd(C, D))
print(B - A + 1 - f(C) - f(D) + f(E))


import math
(A, B, C, D) = list(map(int, input().split()))
(divAC, modAC) = divmod(A, C)
(divBC, modBC) = divmod(B, C)
ans = 0
if modAC == 0:
	ans += divBC - divAC + 1
else:
	ans += divBC - divAC
(divAD, modAD) = divmod(A, D)
(divBD, modBD) = divmod(B, D)
if modAD == 0:
	ans += divBD - divAD + 1
else:
	ans += divBD - divAD
(divACD, modACD) = divmod(A, int(C * D / math.gcd(C, D)))
(divBCD, modBCD) = divmod(B, int(C * D / math.gcd(C, D)))
if modACD == 0:
	ans -= divBCD - divACD + 1
else:
	ans -= divBCD - divACD
print(B - A + 1 - ans)


import fractions
(A, B, C, D) = map(int, input().split())
l = C * D // fractions.gcd(C, D)
x = A - 1 - (A - 1) // C - (A - 1) // D + (A - 1) // l
y = B - B // C - B // D + B // l
print(y - x)


import fractions
(a, b, c, d) = map(int, input().split())

def calc(n):
	return n - n // c - n // d + n // (c * d // fractions.gcd(c, d))
print(calc(b) - calc(a - 1))