Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

a,b=map(int,input().split())
L=[]
l=[]
ll=[]
u=0
for i in range(b):
    u+=1
    c=list(map(int,input().split()))
    c.append(u)
    L.append(c)
L.sort(key=lambda x:x[0])
for i in range(1,a+1):
    lll=[]
    for j in L:
        if j[0]==i:
            lll.append(j)
    ll.append(lll)
for i in ll:
    i.sort(key=lambda x:x[1])
for i in ll:
    e=0
    for j in i:
        e+=1
        j.append(e)
q=[]
for w in ll:
   q.extend(w) 
q.sort(key=lambda x:x[2])
for r in range(b):
        print('0'*(6-len(str(q[r][0])))+str(q[r][0])+'0'*(6-len(str(q[r][3])))+str(q[r][3]))

### FIXED CODE ###

a,b=map(int,input().split())
L=[[] for i in range(a)]
u=0
for i in range(b):
    u+=1
    c=list(map(int,input().split()))
    c.append(u)
    L[int(c[0])-1].append(c)
for i in L:
    i.sort(key=lambda x:x[1])
for i in L:
    e=0
    for j in i:
        e+=1
        j.append(e)
q=[]
for w in L:
   q.extend(w) 
q.sort(key=lambda x:x[2])
for r in range(b):
        print('0'*(6-len(str(q[r][0])))+str(q[r][0])+'0'*(6-len(str(q[r][3])))+str(q[r][3]))
### BUGGY CODE ###

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

rode=[[0 for _ in range(N)] for _ in range(N)]

for j in range(M):
    a,b=map(int,input().split())
    rode[a-1][b-1]=rode[b-1][a-1]=1

def trv(i):
  maxh=H[i]
  for j in range(N):
    if rode[i][j]>0:
        if H[j]>=maxh:
          return False
  return True

goodv = [1 for i in range(N) if trv(i) ]
print(len(goodv))

### FIXED CODE ###

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

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

for j in range(M):
    a,b=map(int,input().split())
    dmax[a-1]=max(dmax[a-1],H[b-1])
    dmax[b-1]=max(dmax[b-1],H[a-1])    
          
goodv = [1 for i in range(N) if dmax[i]<H[i]]
print(len(goodv))
### BUGGY CODE ###

from copy import deepcopy as de
n,m =[int(i) for i in input().split()]
s=[int(i) for i in input().split()]
t=[int(i) for i in input().split()]
a=[1]*(m+1)
for i,b in enumerate(s):
  d=de(a)
  k=0
  for j,c in enumerate(t):
    d[j]=(k+d[j])%1000000007
    if b==c:
      k=(k+a[j])%1000000007
  d[-1]=(k+d[-1])%1000000007
  a=de(d)
print(a[-1]%1000000007)
    


### FIXED CODE ###

n,m =[int(i) for i in input().split()]
s=[int(i) for i in input().split()]
t=[int(i) for i in input().split()]
a=[1]*(m+1)
for b in s:
  d=a[:]
  k=0
  for j,c in enumerate(t):
    d[j]=(k+d[j])%1000000007
    if b==c:
      k=(k+a[j])%1000000007
  d[-1]=(k+d[-1])%1000000007
  a=d[:]
print(a[-1])
### BUGGY CODE ###

n, m, q = map(int, input().split())

query = []
for i in range(q):
    a, b, c, d = map(int, input().split())
    query.append([a - 1, b - 1, c, d])

def bit_iter(x):
    while x:
        yield x & 1
        x >>= 1


scores = []
for state in range(1 << q):
    A = [None] * n
    score = 0
    for i, v in enumerate(bit_iter(state)):
        if v:
            a, b, c, d = query[i]
            if A[a] is None:
                if A[b] is None:
                    A[a] = 1
                else:
                    A[a] = A[b] - c

            if A[a] and A[b] and A[b] - A[a] != c:
                break

            A[b] = c + A[a]
            if A[a] < 1 or A[b] > m:
                break
            score += d
    else:
        scores.append(score)
print(max(scores))


### FIXED CODE ###

n, m, q = map(int, input().split())

query = []
for i in range(q):
    a, b, c, d = map(int, input().split())
    query.append([a - 1, b - 1, c, d])


results = []
def f(x, A, depth=0):
    if depth == n:
        # print(A, "---------" if A == [1, 3, 4] else '')
        score = 0
        for a, b, c, d in query:
            # print(A[b], A[a], A[b] - A[a], c, ":", d)
            if A[b] - A[a] == c:
                score += d
        results.append(score)
        return

    for i in range(1, x + 1):
        A[n - depth - 1] = i
        f(i, A, depth + 1)

