Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
h = int(input())

log = {'1': 1 , '2': 3, '3': 3, '4': 7 }
i = 1
for i in range(1, h+1, 1):
    if not (str(i) in log):
        n = i // 2
        log.setdefault(str(i), log[str(n)]*2 +1)
        ##print("N(" + str(i)+ "):" + str(log[str(i)]))

print(log[str(h)])


Fixed submission:
def input_int():
    return int(input())

def suss(num):
  result = 2
  while num > 1:
    num = num // 2
    result = result * 2
  return result - 1

h = input_int()
print(suss(h))

Buggy python submission (verdict: Time Limit Exceeded):
h = int(input())

log = {'1': 1 , '2': 3, '3': 3, '4': 7 }
i = 1
for i in range(1, h+1, 1):
    if not (str(i) in log):
        n = i // 2
        log.setdefault(str(i), log[str(n)]*2 +1)
        ##print("N(" + str(i)+ "):" + str(log[str(i)]))

print(log[str(h)])


Fixed submission:
def input_int():
    return int(input())

def suss(num):
  result = 2
  while num > 1:
    num = num // 2
    result = result * 2
  return result - 1

h = input_int()
print(suss(h))

Buggy python submission (verdict: Time Limit Exceeded):
h = int(input())

log = {'1': 1 , '2': 3, '3': 3, '4': 7 }
i = 1
for i in range(1, h+1, 1):
    if not (str(i) in log):
        n = i // 2
        log.setdefault(str(i), log[str(n)]*2 +1)
        ##print("N(" + str(i)+ "):" + str(log[str(i)]))

print(log[str(h)])


Fixed submission:
def input_int():
    return int(input())

def suss(num):
  result = 2
  while num > 1:
    num = num // 2
    result = result * 2
  return result - 1

h = input_int()
print(suss(h))

Buggy python submission (verdict: Time Limit Exceeded):
h = int(input())

log = {'1': 1 , '2': 3, '3': 3, '4': 7 }

i = 1
while i <= h:
  if str(i) in log:
    hoge = 1
    # print(log[str(i)])
  else:
    n = i // 2
    log.setdefault(str(i), log[str(n)]*2 +1)
    # print(log[str(i)])
  i=i+1
print(log[str(h)])



Fixed submission:
def input_int():
    return int(input())

def suss(num):
  result = 2
  while num > 1:
    num = num // 2
    result = result * 2
  return result - 1

h = input_int()
print(suss(h))

Buggy python submission (verdict: Runtime Error):
h = int(input())

log = {'1': 1 , '2': 3, '3': 3, '4': 7 }

i = 1
while i <= h:
  if str(i) in log:
    # print(log[str(i)])
  else:
    n = i // 2
    log.setdefault(str(i), log[str(n)]*2+1)
    # print(log[str(i)])
  i=i+1
print(log[h])



Fixed submission:
def input_int():
    return int(input())

def suss(num):
  result = 2
  while num > 1:
    num = num // 2
    result = result * 2
  return result - 1

