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 sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

n, k = MAP()
t = n // k
ans = min(abs(n - k * t), abs(n - k * (t + 1)))
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
c = a%b
print(min(c,b-c))

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K=MI()
    N%=K
    ans = min(abs(N-K), N)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return map(int, sinput())

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

Submission status: Accepted

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

ans1 = N%K
ans2 = -(ans1-K)

if ans1 >= ans2:
  ans = ans2
else:
  ans = ans1

print(ans)

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n,k=map(int,input().split())
print(min(n%k,k-n%k))

Submission status: Accepted

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

while abs(N-K) < N:
    if N > K:
        N %= K
        N = min(N, abs(N-K))
    else:
        N = K%N

print(N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#template
from collections import Counter
def inputlist(): return [int(k) for k in input().split()]
#template
N,K = inputlist()
m = N%K
print(min(m,K-m))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

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

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())
    print(min((N%K, K - N%K)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N,K=MI()
    a=N%K
    b=K-N%K
    print(min(a,b))

main()

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
def replace(n,k):
    n = min(n,abs(n - k))
    if n <= abs(n - k):
        return n,k
    else:
        return replace(n, k)
if k == 1:
    print(0)
else:
    n1, k1 = replace(n,k)
    print(n1)

Submission status: Runtime Error

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

if N==K:
  print(0)

elif N<K:
  lis1=[N,abs(N-K)]
  print(min(lis))
  
else:
  lis2=[N%K,abs(N%K-K)]
  print(min(lis2))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import numpy
n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print(min(n, m-n))

Submission status: Runtime Error

Solution code:
import numpy

n = numpy.int64(n)
m = numpy.int64(m)
n = n % m
print(min(n, m-n))

Submission status: Runtime Error

Solution code:
from ctypes import c_longlong as ll

n = ll(input(""))
m = ll(input(""))
n= n % m
print(min(n, m-n))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)

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

def solve(n,k):
  cand = abs(n-k)
  if n <= cand:
    return n
  else:
    return solve(cand,k)

print(solve(N,K))

Submission status: Runtime Error

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

def solve(n,k):
  cand = abs(n-k)
  if n <= cand:
    return n
  else:
    return solve(cand,k)

print(solve(N,K))

Submission status: Runtime Error

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

if N <= inc :
    print(N)
else :
    print(N%inc)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
num = sum(a)
cnt = 0
for i in range(n):
    if a[i] < num/(4*m):
        continue
    else:
        cnt += 1
if cnt >= m:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

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

while N <= int(K/2):
  N = min([abs(N-K), N % K])

print(N)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import ceil
def main():
    n, k = map(int, input().split())
    if k == 1:
        print(0)
        sys.exit()
    r1 = k % n
    r2 = abs(r1 - k)
    r = min(r1, r2)
    print(r)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
n, k = map(int, sys.stdin.buffer.read().split())
rem = n % k
print(min(rem, k - rem)

Submission status: Runtime Error

Solution code:
import sys
n, k = map(int, sys.stdin.buffer.read())
print(n % k)

Submission status: Runtime Error

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

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

remainder = K % x if K >= x else x % K
  
if remainder > x // 2:
  print(abs(remainder - x))
else:
  print(remainder)

Submission status: Runtime Error

Solution code:
n=int(input(""))
k=int(input(""))
s=n%k
ss=k-s
if(s<ss):
    print(s)
else:
    print(ss)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
setlist = set()
tmp = N
ans = float('inf')
while True:
  tmp = abs(N-K)
  ans = min(tmp,ans)
  if tmp in setlist:
    break
  setlist.add(tmp)
return ans

Submission status: Runtime Error

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

for i in range(200):
    if X - i not in p:
        print(X - i)
        break
    if X + i not in p:
        print(X + i)
        break

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if n == k:
    print(0);
elif n > k:
    n = n - k;
    a = [n];
    i = 0;
    while True:
        n = n-k;
        if n < 0:
            break;
        a.append(n);
        i += 1;
else:
    n = k - n;
    a = [n];
    i = 0;
    while True:
        n = n-k;
        if n < 0:
            break;
        a.append(n);
        i += 1;

if a[i] <= (-1)*n:
    print(a[i]);
else:
    print((-1)*n);

Submission status: Runtime Error

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

cnt = 0
ans = N

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

ans2 = N
ans1 = K % N
if K > N:
    ans2 = N - ( K % N )
    ans1 = N
else:
    ans2 = K - ( N % K )
    ans1 = N % K

if ans1 < ans2:
    print(ans1)
else
    print(ans2)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
print(min(N%K, K-N%K))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
list = []
if K == 1:
  print(0)
else:
  for i in range(N):
    N = abs(N-K)
    list.append(N)
    list = sorted(list)
  print(list[0])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
N = N%K
min_num = 0
while True:
  if (N==0):
    min_num = N
    break
  elif N in history:
    min_num = min(history)
    break 
  history.append(N)
  N = abs(N-K)

print(min_num)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
N = N%K
while True:
  if (N==0):
    min_num = N
    break
  elif N in history:
    min_num = min(history)
    break 
  history.append(N)
  N = abs(N-K)

print(min_num)

Submission status: Runtime Error

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

m = math.floor(n/k)
n = n-m*k

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

Submission status: Runtime Error

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

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

if ans1 > ans2:
  print(ans2)
else:
  print(ans1)

Submission status: Runtime Error

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

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

## Logic

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
# 商品iをそれぞれN個入力
num=[int(i) for i in input().split()]
# 降順で並び替え
num=sorted(num,reverse=True)
# 総得票数
sum_num=sum(num)
# m-1番目つまり人気商品の中の一番人気がない商品が
# 総得票数の1/4Mは未満は人気商品と言えないNo
if num[m-1]<sum_num/(4*m):
    print("No")
else:
    print("Yes")

Submission status: Runtime Error

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

a = []
for i in range(n):
    n = abs(n - k)
    a.append(n)
    
print(min(a))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

if x>k:
    print(x%k)
else:
    print(k%x)

Submission status: Runtime Error

Solution code:
import sys
def func_data(N,K):
    if abs(N-K) <= N:
        N = func_data(abs(N-K),K)
    return N
N,K = map(int,input().split())
sys.setrecursionlimit(10**15)
if K == 1:
    print(0)
else:
    ans = func_data(N,K)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
def func_data(N,K):
    if abs(N-K) <= N:
        N = func_data(abs(N-K),K)
    return N
N,K = map(int,input().split())
sys.setrecursionlimit(sys.maxsize)
if K == 1:
    print(0)
else:
    ans = func_data(N,K)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
def func_data(N,K):
    if abs(N-K) <= N:
        N = func_data(abs(N-K),K)
    return N
N,K = map(int,input().split())
sys.setrecursionlimit(sys.maxsize)
if K == 1:
    print(0)
else:
    ans = func_data(N,K)
    print(ans)

Submission status: Runtime Error

Solution code:
def func_data(N,K):
    if abs(N-K) <= N and N >= 0:
        N = func_data(abs(N-K),K)
    return N
N,K = map(int,input().split())
ans = func_data(N,K)
print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
def sa(x):
    if x < 1:
        return abs(x)
    return abs(sa(x-k)-k)
print(sa(n))

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
def sa(x):
    if x < 0:
        return abs(x)
    return abs(sa(x-k)-k)
print(sa(n))

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
def sa(x):
    print(abs(x))
    if x < 0:
        return abs(x)
    return abs(sa(x-k)-k)
print(sa(n))

Submission status: Runtime Error

Solution code:
# import sys
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
# import copy
# import bisect
# import numpy as np
# from scipy.special import comb

# def my_input(): return sys.stdin.readline().rstrip()
# sys.setrecursionlimit(100000)
# INF = 1001001001001
# MOD = 1000000007

def main():

    N = int(input())
    A, B = map(int, input().split())
    L = list(map(int, input().split()))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
def resolve(n, k, c):
    if abs(n - k) < n:
        n = abs(n - k)
        c += 1
        return resolve(n, k, c)
    else:
        return n
if __name__ == "__main__":
    n, k = [int(_) for _ in input().split()]
    c = 0
    n = resolve(n, k, c)
    print(n)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

t1 = abs(N - K)
t2 = K // t1

if t2 > N:
    print(N)
else:
    print(t2)

Submission status: Runtime Error

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

t1 = abs(N - K)
t2 = K // t1

if t2 > N:
    print(N)
else:
    print(t2)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]
 
sys.setrecursionlimit(1000000)
mod = 1000000007
 
def solve():
    n,k = map(int,input().split())
    if n%k == 0:
      print(0)
    else:
      print(math.gcd(n,k))
    
    return
 
#Solve
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

if(n%k==0):
    print(0)
else:
    print(gcd(n,k))

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = map(int,input().split())
    if n%k == 0:
      print(0)
    else:
      print(math.gcd(n,k))
    
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

def replace(x):
    newx = abs(x-K)
    if newx < K/2:
        print(newx)
    else:
        replace(newx)

replace(N)

Submission status: Runtime Error

Solution code:
from math

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

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

Submission status: Runtime Error

Solution code:
from math import gcd

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = min(n % k,k % n)
t = s
for _ in range(t):
  if abs(s-k)<s:
    s = abs(s-k)
print(s)

Submission status: Runtime Error

Solution code:
# encoding:utf-8
N, K = LI()


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

Submission status: Runtime Error

Solution code:
import copy
N, K = map(int, input().split())
a = copy.deepcopy(N)

while abs(N - K) < N:
    count = 0
    N = abs(N - K)
    count += 1
    
if count == 0:
    print(a)
else:
    print(N)

Submission status: Runtime Error

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

while abs(N - K) < N:
    count = 0
    N = abs(N - K)
    count += 1
    
if count == 0:
    print(a)
else:
    print(N)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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


if (N < K):
    ans = N
#elif(N % K == 0):
 #   ans = 0
else:
    if (N % K == 0):
        ans = 0
    else:
    N = N % K
    if(N <= abs(N-K)):
        ans = N
    else:
        ans = abs(N-K)

print(ans)

Submission status: Runtime Error

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

m=abs(n-k)

mm=k%m
mmm=k-mm

if mmm >= mm:
    ans = mm
else:
    ans = mmm

print(str(ans))

Submission status: Runtime Error

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

m=abs(n-k)

mm=k%m

if k == 1:
    ans = 0
elif k >= mm:
    ans = mm
else:
    ans = k

print(str(ans))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:

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

print(min(amari,abs(amari-k)))

Submission status: Runtime Error

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

print(amari,abs(amari-k))

Submission status: Runtime Error

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

if K == 1:
    print(0)

else:
    shou, amari_1 = divmod(N, abs(N-K))
    N_min_1 = N - (shou * abs(N-K))
    N_min_2 = abs(N_min_1 - abs(N-K))
    print(min(N_min_1, N_min_2))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K=list(map(int,input().split()))
ans=N
A=set()
while ans>0:
    A.add(ans)
    ans=ans-K
    A.add(ans)
print(min([abs(i) for i in A]))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
while n>=0:
  n=ans(n-k)
  if n-k<=0:
    print(n)
    break

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
while n>=0:
  n=ans(n-k)
  if n-k<=0:
    print(n)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
count = 0
lst =[n]
if n > k
  while n - k > 0 :
    n -= k
    count += 1
    lst.append(n)
  opponent = abs(lst[count] - k)  
  if lst[count] >= opponent:
    print(opponent)
  else:
    print(lst[count])
elif n < k:
  op = abs(n-k)
  if n >= abs(n-k):
    print(op)
  else:
    print(n)
else:
  print(0)

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
while(1):
    new_N = abs(N-K)
    if new_N >= N:
        print(N)
        break
    else:
        N=new_N

Submission status: Runtime Error

Solution code:
N = int(input())
nn = N
K = int(input())
n = 1
st = []
m = min(N, K)
while N >= abs(nn - n * m) :
    N = abs(N - K)
    n = n + 1
print(min(m, N))

Submission status: Runtime Error

Solution code:
N = int(input())
nn = N
K = int(input())
n = 1
st = []
m = min(N, K)
while N >= abs(nn - n * m) :
    N = abs(N - K)
    n = n + 1
    st.append(N)
    a = min(st)
print(min(m, a))

Submission status: Runtime Error

Solution code:
N = int(input())
nn = N
K = int(input())
n = 1
st = []
m = min(N,K)
while nn - n * m >= 0:
    N = abs(N - K)
    n = n + 1
    st.append(N)
    t = min(st)
print(min(m, t))

Submission status: Runtime Error

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

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

if N > K:
  b = N % K
  print(int(K/b))

res = [N]
if N < K:
  while True:
    res.append(abs(N - K))
    N = abs(N - K)
    if N in res:
      break
print(min(res))

Submission status: Runtime Error

Solution code:
#abc 161c
import math
n,k = map(int, input().split())
ilow= (n-n%k)%k
ilow = i
iup = i+1
print(min(n,abs(n-k*iup),n-k*ilow))

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
n = 1
m = min(N,K)
while N - n * m > 0:
    N = abs(N - K)
    n = n +1
print(N)

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
n = 1
while N - n * K > 0:
    N = abs(N - K)
    n = n +1
print(N)

Submission status: Runtime Error

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

s = N % K

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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


while abs(N-K) > N:
  N = abs(N-K)
  
print(N)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
num=min(n,k)
a=max(n,k)%min(n,k)
b=min(n,k)
for i in range(10):
    b=abs(b-a)
    num=min(b,num)
print(num if min(n,k)!=1 else 0)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

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

if K == 1:
  print(0)
else:
  # N_swp = np.abs(N - K)
  N_swp = math.sqrt((N - K)**2)
  while N_swp != 0 and N_swp < N :
    N = N_swp
    # N_swp = np.abs(N - K)
    N_swp = math.sqrt((N - K)**2)
  print(int(N))

Submission status: Runtime Error

Solution code:
import numpy as np

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

if K == 1:
  print(0)
else:
  N_swp = np.abs(N - K)
  while N_swp != 0 and N_swp < N :
    N = N_swp
    N_swp = np.abs(N - K)
  print(N)

Submission status: Runtime Error

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


min_result = n
flag = 0
dic = {}

z = n // k
zz = n //z
result = zz

while flag==0:
    result = abs(result - k) 
    print('result',result)
    print('min_result',min_result)
    if result <= min_result:
        min_result = result
        dic[result] = dic.get(result, 0) + 1

        if dic[result] > 1:
            flag=1
print(min_result)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def abs(n, k):
    if n - k >= 0:
        return n-k
    else:
        return k-n
        
N, K = map(int, input().split())
while N > abs(N-K):
    N, K = abs(N-K), K
print(N)

Submission status: Runtime Error

Solution code:
import scipy as sp

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

print(sp.gcd(n,k))

Submission status: Runtime Error

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

print(sp.gcd(n,k))

Submission status: Runtime Error

Solution code:
ans1 = abs(N - K)
cnt = 0
if N > ans1:
    while True:
        ans2 = abs(ans1 - K)
        if ans1 > ans2:
            ans1 = ans2
            cnt = 1
            cnt += 1
        else:
            ANS = ans1
            break
elif N <= ans1:
    ANS = N

# print(ans1, ans2)
print(ANS)
# print(cnt)

Submission status: Runtime Error

Solution code:
#include<iostream>

int main(){
    long long int N,K,r;
    std::cin >> N >> K;
    r = N%K;
    if(r>(K-r)){
        std::cout<<K-r<<std::endl;
    }
    else {
        std::cout<<r<<std::endl;
    }
}

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


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

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

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
if N%K == 0:
   print("0")

#最小値を求める上では余りが割る数を下回るまで続く
elif abs(N%K- K)<N:
     print(abs(N%K- K))
 
else:
     print(N[0])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

N = [abs(n - i*k) for i in range(n)]
ans = min(N)
print(ans)

Submission status: Runtime Error

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

N = [abs(n - ik) for i in range(n)]
ans = min(N)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
ans = N
x = abs(x - K * max(x // K, 1))
while ans > x:
    ans = x
    x = abs(x - K * max(x // K, 1))
print(ans)

Submission status: Runtime Error

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

def f(n, k):
    n2 = abs(k-n)
    if n2 < n:
        f(n2, k)
    else:
        return n
    
print(f(N,K))

Submission status: Runtime Error

Solution code:
a,b=map(int,input().split())
A=list(map(int,input().split()))
 
S=sum(A)
ans=0
for i in range(a):
    if A[i]>=S/(4*b):
        ans+=1
if ans>=b:
    print('Yes')
else:
    print('No')