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

def LI():return list(MI())



N,K=MI()



ans=N%K

ans2=(-N)%K

print((min(ans,ans2)))
n,k=list(map(int,input().split()))

print((min(n%k,(-n)%k)))
def MI():
    return list(map(int, input().split()))


def LI():
    return list(MI())


N, K = MI()
ans = N % K
ans2 = (-N) % K
print((min(ans, ans2)))

n, k = list(map(int, input().split()))
print((min(n % k, (-n) % k)))

-def MI(): -    return list(map(int, input().split())) - - -def LI(): -    return list(MI()) - - -N, K = MI() -ans = N % K -ans2 = (-N) % K -print((min(ans, ans2))) +n, k = list(map(int, input().split())) +print((min(n % k, (-n) % k)))