h = input_int()
print(suss(h))
Buggy python submission (verdict: Runtime Error):
n = int(input())
x = []
u = []
s = 0
for i in range(n):
    x.append(floart(input())
    u.append(input())
    if u == 'JPY':
      s = s + int(x)
    if u == 'BTC':
      s = s + ( x *  380000.0) 

print(s)

Fixed submission:
n = int(input())
x = []
u = []
s = 0
for i in range(n):
    x, u = map(str, input().split())
    if u == 'JPY':
      s = s + int(x)
    if u == 'BTC':
      s = s + ( float(x) *  380000.0) 

print(s)

Buggy python submission (verdict: Runtime Error):
n = int(input())
x = []
u = []
s = 0
for i in range(n):
    x.append(floart(input())
    u.append(input())
    if u[i] == 'JPY':
      s = s + int(x)
    if u[i] == 'BTC':
      s = s + ( x *  380000.0) 

print(s)

Fixed submission:
n = int(input())
x = []
u = []
s = 0
for i in range(n):
    x, u = map(str, input().split())
    if u == 'JPY':
      s = s + int(x)
    if u == 'BTC':
      s = s + ( float(x) *  380000.0) 

print(s)

Buggy python submission (verdict: Runtime Error):
n = int(input())
x = []
u = []
s = 0
for i in range(n):
    x.append(input())
    u.append(input())
    if u[i] == 'JPY':
      s = s + int(x)
    if u[i] == 'BTC':
      s = s + ( float(x) *  380000.0) 

print(s)

Fixed submission:
n = int(input())
x = []
u = []
s = 0
for i in range(n):
    x, u = map(str, input().split())
    if u == 'JPY':
      s = s + int(x)
    if u == 'BTC':
      s = s + ( float(x) *  380000.0) 

print(s)
Buggy python submission (verdict: Time Limit Exceeded):
nums = list(map(int, input().split(' ')))
k = input()
k = int(k)
i = 0

while i < k:
  maxindex = max(enumerate(nums), key = lambda x:x[1])[0]
  nums[maxindex] *= 2

print(sum(nums))


Fixed submission:
nums = list(map(int, input().split(' ')))
k = input()
k = int(k)
i = 0

while i in range(k):
  maxindex = max(enumerate(nums), key = lambda x:x[1])[0]
  nums[maxindex] *= 2
  i = i + 1

print(sum(nums))
Buggy python submission (verdict: Runtime Error):
# coding: utf-8

N = input()
print(N*(N-1) / 2)

Fixed submission:
N = int(input())

print(N*(N-1) // 2)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
N = int(input())

res = sum(range(N))

print(res)

Fixed submission:
N = int(input())

print(N*(N-1) // 2)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
N = int(input())

P = [i+1 for i in range(N)]

res = 0
for i in range(N-1):
    res += (i+1) % P[i+1]

print(res)

Fixed submission:
N = int(input())

print(N*(N-1) // 2)
Buggy python submission (verdict: Runtime Error):
# coding: utf-8

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

print(max(A+B, A-B, A*B)

Fixed submission:
# coding: utf-8
 
A, B = list(map(int, input().split()))
 
print(max(A+B, A-B, A*B))
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import copy

N = int(input())


ss = list()

base = dict([[chr(ord('a') + i),  0] for i in range(26)])
for k in range(N):
    a = copy.copy(base)
    s = input()
    for c in s:
        a[c] += 1

    ss.append(a)

res = 0
for i in range(N-1):
    for j in range(i+1, N):
        flag = True
        for k in range(26):
            c = chr(ord('a')+k)
            if ss[i][c] != ss[j][c]:
                flag = False
                break
        if flag:
            res += 1


print(res)

Fixed submission:
# coding: utf-8
import copy

N = int(input())

d = dict()

res = 0
for i in range(N):
    s = input()
    s = ''.join(sorted(s))

    if s in d.keys():
        res += d[s]
        d[s] += 1
    else:
        d[s] = 1

print(res)
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import math
 
N = int(input())
A = list(map(int, input().split()))
A_sum = sum(A)
 
res = [0 for i in range(N)]
 
for i in range(N):
    res[i] = A_sum - 2 * sum([A[(i+(2*j+1)) % N] for j in range(int((N-1)/2))])
 
print(' '.join(map(str, res)))

Fixed submission:
# coding: utf-8
import math

N = int(input())
A = list(map(int, input().split()))
A_sum = sum(A)

res = list()
res.append(A_sum - 2 * sum([A[(2*j+1)] for j in range(int((N-1)/2))]))

for i in range(1, N):
    res.append(2*A[i-1] - res[i-1])

print(' '.join(map(str, res)))

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import math

N = int(input())
A = list(map(int, input().split()))
A_sum = sum(A)

res = [0 for i in range(N)]

for i in range(N):
    res[i] = A_sum - 2 * sum([A[(i+(2*j+1)) % N] for j in range(int((N-1)/2))])

print(' '.join(map(str, res)))

Fixed submission:
# coding: utf-8
import math

N = int(input())
A = list(map(int, input().split()))
A_sum = sum(A)

res = list()
res.append(A_sum - 2 * sum([A[(2*j+1)] for j in range(int((N-1)/2))]))

for i in range(1, N):
    res.append(2*A[i-1] - res[i-1])

print(' '.join(map(str, res)))
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8

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

b = []
if n % 2 == 0:
    for i in range(n):
        if i % 2 != 0:
            b.insert(0, a[i])
        else:
            b.append(a[i])
else:
    for i in range(n):
        if i % 2 != 0:
            b.append(a[i])
        else:
            b.insert(0, a[i])

print(' '.join(map(str, b)))


Fixed submission:
# coding: utf-8
from collections import deque

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

b = deque()
for i in range(n):
    if i % 2 == (n-1) % 2:
        b.appendleft(a[i])
    else:
        b.append(a[i])

print(' '.join(map(str, b)))

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8

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

b = []
for i in range(n):
    if i % 2 != 0:
        b.insert(0, a[i])
    else:
        b.append(a[i])

print(' '.join(map(str, b if n % 2 == 0 else reversed(b))))


Fixed submission:
# coding: utf-8
from collections import deque

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

b = deque()
for i in range(n):
    if i % 2 == (n-1) % 2:
        b.appendleft(a[i])
    else:
        b.append(a[i])

print(' '.join(map(str, b)))
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
print map(str, [x[0] for x in map(str, raw_input().split())]).upper()

Fixed submission:
# coding: utf-8
print ''.join([x[0] for x in map(str, raw_input().split())]).upper()
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
(a,b,c) = int(raw_input().split())
print "YES" if b - a == c - b else "NO"

Fixed submission:
# coding: utf-8
(a,b,c) = map(int, raw_input().split())
print "YES" if b - a == c - b else "NO"
Buggy python submission (verdict: Time Limit Exceeded):
N= int(raw_input())
a = map(int, raw_input().split())
a.sort()

while(True):
    is_finish = False
    mn = min(a)
    while(a.count(mn) == 1):
        idx = a.index(mn)+1
        if idx >= len(a):
            mn = min(a)
            break
        else:
            mn = a[idx]

    mx = max(a)
    while(a.count(mx) == 1):
        idx = a.index(mx) - 1
        if idx < 0 or mx == mn :
            mx = max(a)
            is_finish = True
            break
        else :
            mx = a[idx]

    if is_finish :
        break
    else:
        a.remove(mn)
        a.remove(mx)

print len(a)


Fixed submission:
N = int(raw_input())
a = set(map(int, raw_input().split()))
print len(a) if len(a) % 2 != 0 else len(a)-1

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

N = int(raw_input())
a = map(int, raw_input().split())
a.sort()

while(True):
    mn = min(a)
    while(a.count(mn) == 1):
        try:
            mn = a[a.index(mn)+1]
        except:
            mn = min(a)
            break

    mx = max(a)
    while(a.count(mx) == 1):
        try:
            mx = a[a.index(mx)-1]
        except:
            mx = max(a)
            break

    a.remove(mn)
    a.remove(mx)

    count = Counter(a)
    if max(count.values()) == 1:
        break

print len(a)


Fixed submission:
N = int(raw_input())
a = set(map(int, raw_input().split()))
print len(a) if len(a) % 2 != 0 else len(a)-1
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N = int(input())
L = np.array(list(map(int, input().split())))

ans = 0

L = np.sort(L)

for a in range(N-2):
    for b in range(a+1, N-1):
        ans += sum((L < (L[a]+L[b])) * (L > L[b]))

print(ans)


Fixed submission:
# https://takeg.hatenadiary.jp/entry/2019/11/22/212816

import bisect

def solve():
    N = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()

    # O(N^2 * logN)の全探索で解く
    ans = 0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass

    print(ans)

if __name__ == "__main__":
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = list(map(int, input().split()))

ans = 0

for a in range(N):
    for b in range(a+1, N):
        for c in range(b+1, N):
            if L[a] < L[b] + L[c] and L[b] < L[c] + L[a] and L[c] < L[a] + L[b]:
                ans += 1

print(ans)


Fixed submission:
# https://takeg.hatenadiary.jp/entry/2019/11/22/212816

import bisect

def solve():
    N = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()

    # O(N^2 * logN)の全探索で解く
    ans = 0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass

    print(ans)

if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Runtime Error):

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

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

points = [K for _ in range(N)]

for q in range(Q):
    A = int(input()) - 1
    points = [p if i == A else p-1 for i, p in enumerate(points)]
for p in points:
    if p < 1:
        print('No')
    else:
        print('Yes')


Fixed submission:
S = input()

weather = ['Sunny', 'Cloudy', 'Rainy']

if S == weather[0]:
    print(weather[1])
elif S == weather[1]:
    print(weather[2])
elif S == weather[2]:
    print(weather[0])

Buggy python submission (verdict: Runtime Error):
import numpy as np

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

points = np.array([K for _ in range(N)])

for q in range(Q):
    A = int(input()) - 1
    points -= 1
    points[A] += 1

for n in range(N):
    if points[n] < 1:
        print('No')
    else:
        print('Yes')


Fixed submission:
S = input()

weather = ['Sunny', 'Cloudy', 'Rainy']

if S == weather[0]:
    print(weather[1])
elif S == weather[1]:
    print(weather[2])
elif S == weather[2]:
    print(weather[0])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math
def input():
    return sys.stdin.readline()[:-1]

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

A = list(map(int, input().split()))

for _ in range(M):
    A[np.argmax(A)] = math.floor(max(A) / 2)

print(sum(A))

Fixed submission:
import sys
import numpy as np
import math
import heapq
def input():
    return sys.stdin.readline()[:-1]

N, M = map(int, input().split())
A = list(map(lambda a: int(a)*(-1), input().split()))
heapq.heapify(A)

for _ in range(M):
    heapq.heappush(A, math.trunc(heapq.heappop(A) / 2))

print(sum(A)*(-1))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
def input():
    return sys.stdin.readline()[:-1]

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

A = list(map(int, input().split()))

for _ in range(M):
    max_index = np.argmax(A)
    A[max_index] = int(A[max_index] / 2)

print(sum(A))

Fixed submission:
import sys
import numpy as np
import math
import heapq
def input():
    return sys.stdin.readline()[:-1]

N, M = map(int, input().split())
A = list(map(lambda a: int(a)*(-1), input().split()))
heapq.heapify(A)

for _ in range(M):
    heapq.heappush(A, math.trunc(heapq.heappop(A) / 2))

print(sum(A)*(-1))
Buggy python submission (verdict: Runtime Error):
import numpy as np
import math
from functools import reduce

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

n = int(input())

a = list(map(int, input().split(' ')))

devisor = gcd_list(a)

a = np.array(a) / devisor
child = a.prod()
child *= devisor

mother = 0

for m in range(n):
    mother += np.delete(a, m).prod()

x = child / mother

print(x)


Fixed submission:
import numpy as np
from fractions import gcd
from functools import reduce

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

n = int(input())

a = list(map(int, input().split(' ')))

devisor = gcd_list(a)

a = np.array(a) / devisor
child = a.prod()
child *= devisor

mother = 0

for m in range(n):
    mother += np.delete(a, m).prod()

x = child / mother

print(x)

Buggy python submission (verdict: Runtime Error):
import numpy as np
import math
from functools import reduce

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

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

n = int(input())

a = list(map(int, input().split(' ')))

devisor = gcd_list(a)

a = np.array(a) / devisor
child = a.prod()
child *= devisor

mother = 0

for m in range(n):
    mother += np.delete(a, m).prod()

x = child / mother

print(x)



Fixed submission:
import numpy as np
from fractions import gcd
from functools import reduce

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

n = int(input())

a = list(map(int, input().split(' ')))

devisor = gcd_list(a)

a = np.array(a) / devisor
child = a.prod()
child *= devisor

mother = 0

for m in range(n):
    mother += np.delete(a, m).prod()

x = child / mother

print(x)
Buggy python submission (verdict: Runtime Error):
num_list = [0] * (N)
a_b_list = []

for _ in range(N-1):
    a_b_list.append(list(map(int, input().split())))

for _ in range(Q):
    p, x = map(int, input().split())
    num_list[p-1] += x

for a_b in a_b_list:
    num_list[a_b[1]-1] += num_list[a_b[0]-1]

print(*num_list)

Fixed submission:
# dfs(深さ優先探索)

N, Q = map(int, input().split(' '))

num_list = [0] * (N+1)

tree_list = [[] for _ in range(N+1)]

for _ in range(N-1):
    a, b = map(int, input().split())
    tree_list[a].append(b)
    tree_list[b].append(a)

for _ in range(Q):
    p, x = map(int, input().split())
    num_list[p] += x

q = [1]
checked = [0] * (N+1)

while q:
    v=q.pop()
    checked[v] = 1
    for u in tree_list[v]:
        if checked[u] == 1:
            continue
        num_list[u] += num_list[v]
        q.append(u)

print(*num_list[1:])
Buggy python submission (verdict: Runtime Error):
A, B = map(int, input().split(' '))

answers = [A+B, A-B, A*B, A/B]
print(max(answers))

Fixed submission:
A, B = map(int, input().split(' '))

answers = [A+B, A-B, A*B]
print(max(answers))
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()

s = S.lstrip('.').rstrip('#')

w = s.count('.')
b = n - w

if w < b:
    print(w)
else:
    print(b)

Fixed submission:
a, b, c = map(int, input().split(' '))

if a > b and b > c and a > c:
    print('No')
if a > c and a > b and c > b:
    print('Yes')
elif b > a and b > c and a > c:
    print('No')
elif b > c and c > a and b > a:
    print('Yes')
elif c > a and c > b and a > b:
    print('No')
elif  c > b and c > a and b > a:
    print('No')
Buggy python submission (verdict: Runtime Error):
	#include<bits/stdc++.h>
	using namespace std;
	using ll = long long;
	#define forn(i,n) for(int i = 0; i< n;++i)
	int INF = 1e9 + 7;
	ll MOD = 998244353;


	int main(){
		ios_base::sync_with_stdio(false);
		cin.tie(NULL);
		int a,b,c,d;
		cin >> a >> b >> c >> d;

		vector<vector<ll>> dp(c+10,vector<ll>(d+10,0));
		
		for(int i = a; i <= c; ++i){
			for(int j = b; j <= d; ++j){
				if(i == a && j == b){
					dp[i][j] = 1;
					continue;
				}
				dp[i][j] = dp[i-1][j]*j + dp[i][j-1]*i - dp[i-1][j-1]*(i-1)*(j-1);
				//dp[i][j] %= MOD;
				dp[i][j] = (MOD + dp[i][j] % MOD) % MOD;
			}
		}
		cout << dp[c][d] << endl;
				
	}


Fixed submission:
a,b,c,d = [int(x) for x in input().split()]
MOD = 998244353
dp = [[0 for i in range(d+10)] for j in range(c+10)]

for i in range(a,c+1):
    for j in range(b,d+1):
        # print(str(i) +'  '+str(j))
        if i == a and j == b:
            dp[i][j] = 1
            continue
        dp[i][j] = dp[i-1][j]*j + dp[i][j-1]*i - dp[i-1][j-1]*(i-1)*(j-1)
        dp[i][j] %= MOD
print(dp[c][d])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
INF = 987456325585
arr  = [int(x) for x in input().split()]

# dp[i][j] -> array is left with elements from index i to j, contains max of x-y if player x starts from here

# dp[0][n-1] -> res

dp = []
for i in range(n):
    dp.append([-INF]*n)

for i in range(n-1,-1,-1):
    for j in range(i,n):
        if i == j:
            dp[i][j] = arr[i]
            continue
        dp[i][j] = max(arr[i] - dp[i+1][j], arr[j] - dp[i][j-1])

print(dp[0][n-1])


Fixed submission:
n = int(input())
INF = 987456325585
arr  = [int(x) for x in input().split()]

# dp[i][j] -> array is left with elements from index i to j, contains max of x-y if player x starts from here

# dp[0][n-1] -> res

dp = []
for i in range(n):
    dp.append([-INF]*n)

for i in range(n-1,-1,-1):
    for j in range(i,n):
        if i == j:
            dp[i][j] = arr[i]
            continue
        dp[i][j] = max(arr[i] - dp[i+1][j], arr[j] - dp[i][j-1])

print(dp[0][n-1])
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split(' '))

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

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

    if N*2 == K:
        print(N)
        return

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

    x = N
    min = x
    while min >= x:
        #print(min, x)
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)


Fixed submission:
N, K = map(int, input().split(' '))

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

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

    if N*2 == K:
        print(N)
        return

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

    x = N % K
    min = x
    while min >= x:
        #print(min, x)
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split(' '))

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

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

    if N*2 == K:
        print(N)
        return

    if N%K == 0:
        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)


Fixed submission:
N, K = map(int, input().split(' '))

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

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

    if N*2 == K:
        print(N)
        return

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

    x = N % K
    min = x
    while min >= x:
        #print(min, x)
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)

