Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.

Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one.

Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?


-----Input-----

The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$).


-----Output-----

Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible.

If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one.


-----Examples-----
Input
6 10

Output
2
Input
21 31

Output
9
Input
5 10

Output
0


-----Note-----

In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Number theory

Mathematics

Complete search


Solutions:
import sys
import random
input = sys.stdin.readline

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

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

def main():
	(a, b) = map(int, input().split())
	k = min(a, b)
	t = max(a, b)
	a = k
	b = t
	k = float('inf')
	ind = 0
	i = 1
	divs = []
	while i * i <= b - a:
		if (b - a) % i == 0:
			divs.append(i)
			divs.append((b - a) // i)
		i += 1
	divs.sort()
	for i in divs:
		if a % i == b % i:
			if a % i == 0:
				if k > lcm(a, b):
					k = lcm(a, b)
					ind = 0
			elif k > lcm(i - a % i + a, i - b % i + b):
				k = lcm(i - a % i + a, i - b % i + b)
				ind = i - a % i
	print(ind)
	return
main()


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

def lcm(a, b):
	return a * b / gcd(a, b)
ans = 0
ans_lcm = 100000000000000000000
a = 0
b = 0

def update(d):
	global ans, a, b, ans_lcm
	k = d - a % d
	lcm_ = lcm(a + k, b + k)
	if lcm_ < ans_lcm or (lcm_ == ans_lcm and d < ans):
		ans_lcm = lcm_
		ans = k

def start():
	global a, b, ans, ans_lcm
	(a, b) = map(int, input().split())
	if a < b:
		(a, b) = (b, a)
	ans_lcm = lcm(a, b)
	i = 1
	while i * i <= a - b:
		if (a - b) % i == 0:
			update(i)
			update((a - b) // i)
		i += 1
	print(ans)
start()


(a, b) = map(int, input().split())
if a == b:
	print(0)
else:
	(a, b) = (max(a, b), min(a, b))
	x = a - b
	if a % x == 0 and b % x == 0:
		print(0)
	elif a < 2 * b:
		print(x - b % x)
	else:
		lis = [i for i in range(1, int(x ** 0.5) + 1) if x % i == 0]
		for i in lis[::-1]:
			lis.append(x // i)
		import bisect
		y = bisect.bisect(lis, x // b)
		print(x // lis[y - 1] - b)


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

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)

def primeFactor(N):
	i = 2
	ret = {}
	n = N
	if n < 0:
		ret[-1] = 1
		n = -n
	if n == 0:
		ret[0] = 1
	d = 2
	sq = int(n ** (1 / 2))
	while i <= sq:
		k = 0
		while n % i == 0:
			n //= i
			k += 1
			ret[i] = k
		if k > 0:
			sq = int(n ** (1 / 2))
		if i == 2:
			i = 3
		elif i == 3:
			i = 5
		elif d == 2:
			i += 2
			d = 4
		else:
			i += 4
			d = 2
	if n > 1:
		ret[n] = 1
	return ret

def divisors(N):
	pf = primeFactor(N)
	ret = [1]
	for p in pf:
		ret_prev = ret
		ret = []
		for i in range(pf[p] + 1):
			for r in ret_prev:
				ret.append(r * p ** i)
	return sorted(ret)
if A == B:
	print(0)
else:
	mi = 10 ** 100
	ans = -1
	D = divisors(abs(B - A))
	for d in D:
		k = -A % d
		L = lcm(A + k, B + k)
		if mi > L or (mi == L and ans > k):
			mi = L
			ans = k
	print(ans)


import sys
import math
(a, b) = [int(s) for s in input().split()]
if not a % b or not b % a:
	print(0)
	sys.exit(0)
else:
	divs = []
	for i in range(1, math.ceil(abs(a - b) ** (1 / 2)) + 1):
		if abs(a - b) % i == 0:
			divs.append(i)
			divs.append(abs(a - b) // i)
	min_ = 1e+20
	nod = -1
	for i in range(len(divs)):
		k = math.ceil(max(a, b) / divs[i]) * divs[i] - max(a, b)
		if (a + k) * (b + k) / divs[i] < min_:
			min_ = (a + k) * (b + k) / divs[i]
			nod = divs[i]
			k_min = k
	print(k_min)


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

def gcd(arg1, arg2):
	if arg1 == 0:
		return arg2
	if arg2 == 0:
		return arg1
	if arg2 > arg1:
		(arg2, arg1) = (arg1, arg2)
	return gcd(arg1 % arg2, arg2)
if a > b:
	(a, b) = (b, a)
k = b - a
array = []
i = 1
while i ** 2 <= k:
	if k % i == 0:
		array.append(i)
		array.append(k // i)
	i += 1
gcdis = a * b / gcd(a, b)
z = 0
for d in array:
	z1 = a - a % d + d
	z2 = b - b % d + d
	if z1 * z2 // gcd(z1, z2) <= gcdis:
		if z1 * z2 // gcd(z1, z2) == gcdis:
			z = min(d - a % d, z)
		else:
			gcdis = z1 * z2 // gcd(z1, z2)
			z = d - a % d
print(z)


import sys, math
input = sys.stdin.readline

def divisors(n):
	i = 1
	l = []
	while i <= int(n ** 0.5):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(n // i)
				l.append(i)
		i = i + 1
	return l

def getLCM(a, b):
	lcm = a * b // math.gcd(a, b)
	return lcm
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
if a == b:
	print(0)
	exit()
l = divisors(b - a)
ans = 10 ** 18
k = 10 ** 18
for x in l:
	e1 = a + (x - a % x)
	e2 = b + (x - b % x)
	if a % x == 0:
		e1 = a
		e2 = b
	lcm = getLCM(e1, e2)
	if lcm < ans:
		ans = lcm
		if a % x == 0:
			k = 0
			continue
		k = x - a % x
	elif lcm == ans:
		if a % x == 0:
			k = 0
			continue
		if x - a % x < k:
			k = x - a % x
print(k)


import sys
input = sys.stdin.readline
import math

def divisors(a):
	ret = []
	b = int(math.sqrt(a)) + 1
	for i in range(1, b + 1):
		if a % i == 0:
			ret.append(i)
			if i != a // i:
				ret.append(a // i)
	return sorted(ret)
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
if a == b:
	print(0)
elif a <= b // 2:
	d = b - a
	dv = divisors(d)
	for x in dv:
		if x >= a:
			break
	ans = x - a
	print(ans)
else:
	d = b - a
	y = a // d
	if a % d > 0:
		y += 1
	x = d * y
	ans = x - a
	print(ans)


from math import *

def lcm(a, b):
	return a * b // gcd(a, b)
(a, b) = map(int, input().split())
(a, b) = (min(a, b), max(a, b))
div = []
for i in range(1, floor(sqrt(b - a)) + 1):
	if (b - a) % i == 0:
		div.append(i)
		if (b - a) // i != i:
			div.append((b - a) // i)
LCM = []
for q in div:
	k = q - a % q
	LCM.append((k, lcm(a + k, b + k)))
LCM.append((0, lcm(a, b)))
LCM.sort(key=lambda x: x[1])
ans = LCM[0][1]
key = []
for i in LCM:
	if i[1] == ans:
		key.append(i[0])
print(min(key))


import math

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	l = x * y // gcd(x, y)
	return l
(a, b) = map(int, input().split(' '))
if a > b:
	(a, b) = (b, a)
n = b - a
if n == 0:
	print(0)
	exit()
divisors = [1, n]
k = []
l = []
for i in range(2, int(math.sqrt(n) + 1)):
	if n % i == 0:
		if i * i != n:
			divisors.append(i)
			divisors.append(int(n / i))
		else:
			divisors.append(i)
for i in range(len(divisors)):
	t = -b % divisors[i]
	k.append(t)
	l.append(lcm(a + t, b + t))
mini = min(l)
if lcm(a, b) <= mini:
	print(0)
else:
	idx = l.index(mini)
	print(k[idx])


from math import *
(a, b) = [int(k) for k in input().split()]
if a == b:
	print(0)
	quit()
if a > b:
	(a, b) = (b, a)
z = b - a
if z < a:
	z *= ceil(a / z)
rz = int(sqrt(z)) + 1
n = 0
rn = 1
en = z
rbn = z
while n < rz:
	n += 1
	if z % n == 0:
		rn = n
		if rn >= a:
			break
		if z // rn >= a:
			rbn = z // rn
		else:
			break
k1 = rn - a
k2 = rbn - a
if k1 < 0:
	k1 = k2
if k2 < 0:
	k2 = k1
print(min(k1, k2))


from math import gcd
(a, b) = map(int, input().split())
if b < a:
	(a, b) = (b, a)
if a == b:
	print(0)
	exit(0)
c = b - a
i = 1
ans = a * b // gcd(a, b)

def get(x):
	A = (a + x - 1) // x * x
	B = A - a + b
	return (A * B // gcd(A, B), A)
r = 0
while i * i <= c:
	if c % i == 0:
		(A, AA) = get(i)
		(B, BB) = get(c // i)
		if A < ans:
			ans = A
			r = AA - a
		if B < ans:
			ans = B
			r = BB - a
		if A == ans:
			r = min(r, AA - a)
		if B == ans:
			r = min(r, BB - a)
	i += 1
print(r)


import math

def factorization(x):
	a = [1]
	for i in range(2, int(math.sqrt(x)) + 1):
		if x % i == 0:
			a.append(i)
	for i in range(len(a) - 1, -1, -1):
		a.append(x // a[i])
	return a
(a, b) = map(int, input().split())
if a == b:
	print(0)
	exit()
if a == 1 or b == 1:
	print(0)
	exit()
if a > b:
	(a, b) = (b, a)
razn = b - a
if a > razn:
	c = a // razn + 1
	print((c * razn - a) % razn)
else:
	s = factorization(razn)
	l = 0
	r = len(s)
	while r - l > 1:
		m = (r + l) // 2
		if s[m] < a:
			l = m
		else:
			r = m
	print(s[r] - a)


import sys
input = sys.stdin.buffer.readline
from math import gcd

def lcm(x, y):
	return x * y // gcd(x, y)
for _ in range(1):
	(a, b) = map(int, input().split())
	if b - a == 0:
		print(0)
		continue
	if a > b:
		(a, b) = (b, a)
	i = 1
	ans = lcm(a, b)
	store = 0
	while i * i <= b - a:
		if (b - a) % i == 0:
			k = i - a % i
			l = lcm(a + k, b + k)
			if ans > l:
				store = k
				ans = l
			i = (b - a) // i
			k = i - a % i
			l = lcm(a + k, b + k)
			if ans > l:
				store = k
				ans = l
			i = (b - a) // i
		i += 1
	print(store)


from math import floor, log2, sqrt, gcd
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
elif a == b:
	print(0)
	quit()
c = b - a
d = [1]
for i in range(2, int(floor(sqrt(c)) + 1)):
	if c % i == 0:
		if i * i == c:
			d.append(i)
		else:
			d.append(i)
			d.append(c // i)
d = list(filter(lambda x: x <= b // a, d))
k = int(10000000000.0)
for i in d:
	kk = (b - a * (i + 1)) // i
	if kk < 0:
		kk %= b - a
	if kk < k:
		k = kk
if a * b // gcd(a, b) < (a + k) * (b + k) // gcd(a + k, b + k):
	k = 0
print(k)


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from time import time
from itertools import permutations as per
from heapq import heapify, heappush, heappop, heappushpop
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\r\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
(a, b) = R()
if a == b:
	exit(print(0))
l = lcm(a, b)
ans = inf
p = abs(a - b)
for i in range(1, int(sqrt(abs(a - b))) + 1):
	if not p % i:
		k = lcm(a + i - a % i, b + i - a % i)
		if k <= l:
			if k == l:
				ans = min(ans, i - a % i)
			else:
				ans = i - a % i
			l = k
		k = lcm(a + p // i - a % (p // i), b + p // i - a % (p // i))
		if k <= l:
			if k == l:
				ans = min(ans, p // i - a % (p // i))
			else:
				ans = p // i - a % (p // i)
			l = k
k = lcm(a + p - a % p, b + p - a % p)
if k < l:
	ans = p
print(ans if ans != inf else 0)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
import math as mt
import inspect, re

def varname(p):
	for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
		m = re.search('\\bvarname\\s*\\(\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*\\)', line)
		if m:
			return m.group(1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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

def lcm(a, b):
	return a * b / gcd(a, b)
mod = int(1000000000.0) + 7

def power(k, n):
	if n == 0:
		return 1
	if n % 2:
		return power(k, n - 1) * k % mod
	t = power(k, n // 2)
	return t * t % mod

def totalPrimeFactors(n):
	count = 0
	if n % 2 == 0:
		count += 1
		while n % 2 == 0:
			n //= 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			count += 1
			while n % i == 0:
				n //= i
		i += 2
	if n > 2:
		count += 1
	return count

def main():
	(a, b) = map(int, input().split())
	i = 1
	if a < b:
		(a, b) = (b, a)
	curr = lcm(a, b)
	ans = 0
	while i * i <= a - b:
		if (a - b) % i == 0:
			k = (i - b % i) % i
			t = lcm(a + k, b + k)
			if t < curr:
				ans = k
				curr = t
			i = (a - b) // i
			k = (i - b % i) % i
			t = lcm(a + k, b + k)
			if t < curr:
				ans = k
				curr = t
			i = (a - b) // i
		i += 1
	print(ans)
	return
main()


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

def gcd(x, y):
	if x == 0:
		return y
	if y == 0:
		return x
	if y > x:
		(y, x) = (x, y)
	return gcd(x % y, y)
if a > b:
	(a, b) = (b, a)
k = b - a
dzielniki = []
i = 1
while i ** 2 <= k:
	if k % i == 0:
		dzielniki.append(i)
		dzielniki.append(k // i)
	i += 1
gcdd = a * b / gcd(a, b)
wynik = 0
for d in dzielniki:
	aa = a - a % d + d
	bb = b - b % d + d
	if aa * bb // gcd(aa, bb) <= gcdd:
		if aa * bb // gcd(aa, bb) == gcdd:
			wynik = min(d - a % d, wynik)
		else:
			gcdd = aa * bb // gcd(aa, bb)
			wynik = d - a % d
print(wynik)


from fractions import gcd
import math
lcm = lambda a, b: a // gcd(a, b) * b
(a, b) = map(int, input().split())
if a == b:
	print(0)
	exit(0)
d = abs(b - a)
divs = {d}
for i in range(2, int(math.sqrt(d))):
	if not d % i:
		divs |= {i, d // i}
sol = [(lcm(a, b), 0)]
for d in divs:
	k = d - a % d if a % d else 0
	sol.append((lcm(a + k, b + k), k))
print(min(sol)[1])


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
from math import *
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN():
	return list(numIN())

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	st = en = be = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
			st = be
			en = i
		if meh < 0:
			meh = 0
			be = i + 1
	return (msf, st, en)

def res(ans, t):
	print('Case #{}: {}'.format(t, ans))

def divi(n):
	l = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l += [i, n // i]
	l.sort()
	return l

def lcm(a, b):
	return a * b // gcd(a, b)
(a, b) = numIN()
if a == b:
	print(0)
	exit(0)
l = divi(abs(a - b))
lc = lcm(a, b)
k = 0
for i in l:
	p1 = a % i
	p2 = i - a % i
	ca = a + p1
	cb = b + p1
	clc = lcm(ca, cb)
	if clc < lc:
		lc = clc
		k = p1
	ca = a + p2
	cb = b + p2
	clc = lcm(ca, cb)
	if clc < lc:
		lc = clc
		k = p2
print(k)


import math
import cmath
(a, b) = (0, 0)
ans = 1 << 50
mi = 1 << 50

def solve(vgcd):
	global mi, ans
	r = (vgcd - a % vgcd) % vgcd
	scm = int((a + r) * (b + r) / int(math.gcd(a + r, b + r)))
	if scm < mi | (scm == mi & r < ans):
		ans = r
		mi = scm

def main():
	global a, b
	(a, b) = (int(x) for x in input().split())
	dif = abs(a - b)
	if dif == 0:
		print(0)
		return None
	i = 1
	while i * i <= dif:
		solve(i)
		solve(dif // i)
		i += 1
	print(ans)
main()


import math
(n, m) = map(int, input().split())

def lcm(i):
	return (n + i) * (m + i) // math.gcd(n + i, m + i)
t = abs(n - m)
p = set([])
i = 1
while i ** 2 <= t:
	if t % i == 0:
		p.add(i)
		p.add(t // i)
	i += 1
p = sorted(list(p))
g = 10 ** 20
key = 0
for i in p:
	index = (i - n % i) % i
	l = lcm(index)
	if l < g:
		g = l
		key = index
print(key)


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

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

def get_factor(a):
	arr = []
	for i in range(1, int(math.sqrt(a)) + 1):
		if a % i == 0:
			arr.append(i)
			arr.append(a // i)
	return arr
c = abs(b - a)
tmp = float('inf')
ans = -1
for i in get_factor(c):
	r = a % i
	k = (i - r) % i
	lcm = (a + k) * (b + k) // gcd_(a + k, b + k)
	if tmp > lcm:
		tmp = lcm
		ans = k
if a == b:
	ans = 0
print(ans)


from math import ceil
from math import gcd
(x, y) = map(int, input().split())
(a, b) = (min(x, y), max(x, y))
x = b - a
if x == 0:
	print(0)
elif x < a:
	c = ceil(a / x)
	ans = c * x
	print(ans - a)
else:
	l = []
	for i in range(1, int(pow(x, 0.5)) + 1):
		if x % i == 0:
			l.append(i)
			if i != x // i:
				l.append(x // i)
	l.sort()
	c = 1
	for i in l:
		c = i
		if i >= a:
			break
	print(c - a)


from math import gcd

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

def Solution(a, b):
	if a == b:
		return 0
	if a < b:
		(a, b) = (b, a)
	diff = a - b
	divs = []
	for d in range(1, diff + 2):
		if d * d > diff:
			break
		if diff % d == 0:
			divs.append(d)
			if d * d <= diff:
				divs.append(diff // d)
	min_lcm = lcm(a, b)
	ans = 0
	for i in divs:
		if a % i != b % i:
			continue
		(l, k) = (0, 0)
		if a % i == 0:
			l = lcm(a + k, b + k)
			k = 0
		else:
			l = lcm(i - a % i + a, i - b % i + b)
			k = i - a % i
		if l < min_lcm:
			ans = k
			min_lcm = l
	return ans
(a, b) = map(int, input().split())
print(Solution(a, b))


from math import gcd
l = list(map(int, input().split()))
l.sort()
diff = l[1] - l[0]
d = []
for i in range(1, int(diff ** 0.5) + 1):
	if diff % i == 0:
		d.append(i)
		if diff // i != i:
			d.append(diff // i)
d.sort()
ans = [[l[0] * l[1] // gcd(l[0], l[1]), 0]]
if len(set(l)) != 1:
	for i in d:
		x = l[0]
		y = l[1]
		k = 0
		if x % i - i != 0:
			k = i - x % i
		x += k
		y += k
		ans.append([x * y // gcd(x, y), k])
else:
	ans.append([0, 0])
ans.sort()
print(ans[0][1])


from fractions import gcd
(a, b) = map(int, input().split())
d = max(a, b) - min(a, b)
c = [float('inf'), 1]
e = [1]
for i in range(1, int(d ** 0.5) + 1):
	if i ** 2 == d:
		e.append(i)
	elif d % i == 0:
		e.append(i)
		e.append(d // i)
for i in e:
	h = i * ((a - 1) // i + 1) - a
	if c[0] > (a + h) * (b + h) // gcd(a + h, b + h):
		c[0] = (a + h) * (b + h) // gcd(a + h, b + h)
		c[1] = h
print(c[1])


(a, b) = list(map(int, input().split()))
(a, b) = (min(a, b), max(a, b))
if b < 2 * a:
	if a == b:
		print(0)
		exit()
	print(-a % (b - a))
	exit()
s = [1]
q = b - a
for i in range(2, int((b - a) ** (1 / 2) + 2)):
	while q % i == 0:
		t = [j * i for j in s]
		for aa in t:
			s.append(aa)
		s = list(set(s))
		q = q // i
if q != 1:
	t = [j * q for j in s]
	for aa in t:
		s.append(aa)
	s = list(set(s))
s.sort()
for i in s:
	if i >= a:
		print(i - a)
		exit()


import math
(a, b) = map(int, input().split())
(a, b) = (min(a, b), max(a, b))
ans = a * b
k = 0
for d in range(1, int(math.sqrt(b - a)) + 1):
	if (b - a) % d == 0:
		for d in [d, (b - a) // d]:
			t = 0
			if a % d != 0:
				t = d - a % d
			if (a + t) * (b + t) // d < ans:
				k = t
				ans = (a + k) * (b + k) // d
print(k)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	(a, b) = map(int, input().split())
	(mx, mn) = (max(a, b), min(a, b))
	if math.gcd(a, b) == mn:
		print(0)
	else:
		v = abs(a - b)
		if v < mn:
			print(math.ceil(mn / v) * v - mn)
		else:
			ans = 10 ** 9 + 1
			for i in range(1, int(math.sqrt(v)) + 1):
				if v % i == 0:
					if i > mn:
						ans = min(i - mn, ans)
					if v // i > mn:
						ans = min(v // i - mn, ans)
			print(ans)
main()


import math

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return factors

def GCD(x, y):
	while y != 0:
		z = x % y
		x = y
		y = z
	return x

def LCM(x, y):
	return x * y // GCD(x, y)

def factors(n):
	super = [1]
	for i in prime_factors(n):
		super.extend([j * i for j in super])
	return list(sorted(set(super)))
(n, m) = map(int, input().split())
_factor = factors(abs(n - m))
ans = 0
minimum = math.inf
for i in _factor:
	k = (i - n % i) % i
	if LCM(n + k, m + k) < minimum:
		ans = k
		minimum = LCM(n + k, m + k)
print(ans)


import math

def lcm(a, b, k):
	return (a + k) * (b + k) // math.gcd(a + k, b + k)
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
best = a * b / math.gcd(a, b)
argbest = 0
for d in range(1, int(math.sqrt(b - a)) + 1):
	if (b - a) % d == 0:
		d1 = d
		d2 = (b - a) // d
		k1 = d1 - a % d1
		k2 = d2 - a % d2
		if lcm(a, b, k1) < best:
			best = lcm(a, b, k1)
			argbest = k1
		if lcm(a, b, k2) < best:
			best = lcm(a, b, k2)
			argbest = k2
print(argbest)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
(a, b) = mi()
(a, b) = (min(a, b), max(a, b))
X = b - a

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)

def prime_factor(x):
	pf = []
	for i in range(1, int(x ** 0.5) + 1):
		if x % i == 0:
			pf.append(i)
			pf.append(x // i)
	pf = sorted(list(set(pf)))
	return pf
if a == b:
	print(0)
else:
	D = prime_factor(X)
	minimum = 10 ** 100
	for d in D:
		k = -a % d
		L = lcm(a + k, b + k)
		if minimum > L:
			minimum = L
			ans = k
	print(ans)


from sys import stdin
from math import sqrt

def parser():
	return map(int, stdin.readline().split())

def calculate_k_mcm(divisor):
	temp_k = divisor - a % divisor
	temp_mcm = (a + temp_k) * (b + temp_k) / divisor
	return (temp_k, temp_mcm)
(a, b) = parser()
if b < a:
	temp = a
	a = b
	b = temp
b_a = b - a
b_a_sqrt = int(sqrt(b_a))
optimal_k = 0
mcm = a * b
for i in range(1, b_a_sqrt + 1):
	if b_a % i == 0:
		if a % i == 0:
			temp_mcm = a * b / i
			if temp_mcm < mcm:
				mcm = temp_mcm
				optimal_k = 0
		else:
			(temp_k, temp_mcm) = calculate_k_mcm(i)
			if temp_mcm < mcm:
				mcm = temp_mcm
				optimal_k = temp_k
		i_couple = int(b_a / i)
		if a % i_couple == 0:
			temp_mcm = a * b / i_couple
			if temp_mcm < mcm:
				mcm = temp_mcm
				optimal_k = 0
		else:
			(temp_k, temp_mcm) = calculate_k_mcm(int(b_a / i))
			if temp_mcm < mcm:
				mcm = temp_mcm
				optimal_k = temp_k
print(optimal_k)


from math import gcd, sqrt

def printDivisors(n):
	i = 1
	c = []
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				c.append(i)
			else:
				c.append(i)
				c.append(n // i)
		i = i + 1
	return c
(n, m) = map(int, input().split())
p = gcd(n, m)
lc = n * m // p
z = abs(n - m)
c = printDivisors(z)
h = []
for i in range(len(c)):
	o = n // c[i]
	u = m // c[i]
	if n % c[i] == 0 and m % c[i] == 0:
		y = n * m // c[i]
		if lc > y:
			lc = y
			h.append([lc, 0])
	else:
		r = c[i] * (o + 1)
		q = c[i] * (u + 1)
		y = r * q // c[i]
		if y < lc:
			lc = y
			h.append([lc, abs(r - n)])
h = sorted(h)
try:
	print(h[0][1])
except:
	print(0)


from math import sqrt
INF = 1e+18

def gcd(x, y):
	if y == 0:
		return x
	else:
		return gcd(y, x % y)

def solution():
	(atmp, btmp) = map(int, input().strip().split())
	a = max(atmp, btmp)
	b = min(atmp, btmp)
	k = 0
	delta = a - b
	min_lcm = INF
	if delta == 0:
		print(0)
		exit(0)
	range_gcd = int(sqrt(delta)) + 1
	for i in range(1, range_gcd):
		if delta % i == 0:
			(k, min_lcm) = getk(a, b, i, k, min_lcm)
			(k, min_lcm) = getk(a, b, delta / i, k, min_lcm)
	print('{0:.0f}'.format(k))

def getk(a, b, i, k, min_lcm):
	current_k = 0
	if b % i > 0:
		current_k = (b // i + 1) * i - b
	lcm = (a + current_k) * (b + current_k) / i
	if lcm < min_lcm:
		min_lcm = lcm
		k = current_k
	return (k, min_lcm)
solution()


from math import gcd
l = list(map(int, input().split()))
l.sort()
a = l[0]
b = l[1]
num = b - a

def lcm(i):
	return (a + i) * (b + i) // gcd(a + i, b + i)
i = 1
factors = []
while i * i <= num:
	if not num % i:
		factors.append(i)
		factors.append(num // i)
	i += 1
factors.sort()
min1 = lcm(0)
ans = 0
for factor in factors:
	k = factor - a % factor
	min2 = lcm(k)
	if min2 < min1:
		min1 = min2
		ans = k
print(ans)


import math

def divisors(n):
	div = []
	d = 1
	while d ** 2 <= n:
		if not n % d:
			div.append(d)
			if d ** 2 != n:
				div.append(n // d)
		d += 1
	return div

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

def C():
	(a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	if not b % a:
		print(0)
		return
	divs = divisors(b - a)
	M = b * a
	k = 0
	for d in divs:
		aux_k = d * math.ceil(b / d) - b
		if lcm(a + aux_k, b + aux_k) < M:
			M = lcm(a + aux_k, b + aux_k)
			k = aux_k
	print(k)
C()


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

def lcm(a, b):
	return a * b / gcd(a, b)
arr = input()
(A, B) = [int(x) for x in arr.split(' ')]
d = abs(A - B)
divisiors = []
for i in range(1, 100000):
	if i * i > d:
		break
	elif d % i == 0:
		divisiors.append(i)
		divisiors.append(d // i)
mini = -1
res = 0
for div in divisiors:
	new_A = A // div
	if A % div != 0:
		new_A += 1
	new_A = new_A * div
	new_B = B // div
	if B % div != 0:
		new_B += 1
	new_B = new_B * div
	L = lcm(new_A, new_B)
	if mini == -1:
		mini = L
		res = new_A - A
	elif L <= mini:
		if L == mini:
			res = min(res, new_A - A)
		else:
			res = new_A - A
		mini = L
print(res)


(a, b) = list(map(int, input().split()))
(a, b) = (min(a, b), max(a, b))
if b % a == 0:
	print(0)
	return
x = b - a
dels = set()
for i in range(1, int(x ** 0.5) + 1):
	if x % i == 0:
		dels.add(i)
		dels.add(x // i)
dels = list(dels)
j = 10 ** 20
for i in dels:
	if i >= a:
		j = min(j, i - a)
print(min((x % a - a) % x, j))


import math
(a, b) = map(int, input().split())
if b > a:
	(a, b) = (b, a)
if not a % b:
	print(0)
	exit()
else:
	sub = a - b
	min_ = 1e+20
	k_min = 0
	for i in range(1, int(math.sqrt(sub)) + 1):
		if sub % i == 0:
			div1 = i
			div2 = sub // i
			k1 = div1 - b % div1
			k2 = div2 - b % div2
			if b % div1 == 0:
				k1 = 0
			if b % div2 == 0:
				k2 = 0
			lcm1 = (a + k1) * (b + k1) / div1
			lcm2 = (a + k2) * (b + k2) / div2
			if lcm1 < min_ or (lcm1 == min_ and k1 < k_min):
				min_ = lcm1
				k_min = k1
			if lcm2 < min_ or (lcm2 == min_ and k2 < k_min):
				min_ = lcm2
				k_min = k2
	print(k_min)


def gcd(p, q):
	while p:
		(p, q) = (q % p, p)
	return q
I = lambda : map(int, input().split())
(a, b) = I()
(a, b) = (min(a, b), max(a, b))
if b % a == 0:
	exit(print('0'))
k1 = 0
t = b - a
ans = 100
minn = 10 ** 31
k = 100
for i in range(1, int(t ** 0.5) + 1):
	if t % i == 0:
		g = i
		k = (g - a % g) % g
		p = (a + k) * (b + k) // gcd(a + k, b + k)
		if p < minn:
			minn = p
			ans = k
		elif p == minn:
			ans = min(ans, k)
		g = t // i
		k = (g - a % g) % g
		p = (a + k) * (b + k) // gcd(a + k, b + k)
		if p < minn:
			minn = p
			ans = k
		elif p == minn:
			ans = min(ans, k)
print(ans)


import math

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
nums = list(map(int, input().split()))
nums.sort()
x = nums[0]
y = nums[1]
k = 0
curr = lcm(x, y)
mod = y % x
if mod == 0:
	print('0')
else:
	rem = y - x
	factors = []
	i = 1
	while i * i <= rem:
		if rem % i == 0:
			factors.append(i)
			factors.append(rem // i)
		i += 1
	factors.sort()
	for i in factors:
		temp = i - x % i
		if lcm(x + temp, y + temp) < curr:
			k = temp
			curr = lcm(x + k, y + k)
	print(k)


import math
a = list(map(int, input().split()))
a.sort()
diff = a[1] - a[0]
yakusuu = []
for i in range(1, int(diff ** (1 / 2)) + 1):
	if diff % i == 0:
		yakusuu.append(i)
		yakusuu.append(diff // i)
yakusuu.sort()
ans = [[a[0] * a[1], 0]]
for i in yakusuu:
	x = a[0]
	y = a[1]
	k = 0
	if x % i != 0:
		k = i - x % i
	x += k
	y += k
	anstemp = x * y // math.gcd(x, y)
	ans.append([anstemp, k])
ans.sort()
print(ans[0][1])


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

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

def make_divisors(n):
	divisors = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			divisors.append(i)
			if i != n // i:
				divisors.append(n // i)
	return divisors
(a, b) = (b, a) if a > b else (a, b)
c = b - a
if c == 0:
	print(0)
	return
pc = make_divisors(c)
ctr = a * b // gcd(a, b)
ans = 0
for p in pc:
	ak = -(-a // p) * p
	k = ak - a
	bk = -(-b // p) * p
	lc = ak * bk // gcd(ak, bk)
	if ctr > lc:
		ctr = lc
		ans = k
print(ans)


import sys
input = sys.stdin.readline
(a, b) = map(int, input().split())
if a == b:
	print(0)
	sys.exit()
if a > b:
	(a, b) = (b, a)
x = b - a
import math
xr = math.ceil(math.sqrt(x))

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

def lcm(x, y):
	return x * y // gcd(x, y)
LIST = []
for i in range(1, xr + 1):
	if x % i == 0:
		LIST.append(i)
		LIST.append(x // i)
ANS = []
for l in LIST:
	y = math.ceil(a / l) * l - a
	ANS.append([lcm(a + y, b + y), y])
print(min(ANS, key=lambda x: x[0])[1])


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)
(a, b) = map(int, input().split())
(a, b) = (min(a, b), max(a, b))
f = []
r = int((b - a) ** 0.5)
for i in range(1, r + 1):
	if i * i == b - a:
		f.append(i)
	elif (b - a) % i == 0:
		f.append(i)
		f.append((b - a) // i)
mhcf = 100000000000000000000000
ans = 0
for i in f:
	if a % i == 0:
		k = 0
		if (a + k) * (b + k) // math.gcd(a + k, b + k) < mhcf:
			ans = k
			mhcf = (a + k) * (b + k) // math.gcd(a + k, b + k)
	else:
		k = i - a % i
		if (a + k) * (b + k) // math.gcd(a + k, b + k) < mhcf:
			ans = k
			mhcf = (a + k) * (b + k) // math.gcd(a + k, b + k)
print(ans)


import sys
input = lambda : sys.stdin.readline().strip()
from math import gcd

def solve():
	(a, b) = map(int, input().split())
	(a, b) = sorted([a, b])
	if a == b:
		return 0
	d = b - a
	best = (a * b // gcd(a, b), 0)
	i = 1
	while i * i <= d:
		if d % i == 0:
			for g in (i, d // i):
				k = (g - a % g) % g
				x = (a + k) * (b + k) // g
				best = min(best, (x, k))
		i += 1
	return best[1]
print(solve())


import os
from io import BytesIO, IOBase
import sys
from math import gcd

def main():
	(a, b) = sorted(map(int, input().split()))
	(f, i, z, l_c_m, ans) = ([], 1, b - a, a * b // gcd(a, b), 0)
	while i * i <= z:
		if not z % i:
			if z // i != i:
				f.append(z // i)
			f.append(i)
		i += 1
	for i in f:
		k = i - a % i
		l_c_m_t = (a + k) * (b + k) // i
		if l_c_m_t < l_c_m:
			(l_c_m, ans) = (l_c_m_t, k)
		elif l_c_m_t == l_c_m:
			ans = min(k, ans)
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from math import gcd, ceil

def factors(n):
	a = set()
	while n % 2 == 0:
		a.add(2)
		n //= 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			a.add(i)
			n //= i
	if n > 2:
		a.add(n)
	return sorted(a)

def divisors(n):
	i = 1
	a = set()
	while i <= n ** 0.5:
		if n % i == 0:
			if n // i == i:
				a.add(i)
			else:
				a.add(i)
				a.add(n // i)
		i += 1
	return sorted(a)

def LCM(x, y):
	return x * y // gcd(x, y)

def solve():
	(a, b) = map(int, input().split())
	if a < b:
		(a, b) = (b, a)
	if a % b == 0:
		print(0)
		return
	lcm = a * b // gcd(a, b)
	x = a - b
	k = ceil(a / x) * x - a
	K = k
	lcm = LCM(a + k, b + k)
	fact = divisors(x)
	for x in fact:
		k = ceil(b / x) * x - b
		l = LCM(a + k, b + k)
		if l < lcm or (l == lcm and k < K):
			lcm = l
			K = k
	print(K)
solve()


import math
import sys

def getfactors(num):
	l = []
	for i in range(1, int(math.sqrt(num)) + 1):
		if num % i == 0:
			l.append(i)
			l.append(num // i)
	return l
(x, y) = map(int, sys.stdin.readline().split())
(a, b) = (min(x, y), max(x, y))
hcf = math.gcd(a, b)
d = b - a
cur = a * b / hcf
k = 0
fact = getfactors(d)
n = len(fact)
for i in range(n):
	if a % fact[i] == 0:
		newk = 0
		newlcm = (a + newk) * (b + newk) / math.gcd(a + newk, b + newk)
		if newlcm < cur:
			cur = newlcm
			k = newk
	else:
		newk = fact[i] - a % fact[i]
		newlcm = (a + newk) * (b + newk) / math.gcd(a + newk, b + newk)
		if newlcm < cur:
			cur = newlcm
			k = newk
ans = k
print(ans)


from math import *

def lcm(a, b):
	return a * b // gcd(a, b)
(a, b) = map(int, input().split())
(a, b) = (min(a, b), max(a, b))
diff = b - a
if b % a == 0:
	print(0)
else:
	divs = set()
	for i in range(1, int(diff ** (1 / 2)) + 1):
		if diff % i == 0:
			divs.add(i)
			divs.add(diff // i)
	curLCM = None
	curK = None
	for can in divs:
		if b % can == 0:
			tLCM = lcm(b, a)
			tk = 0
		else:
			x = b // can
			x = (x + 1) * can
			tk = x - b
			tLCM = lcm(b + tk, a + tk)
		if curK == None:
			(curK, curLCM) = (tk, tLCM)
		elif curLCM > tLCM:
			(curK, curLCM) = (tk, tLCM)
		elif curLCM == tLCM:
			curK = min(tk, curK)
	print(curK)


(a, b) = map(int, input().split())
q = abs(a - b)
if q == 0:
	print(0)
else:
	dq = []
	dq2 = []
	for i in range(1, int(q ** 0.5) + 1):
		if not q % i:
			dq.append(i)
			if i != q // i:
				dq2 = [q // i] + dq2
	dq = dq + dq2
	dq = dq[::-1]
	if a < b:
		(a, b) = (b, a)

	def gcd(a, b):
		for ch in dq:
			if a % ch == 0 and b % ch == 0:
				return ch

	def agcd(a, b):
		return a * b // gcd(a, b)
	ks = [0]
	rezults = [[agcd(a, b), 0]]
	for ch in dq:
		k = ch - a % ch
		if k not in ks:
			ks.append(k)
			rezults.append([agcd(a + k, b + k), k])
	rezults.sort()
	print(rezults[0][1])


def make_divisors(n):
	divisors = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			divisors.append(i)
			if i != n // i:
				divisors.append(n // i)
	return divisors
import math
from functools import reduce

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

def lcm_list(numbers):
	return reduce(lcm_base, numbers, 1)
(a, b) = map(int, input().split())
if a == b:
	print(0)
	exit()
if a > b:
	(a, b) = (b, a)
d = make_divisors(b - a)
X = []
for g in d:
	k = (a + g - 1) // g * g - a
	a_ = a + k
	b_ = b + k
	l = lcm_base(a_, b_)
	X.append((l, k))
X.sort(key=lambda x: (x[0], x[1]))
print(X[0][1])


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

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

def main():
	(a, b) = [int(x) for x in input().split()]
	d = 1
	if a == b:
		print('%d' % 0)
		return
	if a > b:
		(a, b) = (b, a)
	ans = lcm(a, b)
	kans = 0
	while d * d <= b - a:
		if (b - a) % d == 0:
			q1 = (a + d - 1) // d
			k = q1 * d - a
			if lcm(a + k, b + k) < ans:
				ans = lcm(a + k, b + k)
				kans = k
			dp = d
			d = (b - a) // d
			q1 = (a + d - 1) // d
			k = q1 * d - a
			if lcm(a + k, b + k) < ans:
				ans = lcm(a + k, b + k)
				kans = k
			d = dp
		d += 1
	print('%d' % kans)
main()


from math import gcd

def div(n):
	ans = []
	d = 1
	while d * d < n:
		if n % d == 0:
			ans.append(d)
			ans.append(n // d)
		d += 1
	if d * d == n:
		ans.append(d)
	return ans
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
elif a == b:
	print(0)
	exit()
d = b - a
mn = float('+inf')
if 1:
	x = (a + d - 1) // d
	k = x * d - a
	a2 = a + k
	b2 = b + k
	nok1 = a * b // gcd(a, b)
	nok2 = a2 * b2 // gcd(a2, b2)
	ans = div(d)
	ans = sorted(ans)
	for d in ans:
		x = (a + d - 1) // d
		k = x * d - a
		a2 = a + k
		b2 = b + k
		nok2 = a2 * b2 // gcd(a2, b2)
		if nok2 < mn:
			mn = nok2
			ank = k
		nok2 = (a + d) * (b + d) // gcd(a + d, b + d)
		if nok2 < mn:
			mn = nok2
			ank = d
	print(ank)
else:
	...


(a, b) = map(int, input().split())
(a, b) = (min(a, b), max(a, b))

def divisors(n):
	div = []
	for i in range(1, int(n ** (1 / 2)) + 1):
		if n % i == 0:
			div.append(i)
			if i * i != n:
				div.append(n // i)
	return sorted(div)
div = divisors(b - a)
ans = res = 1e+50
for i in div:
	k = i - a % i if a % i else 0
	t = (a + k) * (b + k) // i
	if ans > t:
		ans = t
		res = k
print(res if b - a else 0)


from math import ceil

def factorise(req):
	factor = []
	for i in range(1, int(req ** 0.5) + 1):
		if req % i == 0:
			factor.append(i)
			if req // i != i:
				factor.append(req // i)
	return factor

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
(n, m) = [int(x) for x in input().split()]
a = min(n, m)
b = max(n, m)
ans = 0
req = b - a
factor = list(set(factorise(b - a)))
mn = 10 ** 30
for i in factor:
	aplusk = ceil(a / i) * i
	bplusk = ceil(b / i) * i
	lcm = aplusk * bplusk // gcd(aplusk, bplusk)
	if lcm < mn:
		ans = aplusk - a
		mn = lcm
print(ans)


import sys
from math import gcd

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		a = self.next_int()
		b = self.next_int()
		d = abs(a - b)
		div = []
		for i in range(1, d + 1):
			if d % i == 0:
				div.append(i)
				div.append(d // i)
			if i * i > d:
				break
		t = 0
		m = a * b // gcd(a, b)
		for x in div:
			r = a % x
			if r > 0:
				k = x - r
				tmp = (a + k) * (b + k) // gcd(a + k, b + k)
				if tmp < m:
					m = tmp
					t = k
		print(t)
Main().solve()


from sys import stdin
input = lambda : stdin.readline().strip()
lin = lambda : list(map(int, input().split()))
iin = lambda : int(input())
main = lambda : map(int, input().split())
from math import ceil, sqrt, factorial, log
from collections import deque
from bisect import bisect_left

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

def solve():
	(a, b) = map(int, input().split())
	(a, b) = (max(a, b), min(a, b))
	if a == b:
		print(0)
	else:
		z = []
		d = {}
		for i in range(1, int(sqrt(a - b) + 1)):
			if (a - b) % i == 0:
				z.append(i)
				z.append((a - b) // i)
		for i in z:
			x = (a + i - 1) // i * i - a
			t = (a + x) * (b + x) // i
			if t in d:
				d[t] = min(d[t], x)
			else:
				d[t] = x
		print(d[min(d)])
qwe = 1
for _ in range(qwe):
	solve()


def divisor(n):
	n2 = int(n ** 0.5 + 1)
	ret = set()
	for i in range(1, n2 + 1):
		if n % i == 0:
			ret.add(i)
			ret.add(n // i)
	ret = list(ret)
	ret.sort()
	return ret

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

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

def solve():
	inf = 1 << 60
	(a, b) = list(map(int, input().split()))
	if a > b:
		(a, b) = (b, a)
	if a == b:
		return 0
	ma = min(a, b)
	D = divisor(b - a)
	best_lcm = inf
	best_k = 0
	for d in D:
		r1 = a % d
		r2 = b % d
		if r1 == r2:
			k = 0
			if r1 != 0:
				k = d - r1
			lcm1 = lcm(a + k, b + k)
			if best_lcm > lcm1:
				best_lcm = lcm1
				best_k = k
			elif best_lcm == lcm1:
				best_k = min(best_k, k)
	return best_k
print(solve())


(a, b) = map(int, input().split())
if a == b:
	print(0)
	exit(0)
if a < b:
	(a, b) = (b, a)
dif = a - b
i = 1
div = []
while i * i <= dif:
	if dif % i == 0:
		div.append(i)
		if i * i != dif:
			div.append(dif // i)
	i += 1
div.sort()
ans = 1 << 64
v1 = 0
from math import gcd
for d in div:
	if b % d != a % d:
		continue
	if b % d == 0:
		temp = a * b // gcd(a, b)
		if temp < ans:
			ans = temp
			v1 = 0
	else:
		ta = a + d - a % d
		tb = b + d - b % d
		temp = ta * tb // gcd(ta, tb)
		if temp < ans:
			ans = temp
			v1 = d - b % d
print(v1)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
(a1, b1) = map(int, input().split())
a = min(a1, b1)
b = max(a1, b1)
diff = b - a
div = []
if a == b:
	print(0)
	exit()
for i in range(1, int(diff ** 0.5) + 1):
	if diff % i == 0:
		zz = diff // i
		div.append(i)
		if i != zz:
			div.append(zz)
ans = 100000000000000000
cc = -1
for i in div:
	k = (i - a % i) % i
	xx = (a + k) * (b + k)
	yy = gcd(a + k, b + k)
	temp = xx // yy
	if ans > temp:
		ans = temp
		cc = k
print(cc)


__version__ = '1.1'
__date__ = '2019-04-24'
import sys

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

def solve(a, b):
	(a, b) = (min(a, b), max(a, b))
	if b % a == 0:
		return 0
	diff = b - a
	(k, lcm) = (0, a * b)
	factors = [1, diff]
	fact = 2
	while fact * fact <= diff:
		if diff % fact == 0:
			factors.append(fact)
			if fact != diff // fact:
				factors.append(diff // fact)
		fact += 1
	for f in sorted(factors):
		step = a % f
		if step > 0:
			step = f - step
		this_lcm = (a + step) * (b + step) // gcd(a + step, b + step)
		if this_lcm < lcm:
			lcm = this_lcm
			k = step
	return k

def main(argv=None):
	(a, b) = map(int, input().split())
	print(solve(a, b))
	return 0
STATUS = main()
sys.exit(STATUS)


(a, b) = [int(x) for x in input().split()]
lcm = K = 10 ** 100

def gcd(a, b):
	if b > a:
		(a, b) = (b, a)
	if b == 0:
		return a
	return gcd(a % b, b)
delit = []
if a == b:
	print(0)
else:
	if a > b:
		(a, b) = (b, a)
	for i in range(1, int((b - a) ** 0.5) + 1):
		if (b - a) % i == 0:
			delit.append(i)
			delit.append((b - a) // i)
	for item in delit:
		if a % item == 0:
			k = 0
		else:
			k = item - a % item
		if (a + k) * (b + k) // gcd(b - a, a + k) < lcm:
			lcm = (a + k) * (b + k) // gcd(b - a, a + k)
			K = k
		elif (a + k) * (b + k) // gcd(b - a, a + k) == lcm and k < K:
			K = k
	print(K)


import math

def GCD(a, b) -> int:
	while b:
		x = a % b
		a = b
		b = x
	return a
(a, b) = list(map(int, input().split()))
c = abs(b - a)
maxl = a * b / GCD(a, b)
k = 0
if a % b == 0:
	print(0)
	exit()
if b % a == 0:
	print(0)
	exit()
x = int(math.sqrt(c))
for i in range(1, x + 1):
	if c % i == 0:
		d = int(a / i + 1) * i
		e = int(b / i + 1) * i
		if maxl > d * e / GCD(d, e):
			maxl = d * e / GCD(d, e)
			k = d - a
		d = int(a / (c / i) + 1) * (c / i)
		e = int(b / (c / i) + 1) * (c / i)
		if maxl > d * e / GCD(d, e):
			maxl = d * e / GCD(d, e)
			k = d - a
		if maxl == d * e / GCD(d, e) and k > d - a:
			k = d - a
print(int(k))


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
t = b - a
divisors = set()
for i in range(1, int(t ** 0.5) + 1):
	if t % i == 0:
		divisors.add(i)
		divisors.add(t // i)
lcm = 10 ** 18
ans = 0
for x in divisors:
	k = x - a % x
	if k == x:
		k = 0
	temp = (a + k) * (b + k) // x
	if lcm > temp:
		lcm = temp
		ans = k
	elif lcm == temp:
		ans = min(ans, k)
print(ans)


from sys import stdin, stdout
import math
import sys

def primeFactors(n):
	carr = []
	narr = []
	if n % 2 == 0:
		carr.append(0)
		narr.append(2)
		while n % 2 == 0:
			carr[0] += 1
			n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		if n % i == 0:
			narr.append(i)
			carr.append(0)
			while n % i == 0:
				carr[-1] += 1
				n = n / i
	if n > 2:
		narr.append(n)
		carr.append(1)
	return (narr, carr)
(a, b) = stdin.readline().strip().split(' ')
(a, b) = (int(a), int(b))
if b < a:
	(a, b) = (b, a)
diff = b - a
if math.gcd(a, b) == diff or a == b:
	stdout.write('0')
else:
	pfarr = []
	(narr, carr) = primeFactors(diff)

	def rec(ctr, vtn):
		if ctr == len(narr):
			pfarr.append(int(vtn))
		else:
			rec(ctr + 1, vtn)
			for i in range(1, carr[ctr] + 1):
				rec(ctr + 1, vtn * narr[ctr] ** i)
	rec(0, 1)
	mink = -1
	minlcm = 1 << 64
	for i in pfarr:
		t2 = -1
		if a % i == 0:
			t2 = a // i
		else:
			t2 = a // i + 1
		k = t2 * i - a
		t_lcm = (a + k) * (b + k) // i
		if t_lcm < minlcm:
			minlcm = t_lcm
			mink = k
	stdout.write(str(mink))


def solve(a, b):
	if a > b:
		(a, b) = (b, a)
	d = b - a
	if d <= 1:
		return 0
	if d > a:
		if d % a == 0:
			return 0
		i = a
		q = []
		while i * i <= d:
			if d % i == 0:
				return i - a
			i += 1
		if a * a > d:
			i = d // a
		while i >= 0:
			if d % i == 0:
				return d // i - a
			i -= 1
	elif d < a:
		if a % d == 0:
			return 0
		else:
			return d - a % d
	return 0
(a, b) = map(int, input().split())
print(solve(a, b))


import math

def div(n):
	d = []
	for i in range(1, int(math.sqrt(t)) + 1):
		if t % i == 0:
			d.append(i)
			d.append(t // i)
	return d
(a, b) = map(int, input().split())
if a == b or min(a % b, b % a) == 0:
	print(0)
	exit()
if b < a:
	(a, b) = (b, a)
(m, r, t) = (-1, 0, b - a)
d = div(t)
for i in d:
	if t % i == 0:
		k = i * math.ceil(b / i) - b
		lcm = (a + k) * (b + k) // math.gcd(a + k, b + k)
		if lcm < m or m == -1:
			m = lcm
			r = k
print(r)


import math

def div(n):
	a = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			a.append(i)
			a.append(int(n / i))
	a.sort()
	return a
a = list(map(int, input().split()))
a.sort()
if a[1] % a[0] == 0:
	print(0)
elif a[1] < 2 * a[0]:
	d = a[1] - a[0]
	if a[1] % d == 0:
		print(0)
	else:
		q = int(a[0] / d)
		print((q + 1) * d - a[0])
else:
	divisors = div(a[1] - a[0])
	i = len(divisors) - 1
	while divisors[i] > a[0]:
		k = divisors[i] - a[0]
		i = i - 1
	print(k)


(c, d) = map(int, input().split())
a = min(c, d)
b = max(c, d)
w = b - a
c = b * a
e = 0
for i in range(1, int(w ** 0.5) + 1):
	if w % i == 0:
		if b % i == 0:
			q1 = b // i
			q2 = a // i
		else:
			q1 = b // i + 1
			q2 = a // i + 1
		if i * q1 * q2 < c:
			c = i * q1 * q2
			e = i * q1 - b
		elif i * q1 * q2 == c and i * q1 - b < e:
			e = i * q1
		j = w // i
		if b % j == 0:
			q1 = b // j
			q2 = a // j
		else:
			q1 = b // j + 1
			q2 = a // j + 1
		if j * q1 * q2 < c:
			c = j * q1 * q2
			e = j * q1 - b
		elif j * q1 * q2 == c and j * q1 - b < e:
			e = j * q1
print(e)


from math import gcd
import sys
input = sys.stdin.buffer.readline

def lcm(x, y):
	return x * y // gcd(x, y)
(a, b) = map(int, input().split())
i = 1
if a < b:
	(a, b) = (b, a)
curr = lcm(a, b)
ans = 0
while i * i <= a - b:
	if (a - b) % i == 0:
		k = (i - b % i) % i
		t = lcm(a + k, b + k)
		if t < curr:
			ans = k
			curr = t
		i = (a - b) // i
		k = (i - b % i) % i
		t = lcm(a + k, b + k)
		if t < curr:
			ans = k
			curr = t
		i = (a - b) // i
	i += 1
print(ans)


import math
sqrt = math.sqrt

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

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

def divisores(n):
	k = int(sqrt(n)) + 1
	l = []
	for i in range(1, k):
		if n % i == 0:
			l.append(i)
			if n // i != i:
				l.append(n // i)
	return l
(n, m) = [int(i) for i in input().split()]
if n > m:
	n = n + m
	m = n - m
	n = n - m
divs = divisores(m - n)
k = 0
mmcMin = mmc(n, m)
for i in divs:
	if n % i == m % i:
		d = i - n % i
		p = mmc(n + d, m + d)
		if mmcMin > p:
			mmcMin = p
			k = d
print(k)


from math import gcd
(a, b) = list(map(int, input().split()))
f = abs(a - b)
d = []
i = 1
while i * i <= f:
	if f % i == 0:
		d.append(i)
		d.append(f // i)
	i += 1
d = list(set(d))
d.sort()

def f(i):
	return (a + i) * (b + i) // gcd(a + i, b + i)
x = float('inf')
y = 0
for i in d:
	k = (i - a % i) % i
	z = f(k)
	if z < x:
		x = z
		y = k
print(y)


def gcd(x, y):
	while y:
		r = x % y
		x = y
		y = r
	return x

def lcm(x, y):
	return x / gcd(x, y) * y
(a, b) = map(int, input().split())
import math
if a % b == 0 or b % a == 0:
	kans = 0
else:
	c = abs(a - b)
	kans = 0
	lans = lcm(a, b)
	d = int(math.sqrt(c))
	for i in range(1, d):
		if c % i == 0:
			k = i - a % i
			l = lcm(a + k, b + k)
			if l < lans or (l == lans and k < kans):
				lans = l
				kans = k
			k = c / i - a % (c / i)
			l = lcm(a + k, b + k)
			if l < lans or (l == lans and k < kans):
				lans = l
				kans = k
print(int(kans))


(a, b) = map(int, input().split())
(a, b) = (min(a, b), max(a, b))
if a == b:
	print(0)
else:
	(M, u) = (2 * a * b, 0)
	D = 1
	while D * D <= b - a:
		if (b - a) % D == 0:
			for d in (D, (b - a) // D):
				k = 0
				if a % d != 0:
					k = d - a % d
				Q = (a + k) * (b + k) / d
				if Q < M:
					M = Q
					u = k
		D += 1
	print(u)


(a, b) = map(int, input().split())
if a < b:
	(a, b) = (b, a)
if a == b:
	print(0)
	exit()

def update(g):
	global ans
	if a % g != b % g:
		return
	n = g - a % g if a % g else 0
	lcm = (a + n) * (b + n) // g
	ans = min(ans, (lcm, n))
d = a - b
ans = (10 ** 31, 10 ** 31)
for gcd in range(1, int(d ** 0.5) + 2):
	if d % gcd:
		continue
	update(gcd)
	update(d // gcd)
print(ans[1])


from math import gcd
(a, b) = map(int, input().split())
dif = abs(a - b)
v = []
i = 1
while i * i <= dif:
	if dif % i == 0:
		v.append(i)
		if i * i != dif:
			v.append(dif // i)
	i += 1
ans = 1000000000000000
vl = 0
v.sort()
for i in range(len(v)):
	nr = v[i]
	if a % nr != b % nr:
		continue
	if a % nr == 0:
		pans = a * b // gcd(a, b)
		if pans < ans:
			ans = pans
			vl = 0
	else:
		pans = (nr - a % nr + a) * (nr - b % nr + b) // gcd(nr - a % nr + a, nr - b % nr + b)
		if pans < ans:
			ans = pans
			vl = nr - a % nr
print(vl)


(a, b) = map(int, input().split())
from collections import defaultdict
from math import gcd
(a, b) = (min(a, b), max(a, b))
if b % a == 0:
	print(0)
else:
	d = b - a
	d1 = defaultdict(int)
	l = []
	for i in range(1, d + 1):
		if i ** 2 <= d:
			if d % i == 0:
				if i != d // i:
					l.append(i)
					l.append(d // i)
				else:
					l.append(i)
		else:
			break
	ans = 1000000000000000000
	for i in l:
		x = a // i
		if a % i != 0:
			x += 1
		y = x * i - a
		if (b + y) % (a + y) == 0:
			ans = min(ans, b + y)
			z = b + y
			if d1[z] != 0:
				d1[z] = min(y, d1[z])
			else:
				d1[z] = y
	x = a // d
	if a % d != 0:
		x += 1
	k = x * d - a
	lcm = (a + k) * (b + k) // gcd(a + k, b + k)
	if lcm < ans:
		print(k)
	elif lcm > ans:
		print(d1[ans])
	else:
		print(min(k, d1[ans]))


from math import *

def fact(a):
	i = 1
	while i * i <= a:
		if a % i == 0:
			l.append(i)
			if i * i != a:
				l.append(a / i)
		i += 1
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
l = []
g = 1e+18
ans = 0
fact(b - a)
for i in l:
	k = ceil(a / i) * i - a
	if (a + k) * (b + k) / i < g:
		g = (a + k) * (b + k) / i
		ans = k
print(int(ans))


import math
(m, n) = [int(x) for x in input().split()]
lcm = m * n
minimum_lcm = m * n
need_to_add = 0
minimum_need_to_add = 0
divisors = set()
if m % n != 0 and n % m != 0:
	for i in range(1, int(math.sqrt(abs(m - n))) + 1):
		if (m - n) % i == 0:
			divisors.add(i)
			divisors.add(int(abs(m - n) / i))
	for i in divisors:
		if m % i == n % i and m % i != 0:
			need_to_add = i - m % i
			lcm = int((m + need_to_add) * (n + need_to_add) / i)
			if minimum_lcm > lcm:
				minimum_lcm = lcm
				minimum_need_to_add = need_to_add
			elif minimum_lcm == lcm:
				if minimum_need_to_add > need_to_add:
					minimum_need_to_add = need_to_add
		elif m % i == n % i and m % i == 0:
			need_to_add = 0
			lcm = int(m * n / i)
			if minimum_lcm > lcm:
				minimum_lcm = lcm
				minimum_need_to_add = need_to_add
			elif minimum_lcm == lcm:
				if minimum_need_to_add > need_to_add:
					minimum_need_to_add = need_to_add
print(minimum_need_to_add)


from math import sqrt
(a, b) = input().split(' ')
a = int(a)
b = int(b)
if a < b:
	c = a
	a = b
	b = c
t = a - b
ans = 100000000000000000000
ret = 100000000000000000000
if a == b:
	print(0)
else:
	sqt = int(sqrt(t))
	for i in range(1, sqt + 1):
		if t % i == 0:
			p = (b - 1) // i + 1
			p = p * i
			k = p - b
			tmp = (a + k) * (b + k) // i
			if tmp < ans:
				ans = tmp
				ret = k
			elif tmp == ans and k < ret:
				k = ret
			j = t / i
			p = (b - 1) // j + 1
			p = p * j
			k = p - b
			tmp = (a + k) * (b + k) // j
			if tmp < ans:
				ans = tmp
				ret = k
			elif tmp == ans and k < ret:
				k = ret
	print(int(ret))


def euc(x, y):
	(x, y) = (max(x, y), min(x, y))
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def lcm(x, y):
	return x * y // euc(x, y)
(a, b) = map(int, input().split())
(a, b) = (max(a, b), min(a, b))
if a == b:
	ans = 0
else:
	s = a - b
	(c, d) = (a + (1 + b // s) * s - b, (1 + b // s) * s)
	ans = (1 + b // s) * s - b
	mm = lcm(a, b)
	ans = 0
	for i in range(1, int(s ** 0.5) + 3):
		if s % i == 0:
			k = (1 + b // i) * i - b
			xx = lcm(a + k, b + k)
			if xx < mm:
				ans = k
				mm = xx
			k = (1 + b // (s // i)) * (s // i) - b
			xx = lcm(a + k, b + k)
			if xx < mm:
				ans = k
				mm = xx
print(ans)


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

def factorize(n):
	i = 2
	factors = [1]
	while i * i <= n:
		if n % i > 0:
			i += 1
		else:
			n //= i
			factors = factors + [f * i for f in factors]
	if n > 1:
		factors = factors + [f * n for f in factors]
	return factors
factors = factorize(abs(a - b))
res = a * b // gcd(a, b)
ans = 0
for f in factors:
	k = -a % f
	lcm = (a + k) * (b + k) // gcd(a + k, b + k)
	if lcm < res:
		res = lcm
		ans = k
print(ans)


from math import sqrt, ceil
from collections import defaultdict
lim = 10 ** 5

def sieve(N):
	b = [True] * (N + 1)
	b[0] = False
	b[1] = False
	lim = ceil(sqrt(N))
	i = 2
	while i <= lim:
		if b[i]:
			for n in range(i ** 2, N + 1, i):
				b[n] = False
		i += 1
	return [i for (i, b) in enumerate(b) if b]
P = sieve(lim)

def factor(n):
	if n in P:
		return {n: 1}
	F = []
	for p in P:
		while n % p == 0:
			n //= p
			F.append(p)
		if n in P:
			F.append(n)
			break
	else:
		if n != 1:
			F.append(n)
	C = {}
	for f in F:
		if f not in C:
			C[f] = 0
		C[f] += 1
	return C

def divisors(n):
	return divisors_from_factors(factor(n))

def divisors_from_factors(F):
	D = {1}
	for f in F:
		D |= {f ** p * d for d in D for p in range(1, F[f] + 1)}
	return D
from math import gcd
(A, B) = list(map(int, input().split()))
(A, B) = sorted((A, B))

def lcm(a, b):
	return a * b // gcd(a, b)
if A == B:
	print(0)
else:
	mn = 10 ** 100
	D = divisors(B - A)
	for t in sorted(D):
		x = -A % t
		l = lcm(A + x, B + x)
		if l < mn:
			mn = l
			best = x
	print(best)


(a, b) = list(map(int, input().split()))
(a, b) = (min(a, b), max(a, b))
gap = b - a
arr = []
i = 1
while i * i < gap:
	if gap % i == 0:
		arr.append(i)
		arr.append(gap // i)
	i += 1
if i * i == gap:
	arr.append(i)
arr.sort()
answer = 0
value = 10 ** 20
for i in arr:
	plus = 0
	if a % i:
		plus = i - a % i
	(ta, tb) = (a + plus, b + plus)
	mod = ta % tb
	while mod:
		ta = tb
		tb = mod
		mod = ta % tb
	temp = (a + plus) * (b + plus) // tb
	if temp < value:
		value = temp
		answer = plus
	if temp == value:
		answer = min(answer, plus)
print(answer)


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

def lcm(a, b):
	return a * b // gcd(a, b)
import math
(a, b) = [int(i) for i in input().split(' ')]
if a % b == 0 or b % a == 0:
	print(0)
else:
	n = abs(a - b)
	ks = []
	for i in range(1, min(n, int(math.sqrt(10 ** 9)) + 1) + 1):
		if n % i == 0:
			if a % i == 0:
				ks.append(0)
			else:
				ks.append(i - a % i)
			ks.append(n // i - a % (n // i))
	ks = sorted(set(ks))
	k = ks[0]
	minn = lcm(a + k, b + k)
	for i in ks[1:]:
		if lcm(a + i, b + i) < minn:
			k = i
			minn = lcm(a + i, b + i)
	print(k)


(a, b) = map(int, input().split())
if a < b:
	(a, b) = (b, a)
d = []
ab = a - b
for i in range(1, int(ab ** 0.5) + 1):
	if ab % i == 0:
		d.append(i)
		if i != ab // i:
			d.append(ab // i)
mink = 0
minlcm = float('inf')
for i in d:
	if b % i == 0:
		k = 0
	else:
		k = (b // i + 1) * i - b
	lcm = (a + k) * (b + k) // i
	if lcm < minlcm:
		minlcm = lcm
		mink = k
print(mink)


from sys import exit
from math import gcd
(a, b) = [int(i) for i in input().split()]
if a > b:
	(a, b) = (b, a)
d = b - a
if d == 0:
	print(0)
	exit(0)

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

def ans(x):
	return (a + x - 1) // x * x - a
res = 0
i = 0
while i * i <= d:
	i += 1
	if d % i != 0:
		continue
	if lcm(a + res, b + res) > lcm(a + ans(i), b + ans(i)):
		res = ans(i)
	if lcm(a + res, b + res) > lcm(a + ans(d // i), b + ans(d // i)):
		res = ans(d // i)
print(res)


from math import gcd
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
t = b - a
divisors = set()
for i in range(1, int(t ** 0.5) + 1):
	if t % i == 0:
		divisors.add(i)
		divisors.add(t // i)
lcm = 10 ** 18
ans = 0
for x in divisors:
	k = x - a % x
	if k == x:
		k = 0
	temp = (a + k) * (b + k) // x
	if lcm > temp:
		lcm = temp
		ans = k
	elif lcm == temp:
		ans = min(ans, k)
print(ans)


from math import gcd

def factors(k):
	for i in range(1, int(k ** 0.5)):
		if k % i == 0:
			j = k // i
			yield i
			if i != j:
				yield j
(x, y) = sorted(map(int, input().split()))
d = y - x
ml = float('inf')
mo = 0
for q in factors(d):
	o = (q - x % q) % q
	lcm = (x + o) * (y + o) // gcd(x + o, y + o)
	if lcm < ml:
		ml = lcm
		mo = o
print(mo)


import math
(a, b) = map(int, input().split())
if b > a:
	(a, b) = (b, a)
if not a % b:
	print(0)
	exit()
else:
	sub = a - b
	divs = []
	for i in range(1, int(math.sqrt(sub)) + 1):
		if sub % i == 0:
			divs.append(i)
			divs.append(sub // i)
	min_ = 1e+20
	for i in range(len(divs)):
		k = divs[i] - b % divs[i]
		if b % divs[i] == 0:
			k = 0
		if (a + k) * (b + k) / divs[i] < min_:
			min_ = (a + k) * (b + k) / divs[i]
			k_min = k
	print(k_min)


(a, b) = sorted(list(map(int, input().split())))
if a == b:
	print(0)
elif a > b - a:
	if a % (b - a) == 0:
		print(0)
	else:
		print(b - a - a % (b - a))
elif (b - a) % a == 0:
	print(0)
else:
	ans = float('inf')
	for q in range(1, int((b - a) ** (1 / 2)) + 2):
		if (b - a) % q == 0:
			if (b - a) // q >= a:
				ans = min(ans, (b - a) // q)
			if q >= a:
				ans = min(ans, q)
	print(ans - a)


(a, b) = map(int, input().split())
(a, b) = (min(a, b), max(a, b))
x = b - a
import math

def lcm(a, b):
	return a * b // math.gcd(a, b)
minlcm = lcm(a, b)
ans = 0
if a == b:
	print(0)
else:
	for i in range(1, int(x ** 0.5) + 3):
		if x % i == 0:
			y = math.ceil(b / i) * i
			k = y - b
			if lcm(a + k, b + k) < minlcm:
				minlcm = lcm(a + k, b + k)
				ans = k
			i = x // i
			y = math.ceil(b / i) * i
			k = y - b
			if lcm(a + k, b + k) < minlcm:
				minlcm = (a + k) * (b + k) // math.gcd(x, y)
				ans = k
	print(ans)


from math import *
(a, b) = map(int, input().split())
if a == b:
	print(0)
	exit(0)
if a > b:
	(a, b) = (b, a)
t = b - a
ed = int(sqrt(t)) + 1
k = (b - 1) // (b - a) * b - ((b - 1) // (b - a) + 1) * a
ans = (b - 1) // (b - a) * (b + k)
for i in range(1, ed + 1):
	if t % i == 0:
		p = (i * a + b - a - 1) // (b - a)
		tk = (p * b - (p + i) * a) // i
		tmp = p * (b + tk)
		if tmp < ans or (tmp == ans and tk < k):
			ans = tmp
			k = tk
		i = t // i
		p = (i * a + b - a - 1) // (b - a)
		tk = (p * b - (p + i) * a) // i
		tmp = p * (b + tk)
		if tmp < ans or (tmp == ans and tk < k):
			ans = tmp
			k = tk
		i = t // i
print(k)


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

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

def lcm(a, b):
	return a * b // gcd(a, b)
(a, b) = mp()
(a, b) = (min(a, b), max(a, b))
x = []
w = b - a
dl = 1
while dl ** 2 <= w:
	if w % dl == 0:
		x.append(dl)
		x.append(w // dl)
	dl += 1
kk = 0
m = 10 ** 20
for d in x:
	r = (a + d - 1) // d
	k = r * d - a
	if lcm(a + k, b + k) < m:
		m = lcm(a + k, b + k)
		kk = k
print(kk)


import sys
sys.setrecursionlimit(100000)
from math import gcd

def factors(n):
	ans = []
	for i in range(2, int(n ** 0.5) + 1, 1):
		if n % i == 0:
			ans.append(i)
			ans.append(n // i)
		ans.append(n)
	return ans
(x, n) = input().split()
x = int(x)
n = int(n)
if x > n:
	(x, n) = (n, x)
if gcd(x, n) == n - x:
	print(0)
else:
	min = x * n // gcd(x, n)
	ans = 0
	factor = factors(n - x)
	for i in factor:
		w = x + i - x % i
		y = n + i - n % i
		if w * y / gcd(w, y) < min:
			min = w * y // gcd(w, y)
			ans = i - x % i
	print(ans)


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations

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

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

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007
yp = 0

def solve():
	t = 1
	for tt in range(t):
		(a, b) = num()
		ff = max(a, b)
		gg = min(a, b)
		a = gg
		b = ff
		pq = max(a, b) - min(a, b)
		if a == b:
			print(0)
			continue
		elif a % pq == 0 and b % pq == 0 or b % a == 0:
			print(0)
		else:
			i = 1
			mo = []
			while i <= ma.sqrt(pq):
				if pq % i == 0:
					if pq / i == i:
						mo.append(i)
					else:
						mo.append(i)
						mo.append(pq // i)
				i = i + 1
			mn = -1
			ind = -1
			for i in range(len(mo)):
				pqq = mo[i]
				if pqq == 1:
					continue
				hp = (a // pqq + 1) * pqq - a
				po = (b // pqq + 1) * pqq - b
				if mn == -1:
					up = lcm(a + hp, po + b)
					mn = up
					ind = hp
				else:
					up = lcm(a + hp, po + b)
					if up <= mn:
						mn = up
						ind = min(ind, hp)
			print(ind)
solve()


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

def lcm(a, b):
	return a * b // math.gcd(a, b)
minlcm = lcm(a, b)
bestk = 0
for c in range(1, int(math.sqrt(d)) + 10):
	if d % c == 0:
		k = c - a % c
		if minlcm > lcm(a + k, b + k):
			minlcm = lcm(a + k, b + k)
			bestk = k
		cc = d // c
		if cc:
			k = cc - a % cc
			if minlcm > lcm(a + k, b + k):
				minlcm = lcm(a + k, b + k)
				bestk = k
print(bestk)


from math import gcd
(a, b) = map(int, input().split())
c = abs(a - b)
(a1, a2) = (0, a * b // gcd(a, b))

def work(x):
	global a, a1, a2
	y = x - a % x
	if y == x:
		return
	if (a + y) * (b + y) // gcd(a + y, b + y) < a2:
		(a1, a2) = (y, (a + y) * (b + y) // gcd(a + y, b + y))
i = 1
while i * i <= c:
	if c % i == 0:
		work(i)
		work(c // i)
	i += 1
print(a1)


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

def findans(d):
	(l, r) = (1, 10 ** 20)
	while l < r:
		mid = (l + r) // 2
		if mid * d >= m:
			r = mid
		else:
			l = mid + 1
	k = l * d - m
	lcm = (a + k) * (b + k) // d
	return (lcm, k)
(a, b) = map(int, input().split())
(x, y) = (a, b)
if a == b:
	print(0)
else:
	minlcm = 10 ** 20
	diff = abs(a - b)
	m = max(a, b)
	ans = 0
	import math
	for i in range(1, int(math.sqrt(diff) + 1)):
		if diff % i:
			continue
		(lcm, k) = findans(diff // i)
		if lcm < minlcm:
			minlcm = lcm
			ans = k
		(lcm, k) = findans(i)
		if lcm < minlcm:
			minlcm = lcm
			ans = k
	print(ans)


import math
(a, b) = list(map(int, input().split()))
if a > b:
	(a, b) = (b, a)
diff = b - a
j = 1
ans = a * b / math.gcd(a, b)
val = 0
while j * j <= diff:
	if diff % j == 0:
		k = (j - a % j) % j
		temp = (a + k) * (b + k) / math.gcd(a + k, b + k)
		if ans > temp:
			ans = temp
			val = k
		x = int(diff / j)
		k = (x - a % x) % x
		temp = (a + k) * (b + k) / math.gcd(a + k, b + k)
		if ans > temp:
			ans = temp
			val = k
	j = j + 1
print(val)


import math

def calc(k, n):
	ans = n - k % n
	if ans == n:
		ans = 0
	return ans

def lcm(a, b):
	return a * b // math.gcd(a, b)
(a, b) = map(int, input().split())
ans = a * b
w = 0
if b > a:
	(a, b) = (b, a)
for i in range(1, int((a - b) ** 0.5) + 1):
	if (b - a) % i == 0:
		k = calc(a, i)
		lc = lcm(a + k, b + k)
		if lc < ans:
			ans = lc
			w = k
		elif lc == ans:
			if k < w:
				w = k
		n = a - b
		k = calc(a, n // i)
		lc = lcm(a + k, b + k)
		if lc < ans:
			ans = lc
			w = k
		elif lc == ans:
			if w > k:
				w = k
print(w)


import math

def main():
	buf = input()
	buflist = buf.split()
	a = int(buflist[0])
	b = int(buflist[1])
	if a > b:
		(a, b) = (b, a)
	diff = b - a
	if diff <= 1:
		print(0)
		return
	k = (b - a - a) % diff
	gcd = math.gcd(a + k, b + k)
	cd = common_divisor(a + k, b + k)
	smallest_cm = lcm(a + k, b + k)
	smallest_k = k
	for i in cd:
		j = k // i
		l = lcm(a + k - i * j, b + k - i * j)
		if l < smallest_cm or (l == smallest_cm and k < smallest_k):
			smallest_cm = l
			smallest_k = k - i * j
	print(smallest_k)

def common_divisor(a, b):
	gcd = math.gcd(a, b)
	divisor = []
	for i in range(1, int(gcd ** 0.5) + 1):
		if gcd % i == 0:
			divisor.append(i)
			if gcd // i > i:
				divisor.append(gcd // i)
	divisor = list(sorted(divisor))
	return divisor

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


import math as m
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
x = b - a
ans = int(5e+18)
output = 0
G = 1
while G * G <= x:
	if x % G == 0:
		g = G
		k = (g - a % g) % g
		res = (a + k) * (b + k) // m.gcd(a + k, b + k)
		if res < ans:
			ans = res
			output = k
		elif res == ans:
			output = min(output, k)
		g = x // G
		k = (g - a % g) % g
		res = (a + k) * (b + k) // m.gcd(a + k, b + k)
		if res < ans:
			ans = res
			output = k
		elif res == ans:
			output = min(output, k)
	G += 1
print(output)


from math import ceil

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a
(a, b) = [int(item) for item in input().split()]
mn = float('inf')
ans = -1
d = abs(b - a)
if d == 0:
	print(0)
else:
	for g in range(1, int(d ** 0.5) + 1):
		if d % g != 0:
			continue
		k = min(ceil(b / g) * g - b, ceil(a / g) * g - a)
		if (a + k) % g == 0 and (b + k) % g == 0:
			if (a + k) * (b + k) // g < mn:
				mn = (a + k) * (b + k) // g
				ans = k
		g = d // g
		k = min(ceil(b / g) * g - b, ceil(a / g) * g - a)
		if (a + k) % g == 0 and (b + k) % g == 0:
			if (a + k) * (b + k) // g < mn:
				mn = (a + k) * (b + k) // g
				ans = k
	print(ans)


import math

def pr(n):
	a = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n / i == i:
				a.append(int(i))
			else:
				a.extend([int(i), int(n // i)])
		i = i + 1
	return sorted(a)
(a, b) = list(map(int, input().split()))
if a == b:
	print(0)
	exit()
if a > b:
	(a, b) = (b, a)
c = b - a
d = pr(c)
e = []
for i in range(len(d)):
	f = math.ceil(a / d[i]) * d[i] - a
	e.append([(a + f) * (b + f) // math.gcd(a + f, b + f), f])
print(min(e)[1])


from sys import *
from math import *
from bisect import *

def fun(n):
	a = []
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			a.append(i)
			a.append(n // i)
	a.sort()
	return a
(a, b) = map(int, stdin.readline().split())
if a == b:
	print(0)
	exit()
if a > b:
	(a, b) = (b, a)
c = b - a
d = fun(c)
e = {}
k = []
for i in range(len(d)):
	f = ceil(a / d[i]) * d[i] - a
	x = (a + f) * (b + f) // gcd(a + f, b + f)
	k.append(x)
	if x in e:
		e[x] = min(f, e[x])
	else:
		e[x] = f
print(e[min(k)])


from math import gcd

def divisors(x):
	res = set([])
	for i in range(1, x + 1):
		if i * i > x:
			break
		if x % i == 0:
			res.add(i)
			res.add(x // i)
	res = list(res)
	res.sort()
	return res

def main():
	(a, b) = map(int, input().split())
	V = abs(a - b)
	if a == b:
		print(0)
		return
	minimum_value = 10 ** 40
	k_value = 10 ** 40
	D = divisors(V)
	for d in D:
		K = -a % d
		while gcd(V, a + K) != d:
			K += d
		tmp_result = (a + K) * (b + K) // d
		if tmp_result < minimum_value:
			minimum_value = tmp_result
			k_value = K
		elif tmp_result == minimum_value:
			k_value = min(k_value, K)
	print(k_value)
	return
main()


import math
import sys

def gcd(x, y):
	while x and y:
		if x > y:
			x %= y
		else:
			y %= x
	return x + y

def lcm(x, y):
	return x * y // gcd(x, y)
(a, b) = map(int, input().split())
if a == b:
	print(0)
	sys.exit(0)
if a > b:
	(a, b) = (b, a)
ans = int(2e+18)
div = []
n = b - a
sq = int(math.sqrt(n)) + 1
for i in range(1, sq):
	if not n % i:
		div.append(i)
		if n // i != i:
			div.append(n // i)
Final = -1
for i in div:
	now = a // i * i
	if a % i:
		now += i
	k = now - a
	L = lcm(a + k, b + k)
	if ans > L:
		ans = L
		Final = k
print(Final)


def hcf(a, b):
	while a:
		temp = a
		a = b % a
		b = temp
	return b

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

def check(a, b, ko, kn, min_lcm):
	val = lcm(a + kn, b + kn)
	if min_lcm == -2:
		min_lcm = val
	elif val < min_lcm:
		min_lcm = val
		ko = min(ko, kn)
	elif val == min_lcm:
		ko = min(ko, kn)
	return [ko, min_lcm]

def doit(a, b, x):
	i = 1
	min_lcm = -2
	k = pow(10, 9) + 1
	while i * i <= x:
		if x % i == 0:
			f1 = i
			f2 = x // i
			[k, min_lcm] = check(a, b, k, f1 - a % f1, min_lcm)
			[k, min_lcm] = check(a, b, k, f2 - a % f2, min_lcm)
			if f1 % a == 0:
				[k, min_lcm] = check(a, b, k, 0, min_lcm)
			if f2 % a == 0:
				[k, min_lcm] = check(a, b, k, 0, min_lcm)
			if k == 0:
				break
		i += 1
	[k, min_lcm] = check(a, b, k, 0, min_lcm)
	return k
[a, b] = [int(i) for i in input().split()]
if a == b:
	print(0)
else:
	print(doit(a, b, max(a, b) - min(a, b)))


import math
(a, b) = map(int, input().rstrip().split())
divisors = []
abs1 = abs(a - b)
for i in range(1, int(math.sqrt(abs1)) + 1):
	if abs1 % i == 0:
		divisors.append(i)
		divisors.append(int(abs1 / i))
min1 = 10 ** 20
c = 0
for j in range(len(divisors)):
	q = divisors[j]
	if a % q != b % q:
		continue
	if a % q == 0:
		k = 0
		lcm1 = int((a + k) * (b + k) / math.gcd(a + k, b + k))
		if lcm1 < min1:
			min1 = lcm1
			c = k
	if a % q != 0:
		k = (q - a) % q
		lcm1 = int((a + k) * (b + k) / math.gcd(a + k, b + k))
		if lcm1 < min1:
			min1 = lcm1
			c = k
print(c)


from math import gcd
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
divs = []
i = 1
while i * i <= b - a:
	if (b - a) % i == 0:
		divs.append(i)
		if i * i != b - a:
			divs.append((b - a) // i)
	i += 1
best = (a * b // gcd(a, b), 0)
for d in divs:
	rest = a % d
	if rest == 0:
		rest = d
	ths = d - rest
	if ths < 0:
		continue
	cand = ((a + ths) * (b + ths) // gcd(a + ths, b - a), ths)
	if cand[0] < best[0]:
		best = cand
	elif cand[0] == best[0] and cand[1] < best[1]:
		best = cand
print(best[1])


(are, bre) = list(map(int, input().split()))

def plottt(xre, yre):
	if xre == 0:
		return yre
	if yre == 0:
		return xre
	if yre > xre:
		(yre, xre) = (xre, yre)
	return plottt(xre % yre, yre)
if are > bre:
	(are, bre) = (bre, are)
kre = bre - are
plkiyer = []
ire = 1
while ire ** 2 <= kre:
	if kre % ire == 0:
		plkiyer.append(ire)
		plkiyer.append(kre // ire)
	ire += 1
plotttd = are * bre / plottt(are, bre)
result = 0
for dre in plkiyer:
	aare = are - are % dre + dre
	bbre = bre - bre % dre + dre
	if aare * bbre // plottt(aare, bbre) <= plotttd:
		if aare * bbre // plottt(aare, bbre) == plotttd:
			result = min(dre - are % dre, result)
		else:
			plotttd = aare * bbre // plottt(aare, bbre)
			result = dre - are % dre
print(result)


(a, b) = tuple(map(int, input().strip().split()))
if a == b:
	print(0)
else:
	if a > b:
		c = a - b
	else:
		c = b - a
	import math
	i = math.ceil(math.sqrt(c))
	l = []
	for k in range(1, i):
		if c % k == 0:
			l.append(k)
			l.append(c // k)
	if i > 0:
		if c % i == 0:
			l.append(i)
	mina = 10000000000000000000000
	for mj in l:
		tt = -a % mj
		x = (a + tt) * (b + tt) // mj
		if x < mina:
			mina = x
			tut = tt
	print(tut)


import sys
import math
input = sys.stdin.readline
(a, b) = map(int, input().split())
x = min(a, b)
y = max(a, b)
a = x
b = y
d = abs(a - b)
arr = []
i = 1
while i <= math.sqrt(d):
	if d % i == 0:
		if d // i == i:
			arr.append(i)
		else:
			arr.append(i)
			arr.append(d // i)
	i += 1
minn = math.inf
mink = math.inf
for j in range(len(arr)):
	x = a
	r = x % arr[j]
	if r > 0:
		x += arr[j] - r
	if x * (x + d) // math.gcd(x, x + d) < minn:
		minn = x * (x + d) // math.gcd(x, x + d)
		if r > 0:
			mink = arr[j] - r
		else:
			mink = 0
	elif x * (x + d) // math.gcd(x, x + d) == minn:
		if r > 0:
			mink = min(mink, arr[j] - r)
		else:
			mink = 0
if a == b:
	print(0)
else:
	print(mink)


import math
(a, b) = map(int, input().split())
if a < b:
	(a, b) = (b, a)
s = {}
div = []
for x in range(1, int(math.sqrt(a - b)) + 1):
	if (a - b) % x == 0:
		div += [x, (a - b) // x]
for x in div:
	k = (a + x - 1) // x * x - a
	r = (a + k) * (b + k) // x
	s[r] = min(s.get(r, k), k)
if a == b:
	print(0)
else:
	print(s[min(s)])


(a, b) = input().split()
(a, b) = (int(a), int(b))
(a, b) = (min(a, b), max(a, b))

def eu(a, b):
	if a == 0:
		return b
	if b == 0:
		return a
	if a > b:
		return eu(a % b, b)
	return eu(a, b % a)
opt = b - a
factor = []
i = 1
while i ** 2 < opt + 1:
	if opt % i == 0:
		factor.append(i)
		factor.append(int(opt / i))
	i += 1
target = a * b / eu(a, b)
drop = 0
for i in factor:
	firstupd = a - a % i + i
	secondupd = b - b % i + i
	dres = firstupd * int(secondupd / eu(firstupd, secondupd))
	if dres <= target:
		if dres == target:
			drop = min(i - a % i, drop)
		else:
			target = dres
			drop = i - a % i
print(drop)


import math
(a, b) = map(int, input().split())
if a > b:
	(a, b) = (b, a)
ok = b - a
ans = 0
L = 1e+18
for i in range(1, int(math.sqrt(ok + 1))):
	if ok % i == 0:
		k = int(math.ceil(a / (ok // i)))
		k *= ok // i
		k = k - a
		lcd = (a + k) * (b + k) // math.gcd(a + k, b + k)
		if lcd < L:
			L = lcd
			ans = k
		k = int(math.ceil(a / i))
		k *= i
		k = k - a
		lcd = (a + k) * (b + k) // math.gcd(a + k, b + k)
		if lcd < L:
			L = lcd
			ans = k
print(ans)


import math

def lcm(a, b, k):
	return (a + k) * (b + k) // math.gcd(a + k, b + k)
(a, b) = map(int, input().split())
mn = min(a, b)
mx = max(a, b)
div = set()
for i in range(1, int(math.sqrt(mx - mn)) + 1):
	if (mx - mn) % i == 0:
		div.add(i)
div2 = set()
for i in div:
	div2.add(i)
	div2.add((mx - mn) // i)
l = float('Inf')
k = 0
for i in div2:
	t = -mx % i
	if lcm(a, b, t) < l:
		k = t
		l = lcm(a, b, k)
print(k)


import sys
import math
input = sys.stdin.readline

def fac(maxg):
	i = 1
	l = list()
	while i <= int(math.sqrt(maxg)):
		if maxg % i == 0:
			if i == maxg // i:
				l.append(i)
			else:
				l.append(i)
				l.append(maxg // i)
		i += 1
	l.sort()
	return l
(a, b) = map(int, input().split())
maxg = abs(a - b)
if a == 0 or maxg == 0:
	print(0)
elif min(a, b) < maxg:
	l = fac(maxg)
	for i in range(len(l)):
		if l[i] >= min(a, b):
			print(l[i] - min(a, b))
			break
else:
	a = a % maxg
	b = b % maxg
	if a == 0:
		print(0)
	else:
		print(maxg - a)


n = list(map(int, list(input().split())))
v = [n[0], n[1]]
x = max(v)
y = min(v)

def gcd(x, y):
	while y != 0:
		(x, y) = (y, x % y)
	return x

def lcm2(x, y):
	return x * y / gcd(x, y)

def getFactors(n):
	factors1 = []
	factors = []
	for i in range(1, n + 1):
		if n % i == 0:
			factors1.append(i)
			factors.append(i)
		if i > n ** (1 / 2):
			for foo in factors1[::-1]:
				factors.append(int(n / foo))
			return factors
	return factors
if x - y == 0:
	print(0)
else:
	l = getFactors(x - y)
	li = sorted(set([-x % foo for foo in l]), reverse=True)
	n = []
	for foo in li:
		n.append((lcm2(x + foo, y + foo), foo))
	print(min(n)[1])


from math import gcd
lcm = lambda x, y: x * y // gcd(x, y)
(a, b) = map(int, input().split())
c = abs(a - b)
f = 1
res = [lcm(a, b), 0]
while f * f <= c:
	if c % f == 0:
		k = min(-a % f, -b % f)
		ans = lcm(a + k, b + k)
		if res[0] > ans:
			res = [ans, k]
		elif res[0] == ans:
			res[1] = min(res[1], k)
		k = min(-a % (c // f), -b % (c // f))
		ans = lcm(a + k, b + k)
		if res[0] > ans:
			res = [ans, k]
		elif res[0] == ans:
			res[1] = min(res[1], k)
	f += 1
print(res[1])


import math

def main():
	(a, b) = map(int, input().split())
	(divisors, divisor, diff) = ([], 1, abs(b - a))
	while divisor ** 2 <= diff:
		if diff % divisor == 0:
			divisors.append(divisor)
			if divisor ** 2 != diff:
				divisors.append(diff // divisor)
		divisor += 1
	divisors.sort()
	(min_val, ret) = (a * b / math.gcd(a, b), 0)
	for x in divisors:
		if a % x != b % x:
			continue
		if a % x == 0:
			tmp = a * b / math.gcd(a, b)
			if tmp < min_val:
				min_val = tmp
				ret = 0
		else:
			tmp = (x - a % x + a) * (x - b % x + b) / math.gcd(x - a % x + a, x - b % x + b)
			if tmp < min_val:
				min_val = tmp
				ret = x - a % x
	print(ret)
main()


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

def factor(x):
	S = set()
	i = 1
	while i * i <= x:
		if x % i == 0:
			S.add(i)
			S.add(x // i)
		i += 1
	return S

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

def lcm(a, b):
	if a == 0 or b == 0:
		return 0
	return a * b // gcd(a, b)
S = factor(d)
ans = 0
for f in S:
	x = (f - a % f) % f
	y = (f - b % f) % f
	k = lcm(x, y)
	if lcm(a + k, b + k) < lcm(a + ans, b + ans):
		ans = k
print(ans)