Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

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

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

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0



Sample input:
7 4


Sample output:
1

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if 2 * N < K: print(N)
else: print(min(K * math.ceil(N / K) - N, K - (K * math.ceil(N / K) - N)))

Submission status: Wrong Answer

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

while True :
    #if diff > K:
    #    diff = diff - K
    #else:
    #    diff = K - diff
    if K == 1:
        diff = 0
        cnt = 2
    else:
        diff = abs(diff - K)
        cnt = cnt + 1

    if diff < K and cnt >= 2:
        break

print(diff)

Submission status: Wrong Answer

Solution code:
N , M = (map(int , input().split()))
a = -(-N//M)
if N == M:
    ans = 0
elif a == 1:
    ans = N
else:
    ans = (a*M)-N
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from fractions import gcd


def main():
    n, k = map(int, input().split())
    if k == 1 or n == 0 or n == k or n == 1:
        print(0)
    elif n % k == 0:
        print(0)
    elif n < k:
        print(min(n, k - n))
    else:
        ans = gcd(n, k)
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

#A = max(N,K)
#B = min(N,K)

#sre = (1/(4*M))*sum(A)

init_i = math.floor(N/K)

p1 = abs(N - (init_i-1) * K)
p = abs(N - init_i * K)
m = abs((init_i+1) * K - N)

print(min(p1,p,m))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

import sys
sys.setrecursionlimit(10**6)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    n,k=mis()
    ave=float(k/2)
    ans=n
    dis=abs(ans-k)
    if n>=k:
        if dis>k:
            print(n%k)
            exit()
        else:n-=k
    print(n if n<ave else k-n)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
tmp=0
if(N+K)%2==0:
    N=N/2
    K=K/2


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

while True:
    if n < k:
        r = n
        break
    r = n - k
    if r < 0:
        break
    else:
        n = r
print(abs(r))

Submission status: Wrong Answer

Solution code:
# coding : utf-8

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

flag = 0

alpha = int(N / K)

cad = [N - (alpha-1)*K, N - alpha*K, N - (alpha+1)*K]
cad = list(map(abs, cad))

print(min(cad))

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0 or N == K:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
        if i >= 1:
            if ans in l:
                print(min(l))
                print(l)
                exit()
    print(min(l))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
if k == 1 or n == 0:
    print(0)
    exit()
while True:
    if n > k:
        n = n % k
    else:
        ost = k % n
        if n == ost or ost == 0:
            print(n)
            exit()
        else:
            n = ost

Submission status: Wrong Answer

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

if n == 0:
    print(k)
elif n == 1 or k == 1 or n == k:
    print(0)
elif n > k:
    r = n % k
    if r == 0:
        print(0)
    else:
        print(k-r)
elif n < k:
    print(min(n, k-n))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

def culc(N, K):
    if K == 1:
        print(0)
        return

    x = N
    min = x
    while min > x:
        print(min, x)
        min = x
        x = absdiff(x, K)

    print(min)
    return


culc(N, K)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N, K = map(int, input().split())
if (N == 1 or K == 1 or N == K):
    print(0)
    exit(0)
while (True):
    if (N % K == 0 or K % N == 0): break
    if (N > K):
        N = N % K
    elif (K >= N):
        N = K % N
print(N)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
min = n + 1
if n >= k:
    n = n % k
else:
    n = abs(k - n)
while True:
    if n < min:
        min = n
        n = abs(n - k)
    else:
        break

print(min)

Submission status: Wrong Answer

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


if N == 0:
    print(N)
elif K == 0:
    print(K)
elif N < K:
    k = K % N
    ans = abs(N - k)
    print(ans)
elif N > K:
    k = N % K
    ans = abs(K - k)
    if k == 0:
        print("0")
    else:
        print(ans)
else:
    print("0")

Submission status: Wrong Answer

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

dec = N - abs(N - K)

if dec < 0:
    print(N)
    exit()

q, r = divmod(N, dec)

print(r)

Submission status: Wrong Answer

Solution code:
(a, b)=list(map(int, input().split()))
if a < b or b == 0 or a== 0:
  print(a)
else:
  c = a % b
  if c == 0:
    print('0')  
  else:
    print(abs(b-c))

Submission status: Wrong Answer

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=b
for i in range (1000000):
    x=b-a
    if x==0:
        f=0
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if y[i]<=y[i+1]:
        f=y[i]
        break

print(f)

Submission status: Wrong Answer

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

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

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n,k = input_list()
  if n == k:
    print(0)
    exit()
  if n > k:
    if n % 2 == 0 and k % 2 == 0:
      print(0)
      exit()
    if n % k == 0:
      print(0)
      exit()
    print(1)
  else:
    one = abs(n-k)
    two = abs(one-k)
    print(min(one,two))
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Wrong Answer

Solution code:
N,K= map(int,input().split())
x = N
if K == 1:
  print(0)
while abs(x-K) < x:
    x = abs(x-K)
else:
    print(x)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if(n%k == 0):
        
    g = n//k
        
    print(n - (g*k))
else:
    g = n//k
    ans = n - (g*k)
    print(abs(ans - k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = list(map(int,input().split()))
S = N - K
count = 0

while S > 0:
  N = abs(S)
print(N)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
if n == k:
    print(0)
elif n < k:
    print(n)
elif n >= k:
    ans = n % k
    while (True):
        if ans <= abs(ans - k):
            print(ans)
            exit()
        ans = abs(ans - k)

Submission status: Wrong Answer

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

if k == 1 or n == 0:
  print(0)
elif n < k :
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
else:
  r1 = n // k
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))

Submission status: Wrong Answer

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

right=N%K
left=(int(N/K)+1)*K-N
#print(left,right)

if right>=left:
    print(int(left))
else:
    print(int(right))

Submission status: Wrong Answer

Solution code:
nk = list(map(int, input().split()))
n = nk[0]
if nk[1] == 1:
  print(0)
else:
  for _ in nk:
    diff = abs(n - nk[1])
    if diff < n:
      n = diff
    else:
      break
  print(n)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
for p in range(K):
  if (N - p) % K == 0:
    print(p)
    break

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=[]
if k == 1:
    print(0)
while True:
    n=abs(n-k)
    ans.append(n)
    if ans.count(n)==2 :
        break
print(min(ans))

Submission status: Wrong Answer

Solution code:
NS = [int(_) for _ in input().split()]

N = NS[0]
K = NS[1]

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

def solve(N, K):
    return N % K

print(solve(N, K))

Submission status: Wrong Answer

Solution code:
def main():
    import math
    N, K = map(int, input().split())
    #syo = int(N / K)    # 桁の件
    syo = math.floor(N / K)    # 桁の件
    amari = N % K
    tmp = K * (syo + 1) - N

    if amari <= tmp:
        print(amari)
    else:
        print(tmp)

main()

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
result = 0
if k < n:
    result = n % k
elif n < k:
    result = n
print(result)

Submission status: Wrong Answer

Solution code:
(a, b)=list(map(int, input().split()))
if a < b or b == 0:
  print(a)
else:
  c = a % b
  if c == 0:
    print('0')  
  else:
    print(abs(b-c))

Submission status: Wrong Answer

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

def okikae(t):
    if K/2 >= t and t <= K:
        print(t)
    elif N>K:
        print("0")
    else:
        okikae(abs(t-K))

okikae(N)

Submission status: Wrong Answer

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

if n == 0:
    print(k)
elif k == 1 or n == k:
    print(0)
elif n > k:
    r = n % k
    if r == 0:
        print(0)
    else:
        print(k-r)
elif n < k:
    print(min(n, k-n))

Submission status: Wrong Answer

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

if (N % 2) == 0 and (K % 2) == 0:
    print('2')
elif (N % 2) == 1 and (K % 2) == 1:
    print('2')
elif N == 1 or K == 1 or N == 0 or K == 0:
    print('0')
else:
    print('1')

Submission status: Wrong Answer

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

if N == 0:
    print(K)
elif N == 1 or K == 1:
    print("0")
elif (abs(N - K) % 2 == 0):
    print("2")
else:
    print("1")

Submission status: Wrong Answer

Solution code:
import sys
a,b=map(int,input().split())
i=1
z=[0]*2
saisyo=abs(a-b)
if saisyo>a:
    saisyo=a
if a<b:
  print(saisyo)
  sys.exit()
  
u=a%b
w=(a-u)/b
z[0]=a-(w*b)
z[1]=abs(a-(w+1)*b)
r=int(min(z))


c=[saisyo,r]
print(min(c))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
def gcd(a,b):#a<=b
    while a!=0:
        a,b = b%a,a
    return b


if n == 0:
    print(k)
elif k == 1:
    print(0)
else:
    if n > k:
        a = n
        n = k
        k = a
    print(gcd(n,k))

Submission status: Wrong Answer

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

    print(store)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
temp=10**19
while(n>abs(n-k)):
    n=min(abs(n-k),n//2)
print(n)

Submission status: Wrong Answer

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

m = N//K

a = N
b = abs(a-K*m)
while b < a:
       a = min(a,b)
       b = abs(a-K)
        
print(a)

Submission status: Wrong Answer

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

def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)

cnt = gcd(n, k)

if k == 1:
    print(0)
else:
    print(min(cnt, k))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = N%K
min = 1001001001
for i in range(2):
  x = abs(x-K)
  if x < min:
    min = x
print(min)

Submission status: Wrong Answer

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

if k == 1:
  print(0)
elif n < k or n == 0:
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
else:
  r1 = n // k
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
check = N - M
result = abs(check) - M
print(abs(result))

Submission status: Wrong Answer

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

if a[0] < a[1]:
  print(a[0])
else:
  b = a[0] % a[1]
  c = abs(b-a[1])
  d = abs(b+a[1])
  print(min(b,c,d))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if N % K == 0:
    print(0)
else:
    while True:
        a = N % K
        if a == 0:
            print(K)
            break
        else:
            N = K
            K = a

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


if N == 0:
    print(N)
elif K == 0:
    print(N)
elif N < K:
    k = K % N
    ans = abs(N - k)
    print(ans)
elif N > K:
    k = N % K
    ans = abs(K - k)
    if k == 0:
        print("0")
    else:
        print(ans)
else:
    print("0")

Submission status: Wrong Answer

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

while True:
    if ((n - k)>100):
        n = n//k
    else:
        break

while True:
    an = abs(n-k)
    if n <= an:
        break
    else:
        n = min(n, an)

print(n)

Submission status: Wrong Answer

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

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

if n > k:
    tmp = n%k
    print(min(tmp,abs(tmp-k)))
    exit()

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

if n == 0:
    print(0)

#print(0)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
d = N
# for i in range(N//K + 1):
#     d = d - K
sumk = (N//K)*K
d = N - sumk
if N == 0:
    ans=0
else :

    d2 = N
    if (d*(-1)-K)<0:
        d2 = (d*(-1)-K)*(-1)
    else :
        d2 = (d*(-1)-K)
    d3 = N
    if (d2-K)<0:
        d3 = (d2-K)*(-1)
    else :
        d3 = (d2-K)
    ans = min([(d*(-1)), d2, d3])
    print(ans)

Submission status: Wrong Answer

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

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

intlist = [ (N % K) ,abs (( N % K ) - N )] 
print ( min(intlist) )

Submission status: Wrong Answer

Solution code:
from fractions import gcd
NK = input().split()
N = int(NK[0])
K = int(NK[1])
if N==1 or K==1:
    print(0)
    exit()
else:
    print(gcd(N,K))

Submission status: Wrong Answer

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

    if N == 0 :
        print("0")
    elif K == 1 and N != 0 :
        print("0")
    elif K == 2 and N % 2 == 0 and N != 0 :
        print("0")
    elif K == 2 and N % 2 != 0 and N != 0 :
        print ("1")
    else :
        for i in range(0 , (10^18 // N) + 2) :
            value_diff_abs = N
            N = abs(N - K)
            if value_diff_abs < N :
                print(str(value_diff_abs))
                break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
(a, b)=list(map(int, input().split()))
if a < b:
  print(a)
else:
  c = a % b
  if c == 0:
    print('0')  
  else:
    print(abs(b-c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ans=[]
if k == 1:
    print(0)
while True:
    n=abs(n-k)
    ans.append(n)
    if ans.count(n)>2 :
        break
print(min(ans))

Submission status: Wrong Answer

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


def mod(n, k):
    while k:
        n, k = k, n % k
    return n


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

    print(abs(m - K) if abs(m - K) < m else m)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k:
    print(n)
else:
    if l == 0:
        print(0)
    elif l < k:
        print(l)
    elif l > k:
        print(l - k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

def function(N,K):
    pre =N
    if N > K*10:
        return K-1



    while(1):    
        # print('N',N)
        N = abs(N-K)
        if pre == N:
            return pre

        if pre< N:
            return pre
        pre = N 

print(function(N,K))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from fractions import gcd
NK = input().split()
N = int(NK[0])
K = int(NK[1])
if N==1 or K==1:
    print(0)
    exit()
elif N==0:
    print(K)
else:
    print(gcd(N,K))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
check = N - M
result = check - M
print(abs(result))

Submission status: Wrong Answer

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

var = N % K
if var == 0:
        print(0)
elif var == K-1 or var == 1:
        print(1)
else:
        while N < K:
                N = N // K
        print(N)

Submission status: Wrong Answer

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

if N==0:
    print(0)
cand0=abs(N-K)
cand4=abs(cand0-K)
N=N%K
cand1=abs(N-K)
cand2=abs(cand1-K)
cand3=abs(cand2-K)
print(min(N,cand0,cand4,cand1,cand2,cand3))

Submission status: Wrong Answer

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

if n < k:
    print(n)
elif k == 1 or n == 0 or (k % 2 == 0 and n % 2 == 0) or (k % 2 == 1 and n % 2 == 1):
    print(0)
elif (k % 2 == 0 and n % 2 == 1) or (n % 2 == 0 and k % 2 == 1):
    print(1)

Submission status: Wrong Answer

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

def func(x,K):
    tmp=x-K
    if tmp<0:
        tmp*=(-1)
    return tmp

# def pow(x, n):
#   ans = 1
#   while(n > 0):
#     if(bin(n & 1) == bin(1)):
#       ans = ans*x
#     x = x*x
#     n = n >> 1 #ビットシフト
#   return ans
#
# pow(3,100) #3の100乗

y=N/K
N=func(N,K*y)

ans = N
while(True):
    N=func(N,K)
    if N<ans:
        ans=N
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
nn, kk = input().split(" ")
n = int(nn)
k = int(kk)
 
remain = n % k
if( kk=="1" ):
  print(0)

Submission status: Wrong Answer

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

print(store)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
first = abs(n - k)
list = []
n_n = abs(first - k)
if first == n_n:
  print(n_n)
  
while n_n != first:
  n_n = abs(n_n-k)
  list.append(n_n)
  
if len(list) != 0:
	print(min(list))

Submission status: Wrong Answer

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


    
n1=n-(n//k)*k
n2=n-k
print(min(n1,n2))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().strip().split())
a = 0
case = 0
while (n-k)>=0:
    n = n-k
    case = case+1
if case>=1:
    n = abs(n-k)
else:
    n = n
print(n)

Submission status: Wrong Answer

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

if N < K:
    print(str(N))
elif N%K==0:
    print("0")
else:
    print(str(min(abs(N%K - K), abs(N%K))))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
count=abs(n-k)
count2=count
ans=count
if(k==1):
    print(0)
    exit()
while(1):
    count=abs(ans-k)
    if(count>=ans):
        print(ans)
        exit()
    else:
        print(ans)
        ans=count
        continue

Submission status: Wrong Answer

Solution code:
a,b=input().split()
a=int(a)
b=int(b)
y=[10**18]
f=0
for i in range (1000000):
    x=a-b
    if x==0:
        f=0
        break
    elif x<0:
        y.append(x*(-1))
        a=x*(-1)
    else :
        y.append(x)
        a=x
    if y[i]<=y[i+1]:
        f=y[i]
        break

print(f)

Submission status: Wrong Answer

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

if (N % 2) == 0 and (K % 2) == 0:
    print('2')
elif N == 1 or K == 1 or N == 0 or K == 0:
    print('0')
else:
    print('1')

Submission status: Wrong Answer

Solution code:
NS = [int(_) for _ in input().split()]

N = NS[0]
K = NS[1]

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

def solve(N, K):

    if N < K:
        return N
    else:
        return N % K

print(solve(N, K))

Submission status: Wrong Answer

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

if k == 1:
  print(0)
elif n < k:
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))
else:
  r1 = n // k
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))

Submission status: Wrong Answer

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

def process(a,b):
	a = gcd(a,b)
	for i in range(1000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif k == 1:
	print(0)
else:
	print(process(n,k))

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
if N == 1 or N == 0 or K ==1:
    print(0)
    sys.exit()

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

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

Submission status: Wrong Answer

Solution code:
import fractions
import sys
N,K = map(int,input().split())
L = []
L.append(N)
i = 0
if N == 0:
    print(0)
    sys.exit(0)
if K == 1:
    print(0)
    sys.exit(0)
Q = 0
while True:
  L.append(abs(N-K))
  N = abs(N-K)
  i += 1
  if i > 10**5:
    Q = min(L)
    break
if N%2 == 0 and K % 2 == 0:
    print(min(2,N,Q,abs(N-K)))
    sys.exit(0)
else:
    print(min(abs(N-K),N,Q))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
ZET=N
b=0
c=N
b1=N
for i in range(1000000000000000000):
  c=abs(b1-K)
  if b>=c:
    break
  b1=c
  b=c
print(c)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
from math import ceil 
if N > K:
    p = ceil(N/K - 1)
    N_p = N - p*K
    N_q  = -(N_p - K)
    ans = min(N_p, N_q)
else:
    ans = min(N, K-N)
print(ans)

Submission status: Wrong Answer

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

if k == 1:
  print(0)
elif n < k:
  r1 = n
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(r1, r2, min(r1,r2))
else:
  r1 = n // k
  r2 = n - k
  if r2 < 0:
    r2 *= -1
  print(min(r1,r2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if k > n:
  if abs(k-n)<=n:
    print(abs(k-n))
  else:
    print(n)
  
else:
  amari = n%k
  amari_minus = amari -k
  if abs(amari)<= (amari_minus):
    print(abs(amari))
  else:
    print(abs(amari_minus))

Submission status: Wrong Answer

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

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

if N < K:
        print(N)
else:
        for i in range(100000000):
                a = min(a, abs(a - K)) 
        print(a)

Submission status: Wrong Answer

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

if N == K:
    print(0)
elif N > K:
    if K == 1:
        print(0)
    else:
        print(N % (N-K))
else:
    PN = float('inf')
    while PN > N:
        PN = N
        N = abs(N-K)
    print(PN)

Submission status: Wrong Answer

Solution code:
import sys
import math

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

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

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

while 1:
    if pre < abs(pre-k):
        print(pre)
        sys.exit()
    pre = abs(pre - k)

Submission status: Wrong Answer

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

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

a = abs(N - K)
ans1 = N % a
ans2 = abs((N % a) - a)
if K == 1:
    print(0)
else:
    print(min(ans1, ans2))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, K = map(int, rl().split())
    if N % K == 0:
        print(0)
        exit()

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


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
if(N<K):
    print(N)
elif(K==1):
    print(0)
elif(N>=K):
    d = int(N//K)
    a = N%K
    N = N - (d*K)
    print(abs(N-K))

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
if k > n:
    print(n)
elif n % k == 0:
    print(0)
else:
    q = n % k
    p = n - q
    print(min(p, q))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
tmp=0
if K!=1:
    while N>0:
        tmp =N-K
        if tmp<0:
            print(tmp+K)
            exit(0)
        else:
            N=abs(N-K)
else:
    print(0)

Submission status: Wrong Answer

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

if n/k >= 1.0:
    amari = n%k
else:
    amari = n

if abs(amari-k) < amari:
    print(abs(amari-k))
print(amari)

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
if N == 0 or M == 1:
    print(0)
elif N*2 <= M:
    print(N)
else:
    a = N//M
    p = N - a
    print(min(p,abs(p-N)))

Submission status: Wrong Answer

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


def mod(n, k):
    while k:
        n, k = k, n % k
    return n


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

    print(0 if m == K else m)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
if K == 1 or N == 0:
    print(0)
    exit()
elif K == 0:
    print(N)
    exit()
elif N == 1:
    print(1)
    exit()
else:
    l = []
    ans = N
    for i in range(min(N,K)):
        ans = abs(ans - K)
        l.append(ans)
    print(min(l))

Submission status: Wrong Answer

Solution code:
n,k = map(str, input().split(' '))
k = int(k)
n = int(list(n)[-1])
lis = []
for i in range(10):
  lis.append(abs(n))
  n-=k
print(min(lis))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import fractions

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

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

else:
    print(fractions.gcd(K, N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if n < k:
    print(n)
elif k == 1 or n == 0 or (k % 2 == 0 and n % 2 == 0):
    print(0)
elif (k % 2 == 0 and n % 2 == 1) or (n % 2 == 0 and k % 2 == 1):
    print(1)

Submission status: Wrong Answer

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

if 2 * N <= K: print(N)
else: print(K * math.ceil(N / K) - N)

Submission status: Wrong Answer

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

a = int(N / K)

b = abs(int(N - K * a))
c = abs(int(b - K))

print(min(b,c))