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:
N,K = map(int, input().split())
num = []
num.append(N)

if N > K:
    s = N // K
    N -= (K * s)
min = N
if K == 1:
    print(0)
else:
    while True:
        if K == 1:
            print(0)
            break
        N = abs(N - K)
        if  (N in num)  == True:
            print(min)
            break
        else:
            num.append(N)
            if N < min:
                min = N

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

Submission status: Accepted

Solution code:
import sys


def main(argv):
    n, k = [int(i) for i in sys.stdin.readline().rstrip("\n").split(" ")]
    r = n % k
    print(min(r, abs(r - k)))
    return 0
    

if __name__ == "__main__":
    sys.exit(main(sys.argv))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num_list = [int(i) for i in input().split()]
min_list = []

if num_list[0] < num_list[1]:
    if (num_list[1] - num_list[0]) < num_list[0]:
        print(num_list[1] - num_list[0])
    else:
        print(num_list[0])
elif num_list[0] == num_list[1]:
    print(0)
elif num_list[1] % num_list[0] == 0:
    print(0)
else:
    kake_num = num_list[0] // num_list[1]
    check_num = num_list[0] - (num_list[1]*kake_num)
    min_list.append(check_num)
    min_list.append((num_list[1] - check_num))
    print(min(min_list))

Submission status: Accepted

Solution code:
NK = input("").split()

N = int(NK[0])
K = int(NK[1])

if N == K or K==1:
    print(0)

elif N > K:
    A = N % K
    if A > K-A:
        print(K-A)

    else:
        print(A)

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

Submission status: Accepted

Solution code:
a=list(map(int, input().split()))
def d(a,b):
    _1=a%b
    _2=b-(a%b)
    if _1<_2:
        print(_1)
    else:
        print(_2)
