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:
x,K=map(int,input().split())

def sa(i,j):
    if i-j>=0:
        return i-j
    else:
        return -(i-j)

    
    
A=x%K

if A<sa(A,K):
    print(A)
else:
    print(sa(A,K))

Submission status: Accepted

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

def d(i,j):
    if i>=j:
        i=i-j
        return i-j
    if i<j:
        i=j-i
        return j-i
    else:
        return "aho"

def sa(i,j):
    if i-j>=0:
        return i-j
    else:
        return -(i-j)

    
    
A=x%K

if A<sa(A,K):
    print(A)
else:
    print(sa(A,K))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#K = int(input())
N,K = map(int,input().split())
#N = int(input())
#A = list(map(int,input().split()))
flag=True
c=N
if N%K==0:
    print(0)
    exit()
else:
    l=N//K
    N-=l*K
    while flag==True:

        N=abs(N-K)
        if c <= N:
            flag=False
        else:
            c=N
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

s1 = N % K
s2 = K - s1

print(min(s1, s2))

Submission status: Accepted

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

Submission status: Accepted

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

if n >= k:
    while(n >= k):
        n = n % k
if n > k/2:
    while(n > k/2):
        n = k-n
if n <= k/2:
    print(n)

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
   
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

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

else:
	n=n%k
	while n>k//2:
		n=abs(n-k)
	
	print(n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

n1=n-(k*(n//k))

n2=abs(n1-k)

if n1<n2:
    print(n1)
else:
    print(n2)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
s = n % k
list = [s]
for _ in range(s):
  if abs(s-k) in list:
    break
  elif abs(s-k)<s:
    list.append(abs(s-k))
print(min(list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# encoding:utf-8
N, K = [int(i) for i in input().split()]
print(min(abs(N % K), abs(N % K - K), N))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N,K = map(int,input().split())
S,A = divmod(N,K)
print(min(A,abs(A-K)))

Submission status: Accepted

Solution code:
line1 = input().split()
n = int(line1[0])
k = int(line1[1])
 
print(min(n % k, abs(n % k - k)))

Submission status: Accepted

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


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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a = n % k

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

Submission status: Accepted

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

Submission status: Accepted

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

resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C - Replacing Integer

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

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n, k = wi()

t = n % k
if t > k / 2:
    print(k - t)
else:
    print(t)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if N < K:
    print(min(N, K-N))
    exit()
else:
    wari = N // K
    print(min(N - (K * wari), K * (wari + 1) - N))

Submission status: Accepted

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

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

from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def floor(a, b):
    """floor(a/b) 整数値を返す"""
    c = (a - a % b)/b
    return int(c)


def main():
    n, k = wi()
    a = n % k
    b = abs(a - k)
    print(min(a, b))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = n%k

print(int(min(m,k-m)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  6 02:04:30 2020

@author: shinba
"""

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

N -= (N//K)*K

n= N-K

if abs(n) >= abs(N):
    print(abs(N))
else:
    print(abs(n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
inp_ls = input().split(" ")
n,k = [int(e) for e in inp_ls[:2]]

pl_min = n - (n//k)*k
m_min = abs(pl_min - k)

out_n = m_min if m_min < pl_min else pl_min
print(out_n)

Submission status: Accepted

Solution code:
def input_num():
	ls = input().strip().split(" ")
	return [int(e) for e in ls]

n,k = input_num()

# 本質的でない n>kの場合をカットする
n = n%k
print(min(n,abs(n-k)))

Submission status: Accepted

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

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

print(x)

Submission status: Accepted

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

a=N
while True:
    if a-10**16*K>=0:
        a=a-10**16*K
    elif a-10**14*K>=0:
        a=a-10**14*K
    elif a-10**12*K>=0:
        a=a-10**12*K
    elif a-10**10*K>=0:
        a=a-10**10*K
    elif a-10**8*K>=0:
        a=a-10**8*K
    elif a-10**6*K>=0:
        a=a-10**6*K
    elif a-10**4*K>=0:
        a=a-10**4*K
    elif a-10**2*K>=0:
        a=a-10**2*K
    elif a-K>=0:
        a=a-K
    elif K-a>=a:
        a=a
        break
    else:
        a=K-a
print(a)

Submission status: Accepted

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

mm=n%k
mmm=k-mm

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

print(str(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
lis1=list(map(int,list(input('').split(' '))))
n=lis1[0]
m=lis1[1]
if n%m==0 :
  print(0)
else :
  print(min(abs(m-n%m),abs(n%m)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

if N > K:
    x = N%K
else:
    x = K-N

while  N > x :
    N = x

    if N > K:
        x = N-K

    else:
        x = K-N

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, K = [int(i) for i in input().split()]
    N = N - (K * (N // K))
    if N == 0:
        print(0)
        return

    prevs = set()
    while N not in prevs:
        prevs.add(N)
        N = abs(N - K)

    print(min(prevs))

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

import numpy as np

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

n_mod_k = n % k 
print(min(n_mod_k, np.abs(k-n_mod_k)))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,k = LI()
    v = set()
    while n not in v:
        v.add(n)
        n = min(abs(n-k), n % k)

    return min(v)


print(main())

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

before = abs(N - int(N / K) * K)
before2 = N % K
after = abs(before2 - K)

if before2 > after:
    print(after)
else:
    print(before2)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n, k = MI()
    ans = n % k

    ans = min(ans, abs(ans - k))
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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


d = n % k

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

N, K = Is()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

print(min([t,k-t]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n,k = [int(i) for i in readline().split()]

a = n%k
ans = min(a, k-a)

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n,k = [int(i) for i in readline().split()]

l = [n]

n %= k
l.append(n)

while True:
    diff = abs(l[-1]-k)
    if diff not in l:
        l.append(diff)
    else:
        break

print(min(l))

Submission status: Accepted

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

if x >= k/2:
    print(abs(x-k))
else:
    print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = 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:
  n=n%k

print(min(n,k-n))

Submission status: Accepted

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

Submission status: Accepted

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

amari = N%K

ans = float('inf')
for i in range(10):
    ans = min(ans, amari)
    amari = abs(amari-K)
print(ans)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a = n%k
b = k - a

print(min(a,b))

Submission status: Accepted

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

n = n % k

n = abs(n - k)

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, K = list(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())

cm = N
k = N % K
if k == 0:
    print(0)
    exit()
print(min(abs(k-K), N))

Submission status: Accepted

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

x = n
if n < k:
    a = abs(x - k)
    b = abs(a - k)
    print(min(a, b))
elif n == k:
    print(0)
else:
    x = n % k
    if x == 0:
        print(0)
    else:
        t = x - k
        print(abs(t))

Submission status: Accepted

Solution code:
def func():
  array = input().split(' ')
  N = int(array[0])
  K = int(array[1])
  K_2 = K / 2
  if N <= K_2:
    print(N)
    return
  while 1:
    if N >=K:
      N %= K
    else:
      N = K - N
    if N <= K_2:
      break;

  print(N)

func()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=N//K
print(min(N-a*K,abs((N-a*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:
def main():
	n,k = map(int,input().split())
	
	mod = n % k
	if mod < k:
		print(min(mod,abs(mod-k)))
	else:
		print(mod)



if __name__ == '__main__':
	main()

Submission status: Accepted

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

print(min(a, a % b,abs(a % b- b)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string
#import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from functools import lru_cache, reduce
#from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())

n,k = MI()
if n%k == 0:
    print(0)
else:
    n = n%k
    while abs(n-k) < n:
        n = abs(n-k)
    print(n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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:
        a=n%k
        b=k/2
        if a<b:
            print(a)
        else:
            print(k-a)
else:

    print(min([n,k-n]))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

b = n % k

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, stdout
import math
import bisect
import queue

n, k = map(int, stdin.readline().strip().split())

times = n // k

ans = n - k * times
ans = min(ans,abs(n - (times + 1) * k))

stdout.writelines(str(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

a = n % k
b = abs(a - k)

print(min(a, b))

Submission status: Accepted

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

s = N % K

print(min(s, K-s))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, K = read_ints()
    N %= K
    return min(N, K-N)


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

Submission status: Accepted

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

sho = n // k

a = n - k * sho
b = a - k

print(min(abs(b), a))

Submission status: Accepted

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

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

Submission status: Accepted

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

a = n % k 
b = (k-n) % k

print(min(a,b))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

x = abs(n%k)
x1 = abs(x-k)

if x <= x1:
    print(x)
else:
    print(x1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n, k = [int(x) for x in stdin.readline().rstrip().split()]

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

Submission status: Accepted

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

max_i = max(n, k)


if n>=k:
    n = n%k
if n==0 or k==1:
    print(0)
else:
    for _ in range(max_i):
        tmp = abs(n-k)
        if tmp >= n:
            break
        else :
            n = tmp
    
    print(n)

Submission status: Accepted

Solution code:
import sys
N, K = map(int, sys.stdin.readline().split())
a1 = N - (N // K) * K
a2 = abs(N - ((N // K) + 1) * K)
print(min(a1, a2))

Submission status: Accepted

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

Submission status: Accepted

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

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 = (int(x) for x in input().split())

a = N%K

if abs(K-a) > a:
    print(a)
else:
    print(abs(K-a) )

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=N%K
b=K/2

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

Submission status: Accepted

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

N = N % K

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

Submission status: Accepted

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

t = N//K
N -= t*K

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

Submission status: Accepted

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

x = N % K

print(min(x, K-x))

Submission status: Accepted

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

Submission status: Accepted

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

seen = {}
while(n not in seen):
	seen[n] = abs(n - k)
	if n > k:
		n = n % k
	elif n <= k:
		n = abs(n-k)
print min(seen.values())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, raw_input().split(' '))
seen = {}

while(n not in seen):
	seen[n] = abs(n - k)
	if n < 2 * k:
		n = abs(n-k)
	else:
		n %= k
		n += k		
print min(seen.values())

Submission status: Accepted

Solution code:
n,k = map(int, raw_input().split(' '))
seen = {}

while(n not in seen):
	seen[n] = abs(n - k)
	if n == k:
		break
	elif n > k:
		if n - k < k:
			n = abs(n-k)
		else:
			n %= k
			n += k
		
	elif n < k:
		n = abs(n - k)
print min(seen.values())

Submission status: Accepted

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

print(n)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
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: Accepted

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

Submission status: Accepted

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


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

n = n%k

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