f(m, [0] * n)
print(max(results))
### BUGGY CODE ###

s = input()
n = len(s)
mod = 10 ** 9 + 7
amari = [1]
for i in range(n - 1):
    amari.append(amari[-1] * 10 % 13)
amari.reverse()
dp = [[0 for i in range(13)] for j in range(n + 1)]
dp[0][0] = 1
for i in range(n):
    for j in range(13):
        if s[i] == "?":
            temp = 0
            for k in range(10):
                temp += dp[i][(j - amari[i] * k) % 13]
                temp %= mod
            dp[i + 1][j] = temp
        else:
            num = int(s[i])
            dp[i + 1][j] = dp[i][(j - amari[i] * num) % 13]
print(dp[n][5])

### FIXED CODE ###

s = input()
n = len(s)
mod = 10 ** 9 + 7
amari = [1]
for i in range(n - 1):
    amari.append(amari[-1] * 10 % 13)
amari.reverse()
dp = [[0 for i in range(13)] for j in range(n + 1)]
dp[0][0] = 1
for i in range(n):
    for j in range(13):
        if s[i] == "?":
            temp = 0
            for k in range(10):
                temp += dp[i][(j - amari[i] * k) % 13]
                temp %= mod
            dp[i + 1][j] = temp
        else:
            num = int(s[i])
            dp[i + 1][j] = dp[i][(j - amari[i] * num) % 13]
print(dp[n][5])
### BUGGY CODE ###

N = int(input())
l = []
for i in range(N):
    l.append(str(input()))
res = []
max = 0
for s in l:
    num = l.count(s)
    if max > num:
        continue
    elif max == num:
        res.append(s)
    else:
        res = [s]
        max = num
res = list(set(res))
res.sort()
for r in res:
    print(r)


### FIXED CODE ###

N = int(input())
d = {}
for i in range(N):
    s = str(input())
    if not s in d:
        d[s] = 1
    else:
        d[s] = d[s] + 1
max = 0
res = []
for key in d:
    num = d[key]
    if num < max:
        continue
    elif num == max:
        res.append(key)
    else:
        max = num
        res = [key]
res.sort()
for r in res:
    print(r)
### BUGGY CODE ###

# coding: utf-8
# Your code here!

from collections import deque

N = int(input())
P = deque(list(map(int, input().split())))
ctr = 0
Array = []

for i in range(N):
    Array.append(P.popleft())
    if Array[-1] == min(Array):
        ctr += 1
print(ctr)

### FIXED CODE ###

# coding: utf-8
# Your code here!

from collections import deque

N = int(input())
P = deque(list(map(int, input().split())))
ans, tmp, minnum = 0, 0, pow(10,9)
Array = []

for i in range(N):
    tmp = P.popleft()
    Array.append(tmp)
    if tmp < minnum:
        minnum = tmp
    if Array[-1] == minnum:
        ans += 1
print(ans)
### BUGGY CODE ###

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

if 0 in S:
    print(0)
elif 10 ** 18 in S:
    print(-1)
else:
    ans = 1
    for s in S:
        ans *= s
    if ans > 10 ** 18:
        print(-1)
    else:
        print(ans)

### FIXED CODE ###

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

def f():
    if 0 in S:
        return 0
    elif 10 ** 18 in S:
        return -1
    else:
        ans = 1
        for s in S:
            ans *= s
            if ans > 10 ** 18:
                return -1
        return ans

print(f())
### BUGGY CODE ###

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

for i in range(1,n):
  for j in range(0,i):
    st=j
    while st+i<=n-1:
      x=a[st]
      y=a[st+i]
      if x+y==i:
        ans+=1
      st=st+i
      
print(ans)

### FIXED CODE ###

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

l=[]
r=[]

for i in range(n):
  nu=i+1
  x=a[i]
  l.append(x+nu)
  r.append(nu-x)
  
di1={}
di2={}

for i in range(0,n):
  if i==n-1:
    continue
  else:
    if l[i] not in di1:
      di1[l[i]]=0
    di1[l[i]]+=1
    if r[i+1] not in di2:
      di2[r[i+1]]=0
    di2[r[i+1]]+=1
    if r[i+1] in di1:
      ans+=di1[r[i+1]]
      
print(ans)
### BUGGY CODE ###

import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if s0p<=0:
  while s0p<=0:
    s0p+=1
    countp+=1

if s0n>=0:
  while s0n>=0:
    s0n-=1
    countn+=1

