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: Wrong Answer

Solution code:
##161 C
N,M= map(int, input().split())
amari= N % M
if(amari<=(N/2)):
    print(amari)
else:
   amari = amari-M
   print(amari)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def replace(a,b):
    if a < abs(a - b):
        return a
    else:
        return replace(abs(a - b), b)
if K == 0:
    print(N)
elif N > K:
    print(replace(N%K, K))
else:
    print(N%K)

Submission status: Wrong Answer

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

def replace(a,b):
    if a < abs(a - b):
        return a
    else:
        return replace(abs(a - b), b)
if N > K:
    print(replace(N%K, K))
else:
    print(N%K)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
sFlag=True
N_min=min(N,K)
N_flag=0
while sFlag:
  N=abs(N-K)
  N_K=N-K
  if(N_K<0):
    N_flag=N_flag+1
  N_min=min(N_min,N)
  if(N_flag==2):
    sFlag=False
  if(min(N,K)==1):
    N_min=0
    sFlag=False
print(N_min)

Submission status: Wrong Answer

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

pre_value = n+1
tmp = n
a = n / k 
tmp = n - a*k 
while tmp < pre_value:
    pre_value = tmp
    tmp = abs( tmp - k)
    
print(pre_value)

Submission status: Wrong Answer

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

if abs(N-K) < N:
    if N >= K:
        if N % K != 0:
            if K/(N%K) > 1:
                print(K%(N%K))
            else:
                print(N % K)
        else:
            print(0)
    else:
        if K % N != 0:
            if N/(K%N) > 1:
                print(N%(K%N))
            else:
                print(K % N)
        else:
            print(0)
else:
    print(N)

Submission status: Wrong Answer

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

if abs(N-K) <= N:
    if N >= K:
        if N % K != 0:
            if K/(N%K) >= 1:
                print(K%(N%K))
            else:
                print(N % K)
        else:
            print(0)
    else:
        if K % N != 0:
            if N/(K%N) >= 1:
                print(N%(K%N))
            else:
                print(K % N)
        else:
            print(0)
else:
    print(N)

Submission status: Wrong Answer

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

if abs(N-K) < N:
    if N >= K:
        if N % K != 0:
            if K/(N%K) >= 1:
                print(K%(N%K))
            else:
                print(N % K)
        else:
            print(0)
    else:
        if K % N != 0:
            if N/(K%N) >= 1:
                print(N%(K%N))
            else:
                print(K % N)
        else:
            print(0)
else:
    print(N)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
num_list = list(map(int, input().split()))
#val = num_list[0] % num_list[1]

if num_list[0] <= num_list[1]:
  print(num_list[0])
elif num_list[0] % num_list[1] == 0:
  print(0)
else:
  val = str(num_list[0] / num_list[1])
  print(val[0])

Submission status: Wrong Answer

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

amari = n % k

if amari == 0:
    ans = k
else:
    
    if amari <= k/2:
        ans = amari
    else:
        ans = -(amari - k)

print(ans)

Submission status: Wrong Answer

Solution code:
# C - Replacing Integer
# https://atcoder.jp/contests/abc161/tasks/abc161_c

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

while 1 != 0:
    if N - K >= 0:
        N = abs(N - K)
    else:
        print(N)
        exit()

Submission status: Wrong Answer

Solution code:
# C - Replacing Integer
# https://atcoder.jp/contests/abc161/tasks/abc161_c

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

while 1 != 0:
    if N - K >= 0:
        N = abs(N - K)
        print(N)
    else:
        print((N))
        exit()

Submission status: Wrong Answer

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

if n < k :
  new_n = k - n
else:
  new_n = n - k
  
if new_n >= n:
  print(n)
else:
  if n < k and k % n == 0:
    print(0)
  elif n > k and n % k == 0:
    print(0)
  elif n < k and (k - n ) % 2 == 0:
    print(0)
  elif n < k and (k - n) % 2 == 1:
    print(1)
  elif n > k and (n - k ) % 2 == 0:
    print(0)
  elif n > k and (n - k ) % 2 == 1:
    print(1)

