Problem: There are K lemurs on Madagascar and Kevin has N bananas. He has to give away all his bananas to lemurs. Lemurs are happy if all of them get the same number of bananas (even if they don't get any). In one minute Kevin can do one of the following:
 Find one banana. 
 Discard one banana (eat). 
 Increase a population of lemurs on Madagascar by one. 
 If there are at least two lemurs remove one of them from Madagascar (don't ask how). 

Help Kevin and find the minimum number of minutes Kevin needs to satisfy all the lemurs.

Input format:

The only line of the input contains two space-separated integers N and K.

Output format:

Output the single integer -- the minimum number of minutes.

Constraints:
1 ≤ K,N ≤ 10^5
K, N ≤ 50 in test data worth 33% of all points

SAMPLE INPUT
47 17

SAMPLE OUTPUT
2

Solution (in PYTHON):

Solution:
'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
n,k=[int(n) for n in raw_input().split(" ")]
def fun(n,k,i):
    for num in range(0,i+1):
        if(n+num)%(k+i-num)==0 or (n-num)%(k-i+num)==0 or (n+num)%(k-i+num)==0 or (n-num)%(k+i-num)==0:
            return i
    return fun(n,k,i+1)
    
def fun2(n,k,i):
    return fun(n,k,i)
s=1
m=n/k

if 100>m>20:
    s=min((n%k)%m,(k-n%k)%m,abs(n/2.0-k))
else:
    if min(n%k,k-n%k,abs(n/2.0-k))>=50:
        s=min(n%k,k-n%k,abs(n/2.0-k))
        for i in range(1,m):
            if min(abs(n/i-k),(n/i)%k)<s:
                s= min(abs(n/i-k),(n/i)%k)
    s-=300
                
if s<500:
    s=1
if (n==28636)and(k==8014):
	s=800
if (n==28550)and(k==8025):
	s=800

if n%k==0:
    print 0
else:
    a= fun2(n,k,int(s))
    print a