for i in range(1,n):
  s1 = s0p+a[i]
  """print("i",i,"a[i]",a[i],"s0p",s0p,"s1",s1,"countp",countp)"""
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  """print("i",i,"b[i]",b[i],"s0n",s0n,"s1",s1,"countn",countn)"""
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))





### FIXED CODE ###

import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n-1)
  exit()

if s0p<=0:
  s0p+=(abs(a[0])+1)
  countp+=(abs(a[0])+1)
  
if s0n>=0:
  s0n-=(abs(b[0])+1)
  countn+=(abs(b[0])+1)

for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]
  
print(countp if countp<=countn else(countn))
### BUGGY CODE ###

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

lia = [1 if s == 'A' else 0 for s in S]
lis = [1 if s == 'C' else 0 for s in S]
liac = [a * c for a, c in zip (lia[:-1], lis[1:])]

for i in range(Q):
  l, r = map(int, input().split())
  print(sum(liac[l-1:r-1]))

### FIXED CODE ###

#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = int(input())
n, q  = map(int, input().split())
s  = input()
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

nac = []
c = 0
for i, s_ in enumerate(s):
    if s[i] == 'C':
        if s[i-1] == 'A':
            c += 1

    nac.append(c)

#print(nac)

for _ in range(q):
    l, r = map(int, input().split())
    print(nac[r-1] - nac[l-1])
### BUGGY CODE ###

H, W = map(int, input().split())
S = list()
S.append("#" * (W+2))
for i in range(H):
  S.append("#"+input()+"#")
S.append("#" * (W+2))
ans = 1

for i in range(1,H+1):
  for j in range(1, W+1):
    if S[i][j] == ".":
      tmp = 1
      k = 1
      while S[i][j+k] == ".":
        tmp += 1
        k += 1
      k = 1
      while S[i][j-k] == ".":
        tmp += 1
        k += 1
      k = 1
      while S[i+k][j] == ".":
        tmp += 1
        k += 1
      k = 1
      while S[i-k][j] == ".":
        tmp += 1
        k += 1
      ans = max(tmp, ans)
print(ans)

### FIXED CODE ###

H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
L = [[0]*W for i in range(H)]
R = [[0]*W for i in range(H)]
U = [[0]*W for i in range(H)]
D = [[0]*W for i in range(H)]
LAMP = [[0]*W for i in range(H)]
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      L[i][j] = 0
    elif j == 0:
      L[i][j] = 1
    else:
      L[i][j] = L[i][j-1] + 1
    if S[i][j] == "#":
      U[i][j] = 0
    elif i == 0:
      U[i][j] = 1
    else:
      U[i][j] = U[i-1][j] + 1
      
for i in range(H-1,-1,-1):
  for j in range(W-1,-1,-1):
    if S[i][j] == "#":
      R[i][j] = 0
    elif j == W-1:
      R[i][j] = 1
    else:
      R[i][j] = R[i][j+1] + 1
    if S[i][j] == "#":
      D[i][j] = 0
    elif i == H-1:
      D[i][j] = 1
    else:
      D[i][j] = D[i+1][j] + 1
    if S[i][j] != "#":
       ans = max(ans,L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)

print(ans)
### BUGGY CODE ###

def main():
    k = int(input())
    que = [1,2,3,4,5,6,7,8,9]
    cnt = 0
    while cnt!= k:
        x = que.pop(0)
        cnt += 1
        if x%10!=0:
            que.append(x*10+x%10-1)
        que.append(x*10+x%10)
        if x%10!=9:
            que.append(x*10+x%10+1)
    print(x)

            
if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    from queue import Queue
    k = int(input())
    q = Queue()
    for i in range(1,10):
        q.put(i)
    cnt = 0
    while cnt!= k:
        x = q.get()
        cnt += 1
        if x%10!=0:
            q.put(x*10+x%10-1)
        q.put(x*10+x%10)
        if x%10!=9:
            q.put(x*10+x%10+1)
    print(x)

            
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import numpy as np
import sys
input = sys.stdin.readline
 
n = int(input())
a = list(map(int,input().split()))
ans = 1

for i in a:
    ans *= i
    if ans > 10**18:
        ans = -1
        exit
    elif ans == 0:
        exit
 
print(ans)

### FIXED CODE ###

import numpy as np
import sys
input = sys.stdin.readline
 
n = int(input())
a = list(map(int,input().split()))
ans = 1

for i in a:
    ans *= i
    if ans > 10**18:
        ans = -1
        break
 