Submission status: Wrong Answer

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

if n < k :
  new_n = k - n
else:
  new_n = n - k
  
if new_n > n:
  print(n)
else:
  if n < k and k % n == 0:
    print(0)
  elif n > k and n % k == 0:
    print(0)
  elif n < k and (k - n ) % 2 == 0:
    print(0)
  elif n < k and (k - n) % 2 == 1:
    print(1)
  elif n > k and (n - k ) % 2 == 0:
    print(0)
  elif n > k and (n - k ) % 2 == 1:
    print(1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k and k - n > n :
  print(n)
elif n > k and (n- k) % 2 == 0:
  print(0)
elif n < k and (k - n ) % 2 == 0:
  print(0)
elif n > k and (n - k ) % 2 == 1:
  print(1)
else:
  print(1)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math as mt
N,K=map(int, input().split())
if abs(N-K)<N:
    a,b=max(N,K),min(N,K)
    print(a-b*(a//b))
else:
    print(N)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math as mt
N,K=map(int, input().split())
if abs(N-K)<N:
    print(N-K*(N//K))
else:
    print(N)

Submission status: Wrong Answer

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

if N%K==0:
    print(0)
else:
    x = abs(N-K)
    for i in range(10000):
        x = min(x,abs(x-K))
    print(x)

Submission status: Wrong Answer

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

if N%K==0:
    print(0)
else:
    for i in range(100000000):
        x = min(x,abs(x-K))
    print(x)

Submission status: Wrong Answer

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

if N%K==0:
    print(0)
elif K%N ==0:
    print(N)
else:
    for i in range(10000):
        x = min(x,abs(x-K))
    print(x)

Submission status: Wrong Answer

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

if N%K==0:
    print(0)
else:
    for i in range(10000):
        x = min(x,abs(x-K))
    print(x)

Submission status: Wrong Answer

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

x = abs(N-K)

if K==1:
    print(0)
elif N==0:
    print(0)
else:
    for i in range(1000000):
        x = min(x,abs(x-K))
    print(x)

Submission status: Wrong Answer

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

x = abs(N-K)

if K==1:
    print(0)
else:
    for i in range(1000000):
        x = min(x,abs(x-K))
    print(x)

Submission status: Wrong Answer

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

x = abs(N-K)

if K==1:
    print(0)
else:
    for i in range(10000):
        x = min(x,abs(x-K))
    print(x)

Submission status: Wrong Answer

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

if N==0:
    print(K)
elif K == 1:
    print(0)
elif N*2 == K or N == K*2:
    print(0)
else:
    print(fractions.gcd(N,K))

Submission status: Wrong Answer

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

if K==1:
    print(0)
else:
    print(fractions.gcd(N,K))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
import fractions
print(fractions.gcd(N,K))

Submission status: Wrong Answer

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

while True:
    if x > abs(x - k):
        x = abs(x - k)
        print("temp x{}".format(x))
    else:
        break
print(x)

Submission status: Wrong Answer

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

amari = N % K;

hu = -1* (amari - K);

max_N = max(amari,hu);

print(max_N);

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

amari = N % K;

hu = amari - K;

max_N = max(amari,hu);

print(max_N);

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
l = input().split()
l_int = [int(a) for a in l]
N = l_int[0]
K = l_int[1]
 
if K > N:
    print(N)
else:
    print(min(N % K, abs(K-(N%K))))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  while(len(str(n))>1):
    n = n//10
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  while(len(str(n))>1):
    n = n//2
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  while(len(str(n))>1):
    n = n//k
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))

Submission status: Wrong Answer

Solution code:
l = input().split()
l_int = [int(a) for a in l]
N = l_int[0]
K = l_int[1]

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

Submission status: Wrong Answer

Solution code:
l = input().split()
l_int = [int(a) for a in l]
N = l_int[0]
K = l_int[1]

if K > N:
    print(N)
else:
    print(N % K)

Submission status: Wrong Answer

Solution code:
l = input().split()
l_int = [int(a) for a in l]
N = l_int[0]
K = l_int[1]

def aoki(n, k):
    new_n = abs(n - k)
    return new_n

first = aoki(N, K)
second = 0
third = 0
while first != third :
    second = aoki(first, K)
    third = aoki(second, K)

print(second)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

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

# read data for n sequences.
temp = stdin.readline().split()
N, K = np.asarray(temp).astype(int)

# K==1の場合
if K == 1:
    print(0)
    
elif N <= K:
    print(N)
else:
    rem = N%K
    print(np.abs(rem - K))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

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

# read data for n sequences.
temp = stdin.readline().split()
N, K = np.asarray(temp).astype(int)

# K==1の場合
if K == 1:
    print(0)
    
elif N < K:
    print(N)
else:
    rem = N%K
    print(np.abs(rem - K))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np
############################################

# read data for n sequences.
temp = stdin.readline().split()
N, K = np.asarray(temp).astype(int)

# K==1の場合
if K == 1:
    print(0)
    
if N < K:
    print(N)
    
rem = N%K
print(np.abs(rem - K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def getget(n, k):
    if n >= k:
        n = n%k
    # n<k
    if n == 0:
        return 0
    else:
        r = k%n
        if r == 0:
            return n
        else:
            s = n-r
            return min([r, s])

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

while 0<N:
    N=N-K
    D=abs(N)

print(D)

Submission status: Wrong Answer

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

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

def euclid(num1,num2):
  if num1 < num2: num1,num2 = num2,num1
  while num2 > 0:
    r = num1%num2
    num1 = num2
    num2 = r
  return num1

print(euclid(n,k))

Submission status: Wrong Answer

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

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

def euclid(num1,num2):
  if num1 < num2: num1,num2 = num2,num1
  while num2 > 0:
    r = num1%num2
    num1 = num2
    num2 = r
  return num1

print(euclid(n,k))

Submission status: Wrong Answer

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

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

def euclid(num1,num2):
  if num1 < num2: num1,num2 = num2,num1
  while num2 > 0:
    r = num1%num2
    num1 = num2
    num2 = r
  return num1

print(euclid(n,k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


if NK[0] < NK[1]:
    print(NK[0])
elif NK[0] == 0:
    print(NK[0])
else:
    points = NK[0]//NK[1]
    value = NK[1]*points
    v1 = NK[0] - value
    v2 = abs(v1-NK[1])

    # print(value)
    # print(v1)

    if v1 > v2:
        print(v2)
    else:
        print(v1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
  n,k = (int(x) for x in input().split())
  min = n
  n = n % k
  while True:
    n = abs(n - k)
    if(n < min):
      min = n
    else:
      break

  print(min)

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

while True:
    m = abs(n - k)
    if k == 1:
        break
    elif n == 0:
        result = m
        break
    elif n <= k:
        result = min(n, m)
        break
    else:
        n = n % k

print(result)

Submission status: Wrong Answer

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

while True:
    m = abs(n - k)
    if k == 1:
        break
    elif n == 0:
        result = m
        break
    elif n < k:
        result = min(n, m)
        break
    else:
        n = n % k

print(result)

Submission status: Wrong Answer

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

while True:
    m = abs(n - k)
    if k == 1:
        break
    elif n == 0:
        result = m
        break
    elif n < k:
        result = min(n, m)
        break
    else:
        n = m

print(result)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

for i in range (10*19):
    if (abs(N-K) >= N ):
        print (N)
        break
    else:
        N= abs(N-K)

Submission status: Wrong Answer

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

for i in range (10*18):
    if (abs(N-K) >= N ):
        print (N)
        break
    else:
        N= abs(N-K)

Submission status: Wrong Answer

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

for i in range (N):
    if (abs(N-K) >= N ):
        print (N)
        break
    else:
        N= abs(N-K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
l = input()
m = l.split(" ")
n = int(m[0])
k = int(m[1])
if n > k:
  n = n//k
if n < k:
  n = n
print(n)

Submission status: Wrong Answer

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

list=[]


if N%K==0:
    print(0)
elif N%K==1:
    print(1)
elif N%K==2:
    print(2)
elif N%K==3:
    print(3)
else:
    while N not in list:
        list.append(N)
        N=abs(N-K)
    print(min(list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,K = inputMap()

	N = gcd(N, K)

	ans = N
	while True:
		tmp = abs(ans-K)

		if tmp < ans:
			ans = tmp
		else:
			break

	print(ans)

def gcd(a, b):
	if a > b:
		a, b = b, a

	while a > 0:
		a, b = b % a, a
	return b

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
B=int(N/K)
A=N-B*K
while A>K/2:
    A=min(abs(A-K),abs(A+K))
print(A)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
B=int(N/K)
A=N-B*K
while A>=K/2:
    A=min(abs(A-K),abs(A+K))
print(A)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

while  N > M:
  x = N - M
  x = abs(x)
  N = x
  
print(N)

Submission status: Wrong Answer

Solution code:
a=input().split(" ")
n=int(a[0])
k=int(a[1])
s=int(n/k)
t=s+1
i=n-k*s
j=k*t-n
if i>=j:
    print(j)
else:
    print(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
val = input().split()
N = int(val[0])
K = int(val[1])
min = N
list = [N]
calc = abs(min-K)
while calc not in list:
  list.append(calc)
  if calc < min:
    min = calc
  calc = abs(min-K)
print(min)
print(list)

Submission status: Wrong Answer

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

if abs(n%k) == 0:
  ans = 0
else:
  while n > k:
    n = abs(n - (n // k) * k)
  ans = min(abs(n-k),abs(abs(n-k)-n))

print(ans)

Submission status: Wrong Answer

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

if abs(n%k) == 0:
  ans = 0
else:
  while n > k:
    n = n - (n // k) * k
  ans = min(abs(n-k),abs(abs(n-k)-n))

print(ans)

Submission status: Wrong Answer

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

if n < k:   
    ans = min(abs(n-k),abs(abs(n-k)-n))
elif n == k:
    ans = 0
else:
    if abs(n%k) == 0:
      ans = 0
    else:
      ans = abs(n % k - k)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

while n >= k:
  n = n-k
print(n)

Submission status: Wrong Answer

Solution code:
data = input().split()
n = int(data[0])
k = int(data[1])
 
if n > k :
  res = k - n % k
else:
  res = min(abs(n - k), n, k - n % k)

print(res)

Submission status: Wrong Answer

Solution code:
data = input().split()
n = int(data[0])
k = int(data[1])

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

print(res)

Submission status: Wrong Answer

Solution code:
data = input().split()
n = int(data[0])
k = int(data[1])

if n > k :
  res = k - n % k
else:
  res = min(abs(n - k), n)

print(res)

Submission status: Wrong Answer

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

if n % k == 0:
  print(0)
  exit()
elif n < k:
  print(n)
  exit()

now = abs((n % k) - k)
while True:
  tmp = abs(now - k)
  if tmp < now:
    now = tmp
  else:
    break
  
print(now)

Submission status: Wrong Answer

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

if n % k == 0:
  print(0)
  exit()
elif n < k:
  print(n)
  exit()

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

Submission status: Wrong Answer

Solution code:
from sys import maxsize

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

print(n % k)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
array = []
if k == 1:
    array.append(0)
else:
    for i in range(0,3):
        n = abs(n-k)
        array.append(n)
min_n = min(array)
print(min_n)

Submission status: Wrong Answer

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

for i in range(10**18):
    if K == 0:
        break
    if K == 1:
        n = 0
        break;
    t = K - (n % K)
    if t == n or t > n:
        break
    n = t
    
print (n)

Submission status: Wrong Answer

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

pn = n
for i in range(10**18):
    if K == 1:
        n = 0
        break;
    t = K - (n % K)
    if t == n or t > n:
        break
    n = t
    
print (n)

Submission status: Wrong Answer

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

for i in range(10**18):
    t = K - (n % K)
    if t == n or t > n:
        break
    n = t
    
print (n)

Submission status: Wrong Answer

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

pn = n
for i in range(10**18):
    t = K - (n % K)
    print(t)
    if t == n or t > n:
        break
    n = t
    
print (n)

Submission status: Wrong Answer

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

pn = n
for i in range(10**18):
    if K == 1:
        n = 0
        break
    t = K - (n % K)
    print(t)
    if t == n or t > n:
        break
    n = t
    
print (n)

Submission status: Wrong Answer

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

if K > n:
    res = n
elif n % K == 0:
    res = 0
else:
    res = K

Submission status: Wrong Answer

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

if K == 1:
    print(0)
    exit()
while True:
    b = N
    N = abs(N - K)
    print(b, N)
    if N >= b:
        print(b)
        break

Submission status: Wrong Answer

Solution code:
import math

inputted = list(map(int, input().split()))
N = inputted[0]
K = inputted[1]

candidate1 = N - K * math.floor(N / K)
candidate2 = K - candidate1
answer = min(candidate1, candidate2)

print(answer)

Submission status: Wrong Answer

Solution code:
import math

inputted = list(map(int, input().split()))
N = inputted[0]
K = inputted[1]

answer = N - K * math.floor(N / K)

print(answer)

Submission status: Wrong Answer

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

answer = N % K

print(answer)

Submission status: Wrong Answer

Solution code:
#/usr/bin/env python

import sys

N, K = [int(x) for x in sys.stdin.readline().strip().split()]

mod = N % K

if mod < (K / 2):
    print(mod)
else:
    print(K - mod)

Submission status: Wrong Answer

Solution code:
#/usr/bin/env python

import sys

N, K = [int(x) for x in sys.stdin.readline().strip().split()]

mod = N % K

if mod < (2 / K):
    print(mod)
else:
    print(K - mod)

Submission status: Wrong Answer

Solution code:
val= input()
val_list=list(map(int,val.split()))
n=val_list[0]
k=val_list[1]
while True:
    n = abs(n-k)
    if n< k:
        break
print(n)

Submission status: Wrong Answer

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

if a > b:
    if a % b == 0:
        print(0)
        exit()
    else:
        print(min(int(a/b), b-int(a/b)))
        exit()

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

Submission status: Wrong Answer

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

if a > b:
    if a % b == 0:
        print(0)
        exit()
    else:
        print(1)
        exit()

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())

a = N - np.floor(N / K) * K
b = np.abs(N - np.ceil(N / K) * K)
print(np.minimum(a, b).astype(int))

Submission status: Wrong Answer

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

if a == 1 or b == 1:
    print(0)
    exit()
while True:
    x = abs(a-b)
    if x < a:
        a = x
    else:
        print(a)
        break

Submission status: Wrong Answer

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

if a == 1 or b == 1:
    print(1)
    exit()
while True:
    x = abs(a-b)
    if x < a:
        a = x
    else:
        print(a)
        break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[]
min=n

if n<=k:
    print (n)
elif k==1:
    print (0)
else:
    while n not in l:
        l.append(n)
        n=abs(n-k)
        if n<min:
            min=n
    print (min)

Submission status: Wrong Answer

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

if(N%2==0):
    if(K%2==0):
        print(0)
else:
    print(1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def res():
  if N == K:
    return 0
  elif N > K:
    return N % K if K - (N % K) > (N % K) else K - (N % K)
  else :
    return N - K if N - K > (N-K) - K else (N-K) - K

print(res())

Submission status: Wrong Answer

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

def res():
  if N == K:
    return 0
  elif N > K:
    return N % K if K - (N % K) > (N % K) else K - (N % K)
  else :
    return N

print(res())

Submission status: Wrong Answer

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

def res():
  if N == K:
    return 0
  elif N > K:
    return N % K if N - (N % K) > (N % K) else N - (N % K)
  else :
    return N

print(res())