Buggy python submission (verdict: Time Limit Exceeded):
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)


Fixed submission:
N, K = map(int, input().split(' '))

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

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

    if N*2 == K:
        print(N)
        return

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

    x = N % K
    min = x
    while min >= x:
        #print(min, x)
        min = x
        x = absdiff(x, K)
        if min == 1:
            break

    print(min)
    return


culc(N, K)
Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())

prev_c = S[0]
num_changes = 0
for c in S[1:]:
    if c == prev_c:
        num_changes += 1
        prev_c = 'replaced'
    else:
        prev_c = c

total_changes = 0
if S[0] == prev_c and S[1] == S[0] and len(S) % 2 == 1:
    num_changes2 = 0
    for c in S:
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes * int(K/2 + 1) + num_changes2 * int(K/2)
elif S[0] == prev_c:
    num_changes2 = 0
    for c in S:
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes + num_changes2 * (K-1)
else:
    total_changes = num_changes * K

print(total_changes)


Fixed submission:
S = input()
K = int(input())

prev_c = S[0]
num_changes = 0
for c in S[1:]:
    if c == prev_c:
        num_changes += 1
        prev_c = 'replaced'
    else:
        prev_c = c

total_changes = 0
if len(S) == 1:
    if len(S) % 2 == 0:
        total_changes = int(K/2)
    else:
        total_changes = int((K-1)/2)
elif S[0] == prev_c and S[1] == S[0] and len(S) % 2 == 1:
    num_changes2 = 0
    for c in S:
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes * int(K/2 + 1) + num_changes2 * int(K/2)
elif S[0] == prev_c:
    num_changes2 = 0
    for c in S:
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes + num_changes2 * (K-1)
else:
    total_changes = num_changes * K

print(total_changes)