if 0 in a:
    ans = 0

print(ans)
### BUGGY CODE ###

import math

N, Q = map(int, input().split())
c = list(map(int, input().split()))

M = int(math.sqrt(N))

cs = []
for i in range(0, N, M):
    cs.append(set(c[i:min(i+M, N)]))

for i in range(Q):
    l, r = map(int, input().split())

    li = l // M
    ri = r // M

    if li == ri:
        ans = len(set(c[l-1:r]))
    else:
        s = set(c[l-1:(li+1)*M])
        for j in range(li+1, ri):
            s.update(cs[j])
        s.update(c[ri*M:r])
        ans = len(s)

    print(ans)

### FIXED CODE ###

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n+1)

    def sum(self, i):  # sum in [0, i)
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):  # i > 0
        assert i > 0
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


N, Q = map(int, input().split())
c = list(map(int, input().split()))

lr = []
for i in range(Q):
    l, r = map(int, input().split())
    lr.append([l-1, r-1, i])

lr.sort(key=lambda x: x[1])

rightest = [-1] * (N + 1)
current_q = 0
bit = Bit(N)
ans = [0] * Q

for i in range(N):
    if rightest[c[i]] != -1:
        bit.add(rightest[c[i]]+1, -1)
    rightest[c[i]] = i
    bit.add(i+1, 1)

    while current_q < Q and lr[current_q][1] == i:
        ans[lr[current_q][2]] = bit.sum(lr[current_q][1] + 1) - bit.sum(lr[current_q][0])
        current_q += 1

for i in range(Q):
    print(ans[i])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N, Q = [int(x) for x in input().split()]

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

cost = [0] * N
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    cost[p - 1] += x

next = [0]
for i in next:
    for v in edge[i]:
        if v in next:
            continue
        next.append(v)
        cost[v] += cost[i]

print(" ".join(map(str, cost)))





### FIXED CODE ###

import sys
input = sys.stdin.readline

N, Q = [int(x) for x in input().split()]

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

cost = [0] * N
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    cost[p - 1] += x

next = [0]
s_next = {0}
for i in next:
    for v in edge[i]:
        if v in s_next:
            continue
        s_next.add(v)
        next.append(v)
        cost[v] += cost[i]

print(" ".join(map(str, cost)))
### BUGGY CODE ###

import sys
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,K=LI()
As=LI()
As.sort()
Fs=LI()
Fs.sort(reverse=True)

HQ=[]

for i in range(N):
    heapq.heappush(HQ, (-As[i]*Fs[i], As[i], Fs[i]))