d(a[0],a[1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
n, k = map(int, input().split())
if n == k:
    min = 0
else:
    x=n%k
    y=k-x
    min = min(x,y)
# 出力
print(min)

Submission status: Accepted

Solution code:
import sys

N,K=map(int,input().split())
ZET=N
b=(10**18)
c=N
b1=N
for i in range(int(b)):
  if K==1:
    b1=0
    break
  if N>10**17 and K<10**9:
    b1=N%K
    c=abs(b1-K)
    if b1>c:
      b1=c
    break
  c=abs(b1-K)
  if int(b)<=c:
    break
  b1=c
  b=c
print(b1)

Submission status: Accepted

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

# if N < K:
#     print(N)
# else:
N = abs(N - (N // K + 1) * K)
if N == 0:
    print(0)
elif abs(N-K) < N:
    print(abs(N-K))
else:
    print(N)

Submission status: Accepted

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

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

else:
    while True:
        if N // K == 0:
            print(N % K) if N % K < abs(N - K) else print(abs(N - K))
            break;

        else:
            N = abs(N - K * (N//K))

Submission status: Accepted

Solution code:
nk = input().split(" ")

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

kf = n % k
ki = abs(kf-k)

print(min(kf, ki))

Submission status: Accepted

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

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

if N > K:
  print(min(N%K,K-(N%K)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n,k=map(int,input().split())
if n%k==0:
    print(0)
    sys.exit()
if n%k<abs(k-n%k):
    print(n%k)
else:
    print(abs(k-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())
r=n%k
if -(r-k)>=r:
    print(r)
else:
    print(-r+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 math
first_input = list(map(lambda x:int(x), input().split(" ")))
#second_input = list(map(lambda x:int(x), input().split(" ")))

N = first_input[0]
K = first_input[1]

#print(math.floor(N // K))
#print(K*math.floor(N // K))
if N>K:
    first_min_N = N - K*math.floor(N // K)
else:
    first_min_N = N

print(min(first_min_N,abs(first_min_N-K)))

Submission status: Accepted

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

Submission status: Accepted

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


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

    if 2 * N < K:
        return N

    R = N % K
    
    if R < abs(R - K):
        return R
    else:
        return abs(R - K)


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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
q, mod = divmod(n, k)
if mod > (k//2):
        print(abs(k - mod))
else:
        print(mod)

Submission status: Accepted

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

ans = N

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = N - (N // K) * K
ans1 = abs(x - K)
ans2 = abs(ans1 - K)
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
# n, k = map(int, "2 6".split())
q = n // k #答え
mod = n % k #あまり

if q == 0:

    while True:
        nc = n
        n = n - k
        if n <= 0:
            if abs(n) < nc:
                print(abs(n))
                break
            else:
                print(nc)
                break
else:
    n1 = abs(n - (q * k))
    n2 = abs(n - ((q + 1) * k))
    if n1 < n2:
        print(n1)
    else:
        print(n2)

Submission status: Accepted

Solution code:
import numpy as np
N,K=map(int,input().split())
a=np.abs(N%K)
print(min(a,np.abs(a-K)))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
if N==0 or K==1:
    print(0)
else:
    a=N//K
    N=N-a*K
    for i in range(10**18):
        if N>abs(N-K):
            N=abs(N-K)
        else:
            print(N)  
            break

Submission status: Accepted

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

Submission status: Accepted

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

a=N//K


if a>=1:
    s=N-K*a
    if s>=abs(s-K):
        print(int(abs(s-K)))
    else:
        print(int(s))

elif a==0 and N>=(abs(N-K)):
    print(int(abs(N-K)))

else:
    print(int(N))

Submission status: Accepted

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

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

if a < b:
    if b - a < a:
        print(b-a)
        exit()
    else:
        print(a)
        exit()

k = a % b
if abs(k - b) < k:
    print(abs(k - b))
else:
    print(k)

Submission status: Accepted

Solution code:
import math

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

while True:
    if N>K:
      N = abs(N%K)
    if abs(N-K) < N:
        N = abs(N-K)
        continue
    else:break

print(N)

Submission status: Accepted

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

if (N%K) <=(K/2):
	x=N%K
else:
	x=K-N%K
	
print(x)

Submission status: Accepted

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

def absolute(n,k):
    return abs(n-k)

x = absolute(n, k)
if n % k == 0:
    print(0)
elif n < x:
    print(n)
else:
    while absolute(x, k) < x:
        x = x % k
        x = absolute(x, k)

    print(x)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = N / K
r = N % K
print(min([K-r, r]))

Submission status: Accepted

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

def diff(n,k):
  diff = 0
  if n > k:
    diff = n-k*(n//k)
  elif n < k:
    diff = k-n
  else:
    diff = 0
  return diff

ans = [n]

for i in range(100):
  ans.append(diff(ans[i],k))

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
NK = list(map(int, input().split()))
N = NK[0]
K = NK[1]
posiVal = N - N//K * K
negaVal = N - (N//K + 1) * K
if posiVal < -negaVal:
    print(posiVal)
else:
    print(-negaVal)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while N >= math.ceil(K / 2):
                N = abs(N-K*(N//K+1))
        print(N)

Submission status: Accepted

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

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

else:
    while True:
        if N // K == 0:
            # print('N % K = ' + str(N % K) + ', abs(N - K) = ' + str(abs(N - K)))
            print(N % K) if N % K < abs(N - K) else print(abs(N - K))
            break;

        else:
            # print('N % K = ' + str(N % K) + ', abs(N - K) = ' + str(abs(N - K)))
            N = abs(N - K * (N//K))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
amari=n%k
mainasu=n-(n//k+1)*k
if amari<abs(mainasu):
  print(amari)
else:
  print(abs(mainasu))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(n)

Submission status: Accepted

Solution code:
def solve(n, k):
    occurence = set()

    diff = n % k
    n = diff
    
    if diff == 0 or diff == 1:
        print(diff)
        return
    else:
        while n not in occurence:
            if k == 1:
                print(0)
                return

            occurence.add(n)

            n = abs(n - k)

            if n == 0:
                print(n)
                return

    min_value = min(occurence)
    print(min_value)


if __name__ == '__main__':
    nk = input().split()

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

    solve(n, k)

Submission status: Accepted

Solution code:
#import math
N, K = list(map(int, input().strip().split()))

def gcd2(a, b):
  while True:
    if a > b:
      if a % b == 0:
        return 0
      else:
        #a = a-b
        a = a % b
    elif a == b:
      return 0
    else:
      return min(a, abs(a-b))

ans = gcd2(N, K)
if ans == K:
  print(0)
else:
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 初期入力
N,K = (int(x) for x in input().split(" "))

#
zeroika = 0
if K <N:
    zeroika = N//K
else:
    zeroika = 1
N1 =abs(N-K*zeroika) #N1がマイナスになって初めてABS、そこまではKの倍数単調に減る
while N1<N:
    N =N1
    N1 =abs(N-K)
    #print(N1,end=" ")
print(N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    N, M = map(int, input().split())
    
    if N % M == 0:
        return 0
    
    a = N % M

    return min(a, abs(a-M))

print(solve())

Submission status: Accepted

Solution code:
# coding: utf-8

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

if n==k:
    print(0)
    
############################
if n < k:
    for _ in range(2):
        abs_val = abs(n-k)
        
        if abs_val<=n:
            n=abs_val
            
    print(n)

############################
if n > k:
    z = n//k
    n = n - k*z
    
    for _ in range(4):
        abs_val = abs(n-k)
        
        if abs_val<=n:
            n=abs_val
            
    print(n)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a,b= map(int, input().split())
min = 1000000000000000000000000000000


if a <= b:
  while min > abs(a-b):
    min = abs(a-b)
    a   =abs(a-b)
else :
  a = a%b
  while min > abs(a-b):
    min = abs(a-b)
    a   = abs(a-b)
  
print(min)

Submission status: Accepted

Solution code:
n = list(map(int,input().split()))
a = n[0]
b = n[1]
c = a%b
d = b - c
if d<c:
    print(d)
else:
    print(c)

Submission status: Accepted

Solution code:
# problem C

input_d3 = list(map(int, input().split()))

N = input_d3[0]
K = input_d3[1]

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
# a=sorted(a)
l=[]
c=abs(n-m)
if n%m==0:
    c=0
    print(c)
else:
    c=n%m
    d=abs(m-c)
    l.append(c)
    l.append(d)
    l=sorted(l)
    print(l[0])

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
count=10**18 
if abs(n)>abs(k):
  a=n//k
  n=abs(n)-a*k
  
  
for i in range(10*18):
  n=abs(n-k)
  if count>n:
    count=n
  if count==0:
    print(0)
    exit()
    
print(count)

Submission status: Accepted

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


a = N % K
print(a) if a < abs(a-K) else print(abs(a-K))

Submission status: Accepted

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


if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

    else:
        if abs((N % K) - K) < (N % K):
            print(abs((N % K) - K))

        else:
            print(abs(N % K))

Submission status: Accepted

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

syou = n//k

zet = abs(n-k*syou)
if zet == 0:
  print(zet)
else:
  zet1 = abs(n-k*(syou+1))
  if zet > zet1:
    print(zet1)
  else: 
    print(zet)

Submission status: Accepted

Solution code:
#
N, K = map(int,input().split())
N  %= K
min = N
for i in range(10**6):
    N = abs(N-K)
    if min > N:
        min = N
    if N == 0:
        min = 0
        break
print(min)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

n = N % K 

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N,K = LI()

count = N//K

N = N-count*K
print(min(N,abs(N-K)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def func(x,k):
    x = x%k
    if abs(x-k)>x:
        return x
    return func(abs(x-k),k)

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

Submission status: Accepted

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

x = [n]

if n > k:
    n = n%k


while True:
    n = abs(n - k)
    if n >= x[-1]:
        break
    x.append(n)


#print(x)

print(min(x))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
L = []
if n > k:
  n = n - k*(n//k)
 
if n*2 <= k:
  print(n)
else:
  print(k-n)

Submission status: Accepted

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

elif n%k==0:
    print(0)
else:

    a = n // k
    b= (a + 1) * k - n
    c = n - a * k
    t=n-k
    p=k//t
    r=k-p*t
    print(min(b,c,r))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


a=n%m

if a>abs(a-m):
    a=abs(a-m)


print(a)

Submission status: Accepted

Solution code:
import math

data = input().split()

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

div = n//k
n2 = n - div*k
if div==0:
	n2 += k

ans = []
for i in range(2):
	ans.append(abs(n2-k))
	n2 = abs(n2-k)

print(min(ans))

Submission status: Accepted

Solution code:
"""
N K
操作: x を x と K の差の絶対値で置き換える
整数 N の初期値が与えられます。この整数に上記の操作を 0 回以上
好きな回数行った時にとりうる N の最小値を求めてください。
"""
import math
n,k=map(int,input().split())
x0=n
xi=n
#print("xi: "+str(xi))
xi1=abs(xi-k)
#print("xi1: "+str(xi1))
leap=math.floor(xi1/k)-3
xi1,xi=abs(xi1-k*leap),xi1
i=0
while ((i==0)|(xi1 < xi)) :
  #print(i)
  """
  if(math.floor(i/100000)==0):
    print(xi1,xi)
  """
  xi1,xi=abs(xi1-k),xi1
  i=i+1
#print("xi1: "+ str(xi1))
print(xi)
"""
"""  

"""
#b=sorted(a, reverse=True)
#print(sum(b))
if b[m-1]>= (sum(b) /(4*m)) :
  print("Yes")
else:
  print("No")

x,y,z=map(int,input().split())
a,b,c=x,y,z
a,b=b,a
a,c=c,a
print(a,b,c)
"""

Submission status: Accepted

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

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

Submission status: Accepted

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

else:
    
    x = (n+k-1)//k
    y = (n-k-1)//k
    if y < 0:
        y += 1
    if x < 0:
        x+= 1
    a =abs(x*k-n)
    b = abs(y*k-n)
    if a < b:
        print(a)
        
    else:
        print(b)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if N ==0 or K==1 or N == K:
  print(0)
else:
  while N>abs(N-K):
    N = abs(N%K-K)
  print(N)

Submission status: Accepted

Solution code:
N,K=(int(i) for i in input().split())
N=N-N//K*K
M=N-K
print(min(N,abs(M)))

Submission status: Accepted

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

r = []
n = n % k
if n == 0:
    print(0)
    exit()
while True:
    n = abs(n - k)
    r.append(n)
    if r.count(n) >= 2:
        break

print(min(r))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#N, M = map(int, input().split())
#%A = list(map(int, input().split())

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

a=N % K

if a > K/2:
  a = K - a
  
print(a)

Submission status: Accepted

Solution code:
import math
n, k = input().strip().split(' ')
n, k = [int(n), int(k)]

def fx(m):
    a = m - k 
    if(m - k) < 0:
        a  = - (m - k)
    if a >= m:
        return m
    return fx(a)

l = n
if n>= k:
    i = n//k
    l = n - k * i

ans = fx(l)
print(ans)

Submission status: Accepted

Solution code:
N,K = list(map(int, input().split()))
 
ans = []
a = N
 
ans2 = N % K
ans1 =  (N % K ) - K
if abs(ans2) > abs(ans1):
    print(abs(ans1))
else:
    print(abs(ans2))

Submission status: Accepted

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

nums = []

def calc_num(n, k):
    if n <= k:
        nums.append(min(n, k-n))
        return
    
    n = n % k
    if n in nums:
        return
    elif n == 0:
        nums.append(0)
        return
    else:
        nums.append(n)
        calc_num(n, k)
        
calc_num(N, K)
print(min(nums))

Submission status: Accepted

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

Submission status: Accepted

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

print(min(ans, ans2))

Submission status: Accepted

Solution code:
import sys
N, K = map(int, input().split())
first = N
list_a = []

if N % K == 0:
    print(0)
    sys.exit()
else:
    X = N // K
    N = N - (K * (X+1))

second = N + K

N = abs(N)

list_a.append(N)
list_a.append(second)
list_a.append(first)

list_a = sorted(list_a)

print(list_a[0])

Submission status: Accepted

Solution code:
from fractions import gcd 
n,k = map(int,input().split())
a=k*(n//k)
if n >= k and n%k == 0:
    print(0)
elif n < k:
    print(min(abs(n-k),n))
else:
    print(min(abs(abs(n-a)-k),abs(n-a)))

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
if N > K and N//K>2:
	a = N//K
	N= N-K*(a-2)

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

print(N if N==0 else min(N,T) )

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
Nm = N % K
ans = Nm
A = [Nm]
while 1:
    if ans == 0:
        break
    elif abs(Nm-K) in A:
        break
    else:
        if abs(Nm-K) < ans:
            ans = abs(Nm-K)
        A.append(abs(Nm-K))
        Nm = abs(Nm-K)
    
print(ans)

Submission status: Accepted

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

    if N == 0 or N == K :
        print("0")
    elif N > K :
        ans = N % K
        #print(ans)
        N = ans
        for i in range(0 , N + 1) :
            value_diff_abs = N
            N = abs(N - K)
            #print(str(value_diff_abs))
            if value_diff_abs < N :
                print(str(value_diff_abs))
                break
            elif value_diff_abs == N :
                print(str(value_diff_abs))
                break
    else :
        for i in range(0 , N + 1) :
            value_diff_abs = N
            N = abs(N - K)
            if value_diff_abs < N :
                print(str(value_diff_abs))
                break
            elif value_diff_abs == N :
                print(str(value_diff_abs))
                break


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a = n % k
if a == 0:
    print(0)
    exit()
k_1 = k / 2
if a > k_1:
    ans = k - a
elif a < k_1:
    ans = a
print(ans)

Submission status: Accepted

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

if K == 0:
  print(N)
else:
  if N >= K:
    kotae1 = abs(N - ((N // K) * K))
    kotae2 = abs(N -((N // K) + 1) * K)
    if kotae1 > kotae2:
      print(kotae2)
    else:
      print(kotae1)

  else:
    keisan = abs(N - K)
    if keisan > N:
      print(N)
    else:
      print(keisan)

Submission status: Accepted

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

n = n % k
if k - n < n:
    n = k - n

print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

ansa = n % k
ansb = abs(ansa - k)

ans = min(ansa, ansb)

print(ans)

Submission status: Accepted

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

x = abs(N -(N//K)*K)
y = abs(N - (N//K + 1)*K)

if x < y:
    print(x)
else:
    print(y)

Submission status: Accepted

Solution code:
import sys

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

an = int(n%k)
if  an > (abs(an-k)):
    print(abs(an-k))
    sys.exit()

print(an)

Submission status: Accepted

Solution code:
import sys
# sys.stdin = open('input.txt')

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

n = n - (k * div)
ans = min(n, abs(n-k))
print(ans)

Submission status: Accepted

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

if n % k == 0:
    ans = 0
    print(ans)
    exit()
else:
    n = n % k
    for i in range(10000000000000000000):
        res = abs(n - k)
        if n <= res:
            ans = n
            print(ans)
            exit()
        else:
            n = res

Submission status: Accepted

Solution code:
list=list(map(int,input().split()))
x=list[0]
k=list[1]
if list[0]>=list[1]:
  x=x%k
z=abs(k-x)
if z >=x:
  print(x)
else:
  print(z)

Submission status: Accepted

Solution code:
import sys

N,K = map(int, sys.stdin.readline().strip().split())

if K == 1:
    print(0)
    sys.exit()
if N == K:
    print(0)
    sys.exit()

d = []

d.append(N % K)
while True:
    d.append(abs(d[-1] - K))
    if d[-1] > d[-2]:
        print(d[-2])
        sys.exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
if N > K:
    if N % K == 0:
        print(0)
    else:
        print(K % (N % K))
elif N <= K:
    if K < 2 * N:
        print(K - N)
    else:
        print(N)

Submission status: Accepted

Solution code:
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif n <= abs(n-k):
    print(n)
  else:
    ans = n % k
    if ans > 2:
      while 1:
        hi = abs(ans-k)
        if hi < zen:
          zen = hi
          ans = hi

        else:
          print(ans)
          exit()
    else:
      print(ans)
    
      
if __name__ == '__main__':
  main()

Submission status: Accepted

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

if N > K:
    N = N % K

while 1:
    a = abs(N - K)
    if a >= N:
        break
    else:
        N = a

print(N)

Submission status: Accepted

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

if N <= K:
    ans = abs(N-K)
    lis = []

    for i in range(3):
        ans = abs(ans - K)
        lis.append(ans)
    
    print(min(lis))
    sys.exit()

if N % K != 0:
    print(abs(N-(((N//K) + 1)*K)))
if N % K == 0:
    print(abs(N-((N//K)*K)))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
if K == 1 or N == 0 or N % K == 0:
    print(0)
elif N*2 < K:
    print(N)
elif N < K < 2*N:
    print(K-N)
else:
    tmp = abs(N%K-K)
    print(min(N%K,tmp))

Submission status: Accepted

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

positive = N % K
negative = K - positive
answer = min(positive, negative)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

min_value = 10**18

def check(x):
    global min_value

    if x % K == 0:
        min_value = 0
        return
    
    if x < K:
        if K % x == 0:
            min_value = x
            return
        else:
            if abs(x - K) == min_value:
                return
            else:
                if min_value > abs(x - K):
                    min_value = abs(x - K)
                check(abs(x - K))
    else:
        min_value = x % K
        check(x % K)

check(N)

print(min_value)

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:
    if n % k == 0:
        print(0)
    else:
        ans = min(n % k, abs(n % k - k))
        print(ans)
else:
    ans = min(n, abs(n - k))
    print(ans)

Submission status: Accepted

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