Problem:


Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n %= k

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
from copy import copy,deepcopy
from itertools import permutations,combinations
from pprint import pprint

def myinput():
    return map(int,input().split())

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

n,k = myinput()

r = n%k
r2 = abs(r-k)

ans = min(r,r2)

print(ans)

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
a = N // M
b = N%M
c = abs(N - a*M)
d = abs(N - (a+1)*M)
print(min(c,d))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
q = n // k
print(min(n - q * k, abs(n - (q + 1) * k)))

Submission status: Accepted

Solution code:
def main():
    n,k=map(int,input().split())
    dis=n//k
    n-=k*dis
    ans=n if abs(n-k)>n else abs(n-k)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 20:52:57 2020

@author: over-
"""

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

ans=N%K
if ans!=0:
    ans=K-ans
ans=min(ans,N)
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
'''
N>K
N->N-K
K<N
N->K-N->N
'''
S={N}
while(1):
    if N>K:
        N=N%K
    elif N==K:
        N=0
    else:
        N=K-N
    if N in S:
        break
    S.add(N)
print(min(S))

Submission status: Accepted

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

Submission status: Accepted

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

amari = n%k
amari2 = abs(amari-k)

print(min(amari, amari2))

Submission status: Accepted

Solution code:
#JMD
#Nagendra Jha-4096

 
import sys
import math

#import fractions
#import numpy
 
###File Operations###
fileoperation=0
if(fileoperation):
    orig_stdout = sys.stdout
    orig_stdin = sys.stdin
    inputfile = open('W:/Competitive Programming/input.txt', 'r')
    outputfile = open('W:/Competitive Programming/output.txt', 'w')
    sys.stdin = inputfile
    sys.stdout = outputfile

###Defines...###
mod=1000000007
 
###FUF's...###
def nospace(l):
    ans=''.join(str(i) for i in l)
    return ans
 
 
 
##### Main ####
t=1
for tt in range(t):
    #n=int(input())
    n,k= map(int, sys.stdin.readline().split(' '))
    if(n==k):
        print(0)
        continue
    if(n<k):
        print(min(n,k-n))
        continue
    else:
        n=(n%k)
        print(min(n,k-n))
        continue
    #a=list(map(int,sys.stdin.readline().split(' ')))
    
    
#####File Operations#####
if(fileoperation):
    sys.stdout = orig_stdout
    sys.stdin = orig_stdin
    inputfile.close()
    outputfile.close()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

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

n,k = na()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n,k = tuple([int(t)for t in input().split()])

    t = n%k

    s = k-t
    print(min(t,s))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

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, input().split())

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:
# -*- coding: utf-8 -*-

def solve():
    N, K = map(int, input().split())
    return str(min(N%K, (-N)%K,))

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
def even(n): return 1 if n%2==0 else 0

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


res = n//k

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

x = N // K

ans = N - K * x

temp = abs(ans - K)

if ans >= temp:
    print(temp)
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
temp = N // K
ans = N - temp*K

temp2 = abs(ans - K)
print(min(ans, temp2))

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:
import math
N,K = map(int,input().split())
anss = []
q = N % K
anss.append(q)
anss.append(abs(q - K))
print(min(anss))

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

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

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

ans=n%k

if ans<abs(ans-k):
    print(ans)
else:
    print(abs(ans-k))

Submission status: Accepted

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

Submission status: Accepted

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

ans1 = n%k
ans2 = k - n%k
print(min(ans1,ans2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

ans = min(a, b)

print(ans)

Submission status: Accepted

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

C()

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:
# -*- coding: utf-8 -*-
n,k = list(map(int,input().split()))

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, k = map(int,input().split())

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

Submission status: Accepted

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

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

print(min(a,b))

Submission status: Accepted

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

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

print(min(a,b))

Submission status: Accepted

Solution code:
N,K = list(map(int,input().split()))
m = N
N -= N//K*K
for i in range(100):
    N = abs(N-K)
    m = min(m,N)
print(m)

Submission status: Accepted

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

if x > k:
    # k未満になるまで減算できる.
    x = x - k * (x//k)
# xかk-xかどちらかだけ.
print(min(x, k-x))

Submission status: Accepted

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

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

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = n % k

if ans > (k-ans):
    print(k-ans)
else:
    print(ans)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

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

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

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

print(N)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    x = N // K


    print(min(N % K, (x + 1) * K  - N))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from bisect import *				# bisect_left(arr,x,start,end)  => start and end parameters are temporary
from sys import stdin				# bisect_left return leftmost position where x should be inserted to keep sorted
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''
s
'''

def solve(n,k):
	return min(n%k,abs(n%k - k))
	

N,K = list(map(int, stdin.readline().rstrip().split()))
ans = solve(N,K)
print(ans)

Submission status: Accepted

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


def solve(N: int, K: int):
    print(min(N%K, abs(N%K - K)))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#gcdは3.4.3だとfractionsにある
from fractions import gcd
import math

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    n,k = mi()
    print(min(abs(n-(n//k)*k),abs(n-(n//k+1)*k)))


main()

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())
tmp=n//k
a=n-k*tmp
b=abs(n-k*(tmp+1))
print(min(a,b))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    _, r = divmod(N, K)
    ans = min(abs(r - K), r, r + K)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def read():
    N, K = list(map(int, input().strip().split()))
    return N, K

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


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#def main(S, b):
if __name__ == '__main__':
    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 = -a + K
print(min(a,b))

Submission status: Accepted

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

Submission status: Accepted

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

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict
from itertools import combinations, permutations
from itertools import accumulate
from math import ceil, sqrt, pi

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int):

    N = N%K

    s = set()

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

    print(min(s))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

n %= k
print(min(n, (n - k) * (-1)))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

t = n % k

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

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    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())
l = n//k
a = abs(n-k*(l-1))
b = abs(n-k*l)
c = abs(n-k*(l+1))
print(min(a,b,c))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,K = map(int,input().split(' '))
d = N%K
dif = abs(d-K)
print(min(d,dif))

Submission status: Accepted

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

Submission status: Accepted

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()

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

Submission status: Accepted

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

Submission status: Accepted

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

n1 = N % K
n2 = abs(n1 - K)
print(min(n1, n2))

Submission status: Accepted

Solution code:
import collections
import math
import operator as op
from functools import reduce
from collections import Counter
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


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


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    N, K = get_inputs(int)
    ans = min(N % K, K-(N % K))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ctr = N // K

print(min(abs(N - K * ctr), abs(N - K * (ctr + 1))))

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:
def main():
    n,k = map(int,input().split())
    d = n % k
    dd = abs(d - k)
    print(min(d,dd))
if __name__=='__main__':
    main()

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

mod = n % k

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

Submission status: Accepted

Solution code:
import sys

def solve():
      input = sys.stdin.readline
      N, K = map(int, input().split())
      d = N % K
      minN = N
      minN = min(minN, min(d, K - d))
      print(minN)

      return 0

if __name__ == "__main__":
      solve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

    l = N % K
    r = abs(K -l)

    print(min(l,r))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

r = N%K

if 2*r <= K:
  print(r)
else:
  print(K-r)

Submission status: Accepted

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

def a(x, K):
    return abs(x-K)

tmp2 = N//K
N = N - (tmp2*K)

pre = N + 1
while True:
    if pre <= N:
        N = pre
        break
    pre = N
    N = a(N, K)

print(N)

Submission status: Accepted

Solution code:
def solve():
    N, K = map(int, input().split())
    N = N % K
    print(min(N, abs(N-K)))
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    N = N%K

    while True:
        preN = N
        N = abs(N-K)
        if N > preN:
            print(preN)
            return

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = n//k

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

Submission status: Accepted

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

Submission status: Accepted

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

if (n % k == 0):
    print(0)
else:
    l = n // k
    print(min(n, abs(n-k*l), abs(n-k*(l+1))))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(K - N, N))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
import itertools
from copy import deepcopy
import fractions
import random
from heapq import heappop,heappush
import time
import os
import sys
import datetime
from functools import lru_cache
readline=sys.stdin.readline
sys.setrecursionlimit(2000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def fac_list(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
#############################################

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = [int(i) for i in input().split()]
if N>=K:
    N %= K
print(min(N, K-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
print(min(abs(N-K*n),abs(N-K*(n+1))))

Submission status: Accepted

Solution code:
import copy

import math
import fractions

def j(n):
    if n:print("Yes")
    else:print("No")
    exit(0)
rem = 10 ** 9 + 7

"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def ipmultiplerow(n):
    a = []
    for i in range(n):
        a.append(ip())
    return a
def printrow(a):
    for i in a:
        print(i)

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

n,k = ips()
a = n//k
b = n//k + 1
a*=k
b*=k
ca = abs(n-a)
cb = abs(n-b)
print(min(ca,cb))

Submission status: Accepted

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

x = abs(N - div * K)
y = abs(N - (div + 1) * K)
print(min(x, y))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
if n>=k:
    x=n//k
    n-=x*k
ans=min(n,k-n)
print(ans)

Submission status: Accepted

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

m = n % k
ans = min([abs(k - m), m])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=input().split()
N=int(N)
K=int(K)
if (0<=N<=10**18)and(1<=K<=10**18):
  a=N%K
  b=K-a
  if a>b:
    print(b)
  if a<=b:
    print(a)

Submission status: Accepted

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