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:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def No(): print('No')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N,K = imn()


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

Submission status: Accepted

Solution code:
x,y=map(int,input().split())

if x>=y:
    x=x%y
a=y-x
b=x

print(min(a,b))

Submission status: Accepted

Solution code:
#200404C
N, K = map(int, input().split())
flag = 1
X = N%K
if abs(X-K)<X:
    print(abs(X-K))
else:
    print(X)

Submission status: Accepted

Solution code:
x,K=map(int,input().split())
temp=x+1
if K>=x:
    print(min(x,K-x))
else:
    while temp>x:
        temp=x
        if x>=K:
            x%=K
        else:
            x=K-x
    print(temp)

Submission status: Accepted

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

Submission status: Accepted

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

if nmin > abs(nmin-k):
    nmin = abs(nmin-k)

print(nmin)

Submission status: Accepted

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

Submission status: Accepted

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

r=n%k

R=(r-k)*(-1)
  
print(min(r,R))

Submission status: Accepted

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

if N > K:
    A = N - (N // K) * K
    B = K - A
    if A <= B:
        print(A)
    else:
        print(B)
elif 2*N > K:
    print(K - N)
else:
    print(N)

Submission status: Accepted

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

# 前処理の追加
n %= k

min_ = n

def r(min_, k):
  if min_ > abs(min_ - k):
    min_ = abs(min_ - k)
    return r(min_, k)
  else:
    return min_

print(r(min_, k))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
#n * 2まで
counter = 0
while True:
    if n * 2 < k:
        print(n)
        exit()
    if counter == 0:
        n = abs(n - (k * (n//k)))
        counter += 1
    else:
        n = abs(n - k)

Submission status: Accepted

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

if(n%k==0):
    print("0")
elif(n>k):
    ans1 = n % k
    ans2 = abs(ans1-k)

    if(abs(ans1)<abs(ans2)):
        print(ans1)
    else:
        print(ans2)
else:
    ans = abs(n-k)
    if(ans<n):
        print(ans)
    else:
        print(n)

Submission status: Accepted

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

if n >= k:
    n = n%k
    
while n > abs(n-k):
    n = abs(n-k)
    
print(n)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import math


debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    (n, k) = [int(e) for e in lines[0].split(" ")]

    return (n, k)


def solve(n, k):


    r = n % k

    r2 = abs(r - k)

    result = min([r, r2])
    
     
    return  result


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def input():
    return sys.stdin.readline()


def resolve():
    n, k = map(int, input().split())

    min_num = 9999999999999999

    amari = n % k

    ans_list = []
    ans_list.append(amari)

    ans1 = abs(amari - k)
    ans_list.append(ans1)
    ans2 = abs(ans1 - k)
    ans_list.append(ans2)

    answer = min(ans_list)

    print(answer)




if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
s = N // K
n1 = N - K*s
n2 = n1 - K
print(min(abs(n1),abs(n2)))

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())

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())
n = n % k
print(min(n,k-n))

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

p = n%k
if abs(p-k) < p:
    print(abs(p-k))
else:
    print(p)

Submission status: Accepted

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

Submission status: Accepted

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

temp=n

temp=min(n,n%k)

for i in range(10^6):
    temp=min(temp,abs(temp-k))

print(temp)

Submission status: Accepted

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

X1 = N % K

X2 = abs(X1 -K)

if X1 < X2:
  print(X1)
else:
  print(X2)

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
m=(N//K)
if m>=1:
    tmp=abs(N-K*m)
    res=min(tmp,abs(tmp-K))
else:
    res=abs(N-K)
print(min(N,res))

Submission status: Accepted

Solution code:
N, K = (int(x) for x in input().split())
if N < K:
    a = K-N
    N = min(N,a)
elif N == K:
    N = 0
else:
    N = N%K

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

Submission status: Accepted

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

x = n
mm = n

x = n%m
if x ==0:
  print('0')
else:

  for i in range(n):
    dif = x-m

    if dif < 0:
      mm = min(mm,abs(abs(dif)-m),abs(dif))
      break
    mm = min(mm,dif)
    x = dif
  print(mm)

Submission status: Accepted

Solution code:
def zettai(n,k):
    return abs(n-k)
n,k=map(int,input().split())
n=n%k
while(zettai(n,k)<n):
    n=zettai(n,k)
print(n)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,boundary=1,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0



x,k=LI()
ans=x%k
ans=min(ans,abs(ans-k),x)
print(ans)

Submission status: Accepted

Solution code:
x = list(map(int,input().split(" ")))
n = x[0]
k = x[1]
import math
l = abs(n % k)
# print(l)

if l > k//2:
    print(k-l)
else:
    print(l)

Submission status: Accepted

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

a = n // k 

print(min(abs(n - a * k), abs(n - (a + 1) * k)))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if n == 0:
    print(n)
else:
    x = n // k
    i = n - k * x
    while True:
        if min(i, abs(i-k)) == i:
            break
        else:
            i = abs(i-k)
    print(i)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N,K=nm()
ans=0
if(N>K):
    ans=min(N%K,K-(N%K))
else:
    ans=min(N,K-N)

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,K= list(map(int,input().split()))

a = N//K
b = min(N%K,(a+1)*K-N)

print(b)

Submission status: Accepted

Solution code:
x, K = map(int, input().split())
while True:
    p = x
    if x > K:
        x %= K
    else:
        x = abs(K - x)
    if p <= x:
        print(p)
        break

Submission status: Accepted

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

Submission status: Accepted

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

a=N-K*(N//K)
b=abs(N-K*(N//K+1))

#print(a)
#print(b)
print(min(a,b))

Submission status: Accepted

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

new_N = N % K

while(new_N>(K/2)):
    x = abs(new_N-K)
    new_N = x

print(new_N)

Submission status: Accepted

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

Submission status: Accepted

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


if N>K:
    N=N % K

while True:
    if abs(N - K) < N:
        N = abs(N - K)
    else:
        print(N)
        exit()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
flag=True
tmp=n-n//k*k
cnt=k

while(flag):
    if tmp > abs(tmp-k) :
        tmp=abs(tmp-k)
        cnt+=1
    else:
        flag=False
        print(tmp)

Submission status: Accepted

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

Submission status: Accepted

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


if n>k:
    large=(n-(k*(n//k-1)))-k
    small=k-(n-k*(n//k))
    #print(large,small)
    print(min(large,small))
elif n==k:
    print(0)
else:
    print(min(n,abs(n-k)))

Submission status: Accepted

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

amari = N%K

if amari > K//2:
    print(abs(amari - K))
else:
    print(abs(amari))

Submission status: Accepted

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

n %= k

if 2 * n > k:
  print(k - n)
else:
  print(n)

Submission status: Accepted

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

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:
import math
n, k = map(int,input().split())
n = n % k
if n > k / 2:
  print(-(n - k))
else:
  print(n)

Submission status: Accepted

Solution code:
import math
n,k = map(int,input().split())
x = n % k
list = [x]


while 1:
  x = abs(x - k)
  if(x in list):
    break
  list.append(x)
  
    
print(min(list))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if N%K==0:
  print(0)

elif K >= 2*N:
  print(N)

else:
  a=min(N%K,abs(N-K))
  b=min(a,abs(a-K))
  print(b)

Submission status: Accepted

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

while True:
    b = abs(a-k)
    if a < b:
        print(a)
        break
    else:
        a = b
        continue

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
#import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""


def main():
    N, K = map(int, input().split())
    if N > K:
        a = N % K
        b = abs(a - K)
        c = abs(b - K)
        print(min(b, c))
    else:
        b = abs(N - K)
        c = abs(b - K)
        print(min(b, c))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

x = N
d = N // K
x = N - K*d
while abs(x - K) < x:
  x = abs(x - K)
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def calc(N, K):
    N = N % K
    d = []
    while True:
        x = abs(N - K)
        if x in d:
            return min(d)
        else:
            d += [x]
            N = x


(N, K) = tuple([int(s) for s in input().split()])
print(calc(N, K))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if(n>k):
    if(n%k==0):
        print(0)
    else:
        print(min((n%k),(n//k+1)*k-n))
elif(n==k):
    print(0)
elif(n==0):
    print(0)
else:
    if(k%n==0):
        print(n)
    else:
        if(k-n)<=n:
            print(k-n)
        else:
            print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=[int(i) for i in input().split()]
if n <= k/2:
    print(n)
elif n % k == 0:
    print(0)
else:
    t1=n-(n//k*k)
    t2=abs(t1-k)
    print(min(t1,t2))

Submission status: Accepted

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

if n < k:
	print(min(n, k - n))
else:
	if n % k == 0:
		print(0)
	else:
		print((k - n) % k)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if N < K:
    print(min(N, abs(N-K)))
elif N == K:
    print(N-K)
else:
    cnt = N//K
    ans = abs(N - K*(cnt+1))
    ans2 = abs(N- K*cnt)
    print(min(ans,ans2))

Submission status: Accepted

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

Submission status: Accepted

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

dif = abs(N - K)
if N <= dif:
  print(N)
  exit()

N = N % K
while(True):
  dif = abs(N - K)
  if N <= dif:
    break
  N = dif

print(N)

Submission status: Accepted

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

A=N%K
B=K-A
print(min(A,B))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x,y= list(map(int, input().split()))
if x%y==0:
    print(0)
else:
    z=min((x%y),(y-(x%y)))
    print(z)

Submission status: Accepted

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

Submission status: Accepted

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

a = N % K
b = -(a - K)

print(min(a,b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8

            
def main():
    n, k = map(int, input().split())
    minv = n
    while True:
        sur = abs(minv - k) % k
        if sur >= minv:
            break
        minv = sur
        
    print(minv)
        
  
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = map(int, raw_input().split())
from fractions import gcd

if n % k== 0:
  print 0
else:
  mini = n
  s = set()
  while True:
    if n > k:
      n = n % k
    else:
      n = abs(n-k)
    mini = min(n, mini)
    if n in s:
      print mini
      break
    s.add(n)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
def f(x, k):
    d = abs(k-x)
    if d<x:
        return f(d, k)
    else:
        return x
x = N%K
print(f(x, K))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ret = min(res, k - res)
print(ret)

Submission status: Accepted

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

ans1 = n%k
ans2 = abs(ans1-k)

ans = min(ans1,ans2)


print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=N//K
plus=N-a*K
minus=(a+1)*K-N
if plus>=minus:
	print(minus)
else:
	print(plus)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
import numpy as np

#  = input()
#  = int(input())
N, K = map(int, input().split())
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

if N >= K:
    seimin = N - (N//K) * K
    print(min(seimin, abs(seimin-K)))
else:
    print(min(N, abs(N-K)))

Submission status: Accepted

Solution code:
from math import ceil


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

    r = n % k
    ans = min(n, r, abs(r - k))

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s = n % k

if s < k - s:
    print(s)
else:
    print(k - s)

Submission status: Accepted

Solution code:
def main():
    N,K = map(int,input().split())
    
    if N==K:
        ans = 0
    elif N<K:
        ans = min(-N+K,N)
    else:
        ans = N%K
        ans = min(ans, K-ans)

    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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


x = n//k
ans1 = abs(-k*x+n)
ans2 = abs(-k*(x+1)+n)

ans = min(ans1, ans2)

print(ans)

Submission status: Accepted

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

r = n % k

ans = 0

if abs(r - k) > r:
    ans = r
else:
    ans = abs(r - k)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if N > K:
    N = N - N // K * K

res = 1e18
s = set()
while True:
    res = min(res, K-N)
    if not N in s:
        s.add(N)
        N = K-N
    else:
        break

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
N,K = list(map(int, input().split()))
dup = [N]
N = N % K
M = N
while True:
    N = abs(N-K)
    if ( N < M ):
        M = N
    if N in dup:
        break
    else:
        dup.append(N)

print(M)

Submission status: Accepted

Solution code:


def solve():

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

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N,K=map(int,input().split())
a=1
if N>2*K:
    N=2*K+N%K
while a==1:
    if N>abs(N-K):
        N=abs(N-K)
    else:
        a=0
print(N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
[n,k]=list(map(int,input().split()))
ans1=abs(n-(n//k)*k)
ans2=abs(n-(n//k+1)*k)
ans=min(ans1,ans2)
print(ans)

Submission status: Accepted

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

  print(mod)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

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

if x > K:
    x -= K * (x//K)

y = abs(x-K)
print(min(x,y))

Submission status: Accepted

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

def f(x,k):
    return (abs(x-k) < x)

n = divmod(n,k)[1]

while(f(n,k)):
    n = abs(n-k)

print(n)

Submission status: Accepted

Solution code:
[N,K]=[int(item)for item in input().split()]
while (True):
    tmp = N - K
    if tmp>0:
        N = N%K
        tmp = N - K
    print(abs(tmp) if abs(tmp)<N else N)
    exit()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
list_A=[abs(N-K*(N//K)),abs(N-K*(N//K)-K)]
print(min(list_A))

Submission status: Accepted

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

if n % k == 0:
    print(0)
else:
    n = n - (k*(n // k))
    if n > abs(n - k):
        print(abs(n-k))
    else:
        print(n)

Submission status: Accepted

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

val = N%K
while True:
    bef = val
    val = abs(bef-K)
    if abs(val) >= abs(bef):
        break

print(abs(bef))

Submission status: Accepted

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

saA = N % K 
saB = K - saA

if saA < saB:
    print(saA)
else:
    print(saB)

Submission status: Accepted

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

if n % k == 0:
    ans = 0
else:
    x = n % k
    y = abs(k - x)
    ans = min(x,y)
print(ans)

Submission status: Accepted

Solution code:
import math

N,K = map(int,input().split())
a = N%K
b = abs(N-(N//K+1)*K)
print(min(a,b))

Submission status: Accepted

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

def aaa(a):
    x=n+(-k*(a-1))
    return x
if n<=k:
    print(min(n,abs(n-k)))
else:
    print(min(aaa(n//k + 1),abs(aaa(n//k + 2))))

Submission status: Accepted

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

n2 = n%k
if n2 > k/2:
    n2 = k - n2

print(n2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,K = map(int,open(0).read().split())
x = N % K
x1 = abs(K - x)
print(min(x,x1))

Submission status: Accepted

Solution code:
line = input().rstrip().split(' ')

n = int(line[0])
k = int(line[1])

t = n % k

ans = t
while True:
    t = t - k
    if t < 0:
        t *= -1

    if ans > t:
        ans = t
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
n=int(n)
k=int(k)
n = n % k
minimam = n
record=[]
for i in range(k):
  n=abs(n-k)
  if n < minimam:
    minimam = n
  record.append(n)
  if n in record:
    break
print(minimam)

Submission status: Accepted

Solution code:
import numpy as np

def main():
    n, k = map(np.int64, input().split(" "))
    #a1 = np.int64(np.floor(n // k))
    #a2 = np.int64(np.floor(n // k + 1))
    #b1 = abs(n - a1 * k)
    #b2 = abs(n - a2 * k)
    #print(a1, a2)
    #print(n - a1 * k, n - a2 * k)
    b1 = np.mod(n, k)
    b2 = np.mod(n, k) - k
    #print(b1, b2)
    ans = np.min([abs(b1), abs(b2)])
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
N, K = map(int, sys.stdin.readline().rstrip().split())
ans = min(N % K, abs(K - N % K))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from decimal import *
N,K = map(int,input().split())
ans = Decimal(N)%Decimal(K)
if K-ans > ans:
    print(Decimal(ans))
else:
    print(Decimal(K-ans))

Submission status: Accepted

Solution code:
def c161(n , k):

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

def main():
    n , k = map(int , input().split())
    print(c161(n , k))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split()]
N = N%K
if N<abs(N-K):
    print(str(N))
else:
    print(str(abs(N-K)))

Submission status: Accepted

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


a = N // K
b = N % K


if b == 0:
    print(0)
    exit()

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

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
if N%K==0:
    print(N%K)
elif N>=K:
    A=N//K
    B=N-(A*K)
    if B>=abs(B-K):
        print(abs(B-K))
    else:
        print(B)
else:
    if N>=abs(N-K):
        print(abs(N-K))
    else:
        print(N)

Submission status: Accepted

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

n = abs(n - ((n // k) - 1) * k)

ans = abs(n-k)
prev = 10**18 + 10
end = True
while end:
    ans = min(abs(ans-k), prev)
    if prev == ans:
        end = False
    prev = ans

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = 10**19
if n >= k:
    a=n//k
b=n%k
c=abs(b-k)
l=[n,b,c]
print(min(l))

Submission status: Accepted

Solution code:
# coding: utf-8

n, k = map(int, input().split())

n_k_amari = n % k
n_k_1 = k - n_k_amari

print(min(n, n_k_amari, n_k_1))

Submission status: Accepted

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

ans = N
if N%K==0 :
  print(0)
else:
  N = N%K
  while True:
      N = abs(N-K)
      if N < ans:
          ans=N
      elif N == ans or ans== 0:
          break
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = [int(x) for x in input().split()]
a, b = divmod(n, k)
print(min(b, k - b))

Submission status: Accepted

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