Problem:


Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Accepted

Solution code:
url = "https://atcoder.jp/contests/abc161/tasks/abc161_c"

def main():
    n, m = list(map(int, input().split()))

    anss = []
    ans = 1
    while ans != 0:
        if n > m:
            ans = n % m
            n = ans
        else:
            ans = abs(n - m)
            n = ans
        if ans in anss:
            break
        else:
            anss.append(ans)
    print(min(anss))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
kaisu = int(n / k) + 1
if n % k == 0:
    print(0)
else:
    m = abs((n % k) - k)
    print(min(m, n))

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
stopf = True
wari = N // K
N = N - K*wari
def exchangeK():
    global stopf
    global N
    if N > abs(N-K):
        N = abs(N-K)
    else:
        stopf = False
while stopf:
    exchangeK()
print(N)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

def substract(min):
    if N % K == 0:
        return 0
    tmp = N % K
    if abs(tmp - K) < tmp:
        return abs(tmp-K)
    return tmp
    #while(1):
    #    tmp = abs(tmp - K)
    #    if tmp > min:
    #        break
    #    min = tmp
    #return min

if abs(N-K) < N or abs(N-K) < K:
    print(substract(abs(N-K)))
elif N < K:
    print(N)
else:
    print(K)

Submission status: Accepted