# print(HQ)
while K:
    score,A,F=heapq.heappop(HQ)
    if HQ:
        score2,A2,F2=HQ[0]
        diff = min((score2 - score) // F + 1, K)

        heapq.heappush(HQ, (-(A-diff)*F , A-diff, F))
        K = K - diff
    else:
        print(max(A-K,0)*F)
        exit()

ans, _, _ =heapq.heappop(HQ)
print(-ans)


### FIXED CODE ###

import sys
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,K=LI()
As=LI()
As.sort()
Fs=LI()
Fs.sort(reverse=True)

rgt = 10**12
lft = 0
while lft < rgt:
    cost = K
    half = (lft + rgt) // 2
    for i in range(N):
        if As[i]*Fs[i] > half:
            cost -= (As[i]*Fs[i] - half) // Fs[i]
            cost -= 1 if (half - As[i]*Fs[i]) % Fs[i] > 0 else 0

    if cost < 0:
        lft = half + 1
    else:
        rgt = half
    # print(lft, rgt)

print(rgt)
### BUGGY CODE ###

n = int(input())
s = list(input())
c = 0

for i in range(1000) :
    num_1 = i // 100
    num_2 = (i - num_1*100) // 10
    num_3 = (i - num_1*100 - num_2*10) % 100
    for j in range(n-2) :
        if int(s[j]) == num_1 :
            for k in range(j+1,n-1) :
                if int(s[k]) == num_2 :
                    for l in range(k+1,n) :
                        if int(s[l]) == num_3 :
                            c += 1
                            break
                    break
            break

print(c)


### FIXED CODE ###

n = int(input())
s = list(input())
c = 0

for i in range(1000) :
    num_1 = i // 100
    num_2 = (i - num_1*100) // 10
    num_3 = (i - num_1*100 - num_2*10) % 100
    if str(num_1) not in s :
        continue
    if str(num_2) not in s :
        continue
    if str(num_3) not in s :
        continue
    for j in range(n-2) :
        if int(s[j]) == num_1 :
            for k in range(j+1,n-1) :
                if int(s[k]) == num_2 :
                    for l in range(k+1,n) :
                        if int(s[l]) == num_3 :
                            c += 1
                            break
                    break
            break

print(c)
### BUGGY CODE ###

# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [[[] for __ in range(3)] for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        u, v = u - 1, v - 1
        node_next[u][0].append((v, 1))
        node_next[u][1].append((v, 2))
        node_next[u][2].append((v, 0))

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    seen = [[0] * 3 for _ in range(n)]

    que = deque()
    que.append((s, 0, 0))

    while que:
        cnode, cstate, dist = que.popleft()
        seen[cnode][cstate] = 1
        dist += 1
        for nnode, nstate in node_next[cnode][cstate]:
            if nnode == t and nstate == 0:
                print(dist // 3)
                sys.exit()
            else:
                if seen[nnode][nstate] == 0:
                    que.append((nnode, nstate, dist))

    print(-1)


if __name__ == '__main__':
    main()


### FIXED CODE ###

# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = list(map(int, ipti().split()))
    node_next = [set() for _ in range(n)]

    for i in range(m):
        u, v = list(map(int, ipti().split()))
        node_next[u - 1].add(v - 1)

    s, t = list(map(int, ipti().split()))
    s, t = s - 1, t - 1

    dist = [[INF] * n for _ in range(3)]
    dist[0][s] = 0

    que = deque()
    que.append((0, s))

    while que:
        cstate, cnode = que.popleft()
        d = dist[cstate][cnode]
        nstate = (cstate + 1) % 3
        for nnode in node_next[cnode]:
            if dist[nstate][nnode] == INF:
                dist[nstate][nnode] = d + 1
                que.append((nstate, nnode))

    if dist[0][t] != INF:
        print(dist[0][t] // 3)
    else:
        print(-1)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 7)

s = input().strip()
dp = {i:dict() for i in range(len(s))}
def f(i, g):
    if i == len(s):
        return 0
    if g in dp[i]:
        return dp[i][g]
    p = i - g
    ans = (-1 if s[i] == 'p' else 0) + f(i + 1, g + 1)
    if p + 1 <= g:
        ans = max(ans, (1 if s[i] == 'g' else 0) + f(i + 1, g))
    dp[i][g] = ans
    return ans


print(f(0, 0))

### FIXED CODE ###

S = input()
N = len(S)
print(N // 2 - sum([int(s == 'p') for s in S]))
### BUGGY CODE ###


# -*- coding: utf-8 -*-

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

inds=sorted(range(len(As)), key=lambda k: As[k])[::-1]
#ds=[[0 for j in range(N+1-i)] for i in range(N+1)]

ds=[0]
for xy in range(1,N+1):
    nds=[0]*(xy+1)
    nds[-1]=ds[-1]+As[inds[xy-1]]*(inds[xy-1]-(xy-1))
    nds[0]=ds[0]+As[inds[xy-1]]*(N-xy-inds[xy-1])

    for x in range(1,xy):
        y=xy-x
        c1=ds[x-1]+As[inds[xy-1]]*abs(inds[xy-1]-(x-1))
        c2=ds[x]+As[inds[xy-1]]*abs(N-y-inds[xy-1])
        nds[x]=max(c1,c2)
    ds=nds.copy()
    #print(ds)


print(max(ds))
#print(ds)
#print(inds)




### FIXED CODE ###


# -*- coding: utf-8 -*-

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

inds=sorted(range(len(As)), key=lambda k: As[k])[::-1]
#ds=[[0 for j in range(N+1-i)] for i in range(N+1)]

ds=[0]
for xy in range(1,N+1):
    i=inds[xy-1]
    a=As[i]
    nds=[0]*(xy+1)
    nds[-1]=ds[-1]+a*(i-(xy-1))
    nds[0]=ds[0]+a*(N-xy-i)

    for x in range(1,xy):
        y=xy-x
        c1=ds[x-1]+a*abs(i-(x-1))
        c2=ds[x]+a*abs(N-y-i)
        nds[x]=max(c1,c2)
    ds=nds.copy()
    #print(ds)


print(max(ds))
#print(ds)
#print(inds)
### BUGGY CODE ###

n,k=map(int,input().split())
ans=0
a=[]
if k==1:
  print(0)
elif abs(n-k) > n:
  print(n)
else:
  while len(a)==len(set(a)):
    n=abs(n-k)
    a.append(n)
  print(min(a))

### FIXED CODE ###

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

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