Solution code:
def main():
    N, K = list(map(int, input().split()))

    N = N - K * (N//K)
    #N<K

    if N <= K//2:
        res = N
    else:
        res = abs(K - N)

    print(res)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
store = N
x = N % K
if N == 0:
    print(0)
else:
    while x < store:
        store = x
        x = abs(x - K)

    if store > abs(N - K):
        print(abs(N - K))
    else:
        print(store)

Submission status: Accepted

Solution code:
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    k = kwargs["k"]

    res = n - (max(0, (n - 2 * k) // k)) * k

    while True:
        if res <= abs(k - res):
            break
        else:
            res = abs(k - res)

    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    #kwargs["n"] = cin[0]
    #kwargs["n"], kwargs["k"] = cin
    #kwargs["n"] = [int(i) for i in cin][0]
    kwargs["n"], kwargs["k"] = [int(i) for i in cin]

    cout = main(kwargs)
    print(cout)

Submission status: Accepted

Solution code:
import sys

N, K = [int(e) for e in input().split()]

N = N - K * (N // K)

min = N
N = abs(N - K)
if N < min:
    min = N


print(min)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())

n = n%k

if n<abs(n-k):
    print(n)
else:
    print(abs(n-k))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())

if n == k or k == 1:
    print(0)
    exit()

if n > k:
    temp = n%k
    if temp < abs(k-temp):
        ans = temp
    else:
        ans = abs(k-temp)
    print(ans)
else:
    if k-n < n:
        print(k-n)
    else:
        print(n)

Submission status: Accepted

Solution code:
N,K =map(int,input().split())
amari = N%K

ans = min(amari,abs(amari-K))


print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a=n//k
b=n%k
c=abs(n-k*(a+1))
print(min(b,c))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

x = N // K

y = N - x*K

ans_1 = y
ans_2 = abs(y-K)

if ans_1 <= ans_2:
    print(ans_1)
else:
    print(ans_2)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
q = N // K
r = N % K

if r == 0:
    print(0)
else:
    mimi = abs(K-r)
    mini = min(mimi,N)
    print(mini)

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
N%=K
while N > K/2:
    N = abs(N-K)
print(N)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if n < k:
    p = k-n
    n = k+p
if n % k == 0: print(0)
else:
    a,b = divmod(n, k)
    print( min (  b ,  abs(b - k)  )  )

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = abs(n % k)
a = min(a, k-a)
print(a)

Submission status: Accepted

Solution code:
data = list(map(int, input().split()))
N = data[0]
K = data[1]

N=N%K

start = N
ans = 0

def hoge(i, j):
  return abs(i - j)

ans = hoge(N, K)

if start <= ans:
  print(start)
else:
  while ans >= hoge(ans, K):
    ans = hoge(ans, K)
  print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np

n,k=(int(x) for x in input().split())
print(min(n%k,k-n%k))

Submission status: Accepted

Solution code:
n,k=map(int,input('').split(' '))
s=0
h=0

n=n%k
while s==0:
    h=n
    n=n-k
    if n<0:
        n=n*(-1)
    if h>n:
        s=0
    else:
        s=1
        n=h

print(n)

Submission status: Accepted

Solution code:
N,K=list(map(int,input().split()))
n=N%K
old_n=K+1
while True:
  if old_n<=n:
    print(old_n)
    break
  old_n=n
  n=abs(n-K)

Submission status: Accepted

Solution code:
N, K  = map(int, input().split())

N = N % K

print ( min( N, (K-N) ) )

Submission status: Accepted

Solution code:
n, k= list(map(int, input().split()))
if k<=n:
    if n%k == 0:
        print("0")
        exit()
amari = n%k
n = amari
while(True):
    new_n = abs(k-n)
    if new_n < n:
        n = new_n
    else :
        break
print(n)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
if k ==1:
    print(0)
else:
    ans1 = n//k
    ans2 = ans1+1
    mn = min((n-(ans1*k)),(ans2*k)-n)
    print(mn)

Submission status: Accepted

Solution code:
N, K = [int(n) for n in input().split(' ')]

N = N%K
if N==0:
    print(N)
else:
    print(min(abs(N-K), N))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
buf = abs(n-k)
print(min(n%k,abs(n%k-k)))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
ans=N%K
if ans==0:
	print(0)
	exit()
else:
	ans=min(ans,abs(ans-K))
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

div, mod = divmod(n, k)

# print(div)
# print(mod)

mod_ = abs(mod - k)

# print(mod_)

if mod < mod_:
    print(mod)
else:
    print(mod_)

Submission status: Accepted

Solution code:
num = list(map(int, input().split()))
print(min([num[0],num[0]%num[1],abs((num[0]%num[1])-num[1])]))

Submission status: Accepted

Solution code:
N,K = list(map(int, input().split()))
m = N // K
a = N - m*K
b = abs(a- K)
print(min(a,b))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if k == 1 or n == 0:
    print(0)
    exit()
if n > k:
    n = n % k
print(min(n, k - n))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

# if N == K:
#     print(0)
#     exit(0)
# elif N < K:
#     print(N)
#     exit(0)

ans = []
before = abs(N - K)
N = N % K
for i in range(10000):
    ze = abs(N - K)
    ans.append(ze)
    before = ze
    N = ze
print(min(ans))

Submission status: Accepted

Solution code:

def main():
    n,k = map(int,input().split())
    if n >= k:
        answer = min(n%k,k-(n%k))
    else:
        answer = min(k-n,n)

    print(answer)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = [int(i) for i in input().split()]

if k>=n*2:
  print(n)
elif k>=n:
  print(abs(n-k))
else:
  amari=n%k
  print(min(amari,abs(amari-k)))

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
x = N % K
y = (N//K + 1) * K - N 
print(min(x,y))

Submission status: Accepted

Solution code:
n, k = (int(x) for x in input().split())
ans = n % k
if n >= 1:
    if ans > (k - ans) / 2:
        ans = k % ans
print(ans)

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
d, m = divmod(n, k)
result = min(abs(m - k), m)
print(result)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

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

Submission status: Accepted

Solution code:
a,b = map(int, input().split())
c = b*100
d = b*10000
e = b*1000000
f = b*100000000
g = b*10000000000
h = b*1000000000000
i = b*100000000000000
j = b*10000000000000000
k = b*1000000000000000000

while(a > abs(a-b)):
    if a > k:
        a = abs(a-k)
    elif a > j:
        a = abs(a-j)
    elif a > i:
        a = abs(a - i)
    elif a > h:
        a = abs(a - h)
    elif a > g:
        a = abs(a - g)
    elif a > f:
        a = abs(a - f)
    elif a > e:
        a = abs(a - e)
    elif a > d:
        a = abs(a - d)
    elif a > c:
        a = abs(a - c)
    else:
        a = abs(a-b)

print(a)

Submission status: Accepted

Solution code:
N, K = map(int, input().strip().split())

n = N-(((N-K)+(K-1))//K)*K
print(min(n, abs(K-n)))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
H = 0
while True:
  if (N > K):
    H = N%K
  else:
    H = K-N
  if (H>N):
    break
  else:
    N = H
print(N)

Submission status: Accepted

Solution code:
import math
n, k = map(int, input().split())

if n < k:
    if n > k / 2:
        print(k - n)
    else:
        print(n)
else:
    s = math.ceil(n / k)
    ans = abs(n - s  * k)
    while True:
        x = abs(ans - k)
        if x > ans:
            break
        else:
            ans = x
    print(ans)

Submission status: Accepted

Solution code:
def f(N, K):
    if N < K:
        if K-N >= N:
            return N
        else:
            return f(K-N, K)
    else:
        if N <= N%K:
            return N
        else:
            return f(N%K, K)


def main():
    N, K = [int(x) for x in input().split()]
    print(f(N, K))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10**20)


def fom(x, k):
    if x <= abs(x - k):
        return x

    return fom(abs(x - k), k)


n, k = map(int, input().split())
m = n % k
print(fom(m, k))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def main():
    N,K = map(int, input().split())
    
    N = N % K

    def sousa(num):
        before = num
        after = abs(N - K)
        if after >= before:
            return before
        else:
            return sousa(after)

    print(sousa(N))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split(' '))


def main(n, k):
    # print(n, k)
    if n % k == 0:
        # print('n%k=0')
        res = 0
    else:
        # print(n % k, abs((n % k)-k), n)
        res = min(n % k, abs((n % k) - k), n)

    print(res)


main(N, K)
# main(7, 4)
# main(2, 6)
# main(1000000000000000000, 1)
# main(9, 6)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
p = n % k
if p < abs(p-k):
    print(p)
else:
    print(abs(p-k))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
amari = N % K
nokori = K - amari
if nokori < amari:
  print(nokori)
else:
  print(amari)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
n = n % k
while n > abs(n - k):
    n = abs(n - k)

print(n)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
a = n%m
b = abs((n%m)-m)
if a < b:
    print(a)
else:
    print(b)

Submission status: Accepted

Solution code:
a = list(map(int,input().split()))
n = a[0]
k = a[1]

if 2*n <= k:
  n = n
elif (k<2*n)and(n<=k):
  n = k-n
elif k<n:
  n = n%k
  if 2*n<=k:
    n = n
  else:
    n = k-n
print(n)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())

if n==k:
  print(0)

elif k > n:
  if k-n<=n:
    print(k-n)
  else:
    print(n)
  
else:
  amari = n%k
  com3 = k -amari
  if amari<= com3:
    print(amari)
  else:
    print(com3)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
if N > K:
    p = (N - 1)//K
    N_p = N - p*K
    N_q  = -(N_p - K)
    ans = min(N_p, N_q)
else:
    ans = min(N, K-N)
print(ans)

Submission status: Accepted

Solution code:
n , k = map(int,input().split())

if(n%k == 0):
        
    g = n//k
        
    print(n - (g*k))
else:
    g = n//k
    ans = n - (g*k)
    if(abs(ans - k) < ans):
        print(abs(ans - k))
    else:
        print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
if n == k:
    print(0)
else:
    ans = n % k
    while (True):
        if ans < abs(ans - k):
            print(ans)
            exit()
        ans = abs(ans - k)

Submission status: Accepted

Solution code:
n, k = input().split(' ')
n = int(n)
k = int(k)

if k == 1 or n == 0:
  print(0)
elif n < k :
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
else:
  r1 = n % k
  r2 = r1 - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
m=n%k
if m>abs(m-k):
    m=abs(m-k)
print(m)

Submission status: Accepted

Solution code:
n,k=map(int,input().split(" "))

if(n==k or n%k==0):
    print(0)
elif(n<k):
    print(min(n,abs(n-k)))
else:
    q=n//k
    ma=n%k

    aa = n-q*k
    print(min(aa,abs(aa-k)))

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    while True:
        if n % k == 0:
            n = 0
            break
        else:
            if n // k == 0:
                if n > abs(n - k):
                    n = abs(n - k)
                break
            else:
                n = n % k
    print(n)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

t = N % K

while t >= abs(t -K):
  t = abs(K - t)
  if t == 0:
    break

print(t)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

if n >= k:
  d = n//k
  if k*(d+1)-n >= n-k*d:
    n = n-k*d
  else:
    n = k*(d+1)-n
else:
  if n >= k-n:
    n = k-n
print(n)

Submission status: Accepted

Solution code:
n, k = (int(i) for i in input().split())
if k > n:
    print(min(n, abs(n-k)))
elif n % k == 0:
    print(0)
else:
    q = n % k
    p = (k * (n//k+1)) - n
    print(min(p, q))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

a, b = N // K, -(-N // K)

def f(t):
    return abs(N - K * t)

print(min(f(a), f(b)))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())

a1=n%k
a2=abs(a1-k)
print(min(a1,a2))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
if n>k:
	n=n-n//k*k;
print(min(n,k-n))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# coding: utf-8
"""
about module
"""


def main():
    n, k = map(int, input().split())
    if n > k:
        n = n % k

    while n * 2 > k:
        n = abs(n - k)
    return n


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
print(min(N%K,abs((N%K)-K)))

Submission status: Accepted

Solution code:
hoge = input().split()
N = int(hoge[0])
K = int(hoge[1])


while 1:
  if N / K > pow(10,18):
    N = N - (K * pow(10,17))
  elif N / K > pow(10,17):
    N = N - (K * pow(10,16))
  elif N / K > pow(10,16):
    N = N - (K * pow(10,15))
  elif N / K > pow(10,15):
    N = N - (K * pow(10,14))
  elif N / K > pow(10,14):
    N = N - (K * pow(10,13))
  elif N / K > pow(10,13):
    N = N - (K * pow(10,12))
  elif N / K > pow(10,12):
    N = N - (K * pow(10,11))
  elif N / K > pow(10,11):
    N = N - (K * pow(10,10))
  elif N / K > pow(10,10):
    N = N - (K * pow(10,9))
  elif N / K > pow(10,9):
    N = N - (K * pow(10,8))
  elif N / K > pow(10,8):
    N = N - (K * pow(10,7))
  elif N / K > pow(10,7):
    N = N - (K * pow(10,6))
  elif N / K > pow(10,6):
    N = N - (K * pow(10,5))
  elif N / K > pow(10,5):
    N = N - (K * pow(10,4))
  elif N / K > pow(10,4):
    N = N - (K * pow(10,3))
  elif N / K > pow(10,3):
    N = N - (K * pow(10,2))
  else:
    break
  
_min = N
while 1:
  if _min <= abs(_min - K):
    break
  else:
    _min = abs(_min - K)

print(int(_min))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
import math
# stdin = open("sample.txt")

N,K = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = N % K
ans2 = (ans1 - K) * -1
# print(ans1,ans2)
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
def LI():
	return [ int(s) for s in input().split() ]
N,K= LI()

MOD = N % K

print(min(MOD,K-(MOD)))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
if abs(N-(N//K)*K)<abs((N-(N//K)*K)-K):
    print(N-(N//K)*K)
else:
    print(abs((N-(N//K)*K)-K))

Submission status: Accepted

Solution code:
N1, K = map(int, input().split()) 
if N1>K:
  N1 = N1%K
N2=N1-K
if N2<0:
  N2=-N2
if N1>N2:
  print(N2)
else:
  print(N1)

Submission status: Accepted

Solution code:
l = input().split()
n =int( l[0])
k = int(l[1])
if n%k < k -(n%k) :
    print(n%k)
else:
    print(k -(n % k))

Submission status: Accepted

Solution code:
nk = list(map(int,input().split()))

N = nk[0]
x = nk[1]

while N > abs(N-x):
    if N > x:
            N = abs(N-(N//x)*x)
    else:
        N = abs(N-x)

print(N)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

if n > k:
    n = n % k

while abs(n-k) < n:
        n = abs(n - k)

print(n)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
min = N % K
ans = 0
if min < abs(min - K):
    ans = min
else:
    ans = abs(min - K)
print(ans)

Submission status: Accepted

Solution code:

NS = [int(_) for _ in input().split()]

N = NS[0]
K = NS[1]

def rep(x, K):
    return abs(x-K)

def solve(N, K):
    b = N % K
    if b != 0:
        b = rep(N%K, K)
    
    if b > N:
        b = N

    return b

print(solve(N, K))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if n % k == 0:
    print(0)
elif n < k and n <= k / 2:
    print(n)
elif n < k and n > k / 2:
    print(abs(n - k))
elif n % k <= k / 2:
    print(n % k)
else:
    print(k - (n % k))

Submission status: Accepted

Solution code:
#n = int(input())
n,k = map(int,input().split())
#a_L = list(map(int,input().split()))

if abs(n-k) >= n:
    print(n)
    exit()

if n > k:
    tmp = n%k
    print(min(tmp,abs(tmp-k)))
    exit()

if n==k:
    print(0)
    exit()

if n == 0:
    print(0)


print(abs(n-k))

Submission status: Accepted

Solution code:
N, K = map(int, raw_input().split())

mod = N % K
mod1 = K - mod

if mod > mod1:
    print(mod1)
else:
    print(mod)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
if n % k == 0:
    print(0)
else:
    a = [n]
    b = n//k
    c = n-(b*k)
    half = k//2
    while c > half:
        c = abs(c-k)
    a.append(c)
    print(min(a))

Submission status: Accepted

Solution code:
N, K = map(int,input().split())


N_list = [N,K]

if K == 1:
    print(0)
    
elif N>K:
    print(min(N%K,abs(N%K-K)))
    
elif N == K:
    print(0)
    
else:
    print(min(N,abs(N-K)))

Submission status: Accepted

Solution code:
import math

#n,k
n,k = (int(x) for x in input().split())

b = n % k

#print(b)

c = abs(k-b)

if b < c:
    ans = b
else:
    ans = c

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
a = n%k
if a>=((k//2)+1):
    print(k-a)
else:
    print(a)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

n %= k
print(min(n, k - n))

Submission status: Accepted

Solution code:
N, K = map(int,input().split())

def replace(N, K):
  N = abs(N - K)
  return N

N = N%K
already = set([N])

while True:
  N = replace(N, K)
  if N in already:
    break
  else:
    already.add(N)

print(min(already))

Submission status: Accepted

Solution code:
import sys
from math import floor

if __name__ == "__main__":
	N, K = map(int,input().split())

	if K == 1 or (N % K) == 0:
		print(0)
		sys.exit()
	elif N > K:
		N = N % K

	for _ in range(K):
		if abs(N - K) < N:
			N = abs(N - K)
		else:
			print(N)
			break

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
n%=k
while True:
    f=n
    n=abs(n-k)
    if f<=n:
        print(f)
        break

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
c=n%k
o=abs(c-k)
print(min(c,o))

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c=a%b
d=[a,c,(c-b)*-1]
print(min(d))

Submission status: Accepted

Solution code:
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N,K = LMIIS()
    count = 0
    pre = 10**18+1

    x = N


    x = N % K
    while x >= K //2 and x!= 0:
        x = K % x

    print(x)



    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

N,K = (int(x) for x in input().split(' '))

preN=0

N=N%K

while True:
	preN = N
	N = abs(N-K)

	if preN<N:
		break
print(preN)

Submission status: Accepted

Solution code:
line = input().split(" ")
leftNum = int(line[0])
rightNum = int(line[1])

output = ""

if leftNum < rightNum:
  output = leftNum

leftNum = leftNum % rightNum  
  
while(1):
  output = abs(leftNum - rightNum)
  if output <= (rightNum / 2):
    break;
  leftNum = output

print(output)

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
n = n % k

while(True):
    tmp = abs(n - k)
    if tmp >= n:
        break
    n = tmp
print(n)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
C = N % K
print(min(C, abs(C - K)))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
if N >= K:
    n = N % K
    print(min(abs(n-K),n))
else:
    print(min(abs(N-K),N))

Submission status: Accepted

Solution code:
def main():
    n, k = [int(e) for e in input().split()]

    print(min(n % k, k - (n % k)))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

I = N // K
J = N % K
j = J

if J == 0:
  pass
else:
  j = J
  for i in range(I+2):
    J = abs(J - K)
    if J <= j:
      j = J
    else:
      break
      
print(j)

Submission status: Accepted

Solution code:
x,y = map(int,input().split())
k = x % y
if k <= y-k:
    print(k)
else:
    print(y-k)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())

right=N%K
left=K-N%K
#print(left,right)

if right>=left:
    print(int(left))
else:
    print(int(right))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
if (N%K==0):
  print(0)
else:
  print(min(abs(N -(N//K)*K),abs(N -(N//K+1)*K)))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

while N > K:
  N = N % K

dis = K - N

if dis >= N:
  min = N
if dis < N:
  min = dis

print(min)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
if N==0:
    print(0)
    exit()
if K==1:
    print(0)
    exit()
if K==2:
    if N%2==0:
        print(0)
        exit()
    else:
        print(1)
        exit()

def func(x,K):
    tmp=x-K
    if tmp<0:
        tmp*=(-1)
    return tmp

if (N>K):
    y=int(N/K)
    N=func(N,K*y)

ans = N
while(True):
    N=func(N,K)
    if N<ans:
        ans=N
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N ,K = map(int,input().split())

intlist = [ (N % K) ,abs (( N % K ) - K )] 
print (min(intlist))

Submission status: Accepted

Solution code:
n,k = list(map(int,input().split()))
if n%k==0:
    print(0)
else:
    l = [n]
    p = n//k
    c = n - (k*p)
    half = k//2
    while c > half:
        c = abs(c-k)
    l.append(c)
    print(min(l))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
N = N%K
while(1):
    tmpN = abs(N-K)
    if tmpN < N:
        N = tmpN
    else:
        break;
print(N)

Submission status: Accepted

Solution code:
n,k= map(int,input().split())

if n%k < k/2 :
    print(n%k)
else:
    print(abs((n%k)-k))

Submission status: Accepted

Solution code:
N,K =  map(int,input().split())
T = N%K
L = [T,abs(T-K)]
print(min(L))

Submission status: Accepted

Solution code:
N, k = map(int,input().split())
N = N % k
while N > abs(N-k):
    N = abs(N-k)
print(N)

Submission status: Accepted

Solution code:
def solution():
    n, m = map(int, input().split())
    # if n < m:
    #     print(0)
    #     return
    if n == 0:
        print(0)
        return
    ans = n % m
    print(min(ans, abs(ans-m)))

solution()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 21:17:14 2020

@author: liang
"""

N, K = map(int,input().split())

if N > K :
    N = N%K
ans = N

    
while(min(ans,abs(ans-K)) != ans):
    ans = abs(ans-K)
    

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if n>k:
    n=n%k
if n > k/2:
    n=k-n
print(n)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if n>=k:
    n=n%k
#now n<k
if 2*n>=k:
    n=k-n
print(n)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
mod = n % k

if mod == 0:
    print("0")
elif (k - mod) > mod :
    print(mod)
else:
    print(k - mod)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())

m = N//K 
if m == 0:
    a = N
    b = abs(a-K)
    while b < a:
        a = min(a,b)
        b = abs(a-K)
else:
    a = N
    b = abs(a-K*m)
    while b < a:
        a = min(a,b)
        b = abs(a-K)

print(a)

Submission status: Accepted

Solution code:
n, k = map(int, input().split(' '))
n = n % k
while True:
    if n <= k:
        if n < abs(n - k):
            break
        else:
            n = abs(n - k)
        break
    n = abs(n - k)
print(n)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter, deque

INF = float('inf')


def abs_score(a, b):
    return abs(a - b)


def main():
    N, K = map(int, input().split())

    if N < K:
        sa = abs(N - K)
        print(min(N, sa))
        exit()

    if N % K == 0:
        a = N // K
    else:
        a = N // K + 1

    print(abs(N - a * K))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=N%K
if x<=K/2:
    print(x)
else:
    print(K-x)

Submission status: Accepted

Solution code:
N, K= map(int, input().split())

if N//K>=2:
  N=N-(N//K)*K
  
while N>abs(N-K):
  N=abs(N-K)

print(N)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())


def calc(i, j, bf):
    if i == 0:
        return 0

    if i > bf:
        return bf

    return calc(abs(i - j), j, i)


amari = n % k

ans = calc(amari, k, amari)

print(min(ans, n))

Submission status: Accepted

Solution code:
def get_int_list(inp_str):
    return list(map(int, inp_str.split(" ")))

n, k = get_int_list(input())

joyo = n % k
ans = int(min(joyo, k - joyo))
print(ans)

Submission status: Accepted

Solution code:
a = list(map(int,input().split()))

#if a[0] < a[1]:
# print(a[0])
#else:
b = a[0] % a[1]
c = abs(b-a[1])
d = abs(b+a[1])
print(min(b,c,d))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=n//k
n=n-k*a
if n>abs(n-k):
	n=abs(n-k)
print(n)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
ans=n
nex=n
w=n//k
#print(w)
nex=n-k*w
if ans>nex:
  ans=nex
#print(nex,ans)
while (nex<=k and nex>(k-nex)) and ans!=0:
  nex=k-nex
  if nex<ans:
    ans=nex
  if ans==0:
    break
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

d = n // k
ans = min(k-(n-k*d), (n-k*(d-1))-k)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
print(min(n,n-k*(n//k),abs(n-k*(n//k)-k)))

Submission status: Accepted

Solution code:
inputLine=input()
N=int(inputLine.split(" ")[0])
K=int(inputLine.split(" ")[1])

x=N%K
result=x
firstX=x

xNext=abs(x - K)
result=min(result, xNext)
x=xNext
while(x != firstX):
    xNext=abs(x - K)
    result = min(result, xNext)
    x=xNext

print(result)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N >= K:
    N -= (N//K)*K
print(min(N, K-N))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
diff = abs(N - K)

while(N > diff):
  max_num = max(N, K)
  min_num = min(N, K)
  N = max_num % min_num
  diff = abs(N - K)

print(N)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
min = n + 1
if n >= k:
    n = n % k
    
while True:
    if n < min:
        min = n
        n = abs(n - k)
    else:
        break

print(min)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n, k = LI()
    n = n % k
    print(min(n % k, abs(n - k)))
    
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

if k > n:
    tmp = n
else:
    tmp = n % k

if tmp > k / 2:
    tmp = k - tmp

print(tmp)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())

a = n%k
b = abs(k - a)
print(min(a,b))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())

x = n//k
a = k * x
b = a + k

b -= n
a -= n

b = abs(b)
a = abs(a)

if a <= b:
    print(a)
else:
    print(b)

Submission status: Accepted

Solution code:
x, k = map(int, input().split())
result = x%k
if x < k :
    if x < (k - x) :
        print(x)
    else :
        print(k - x)
else :
    if result == 0 :
        print(0)
    elif k > result :
        print(abs(k - result))
    else :
        print(result)