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

from itertools import permutations

def hoge():
  N, M, R = map(int, input().split())
  r = map(int, input().split())
  r = [x - 1 for x in r]
  inf = 1e18
  d = [[inf] * N for i in range(N)]

  for i in range(N):
      d[i][i] = 0

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


  for k in range(N):
      for i in range(N):
          for j in range(N):
              t = d[i][k] + d[k][j]
              d[i][j] = min(d[i][j], t)


  ans = inf
  for i in permutations(r):
      tmphoge = len(i) - 1
      temp = 0
      for j in range(tmphoge):
          temp += d[i[j]][i[j+1]]
          if temp > ans:
              break
      ans = min(ans, temp)

  print(ans)
hoge()


### FIXED CODE ###

from itertools import permutations

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

def main():
    n, m, r = input_list()
    rlist = input_list()
    rlist = [r-1 for r in rlist]
    d = [[float('inf')]*n for i in range(n)]
    for i in range(n):
        d[i][i] = 0
    
    for i in range(m):
        a, b, c = input_list()
        d[a-1][b-1] = c
        d[b-1][a-1] = c

    for k in range(n):
        for i in range(n):
            for j in range(n):
                distance = d[i][k]+d[k][j]
                if d[i][j] > distance:
                    d[i][j] = distance

    ans = float('inf')
    for p in permutations(rlist):
        distance = 0
        for i in range(r-1):
            distance += d[p[i]][p[i+1]]

        if ans > distance:
            ans = distance

    print(ans)

main()
### BUGGY CODE ###

import heapq
X, Y, A, B, C = map(int, input().split())

p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort()

p = p[:X]
q = q[:Y]
heapq.heapify(p)
heapq.heapify(q)

jdgp = True
jdgq = True

while r and (jdgp or jdgq):
    if jdgp:hp = heapq.heappop(p)
    if jdgq:hq = heapq.heappop(q)
    rr = r.pop()
    if hp > hq:
        if jdgp:heapq.heappush(p, hp)
        if rr > hq:
            heapq.heappush(q, rr)
            hq = rr
        else:
            if jdgq:heapq.heappush(q, hq)
            r.append(rr)
            jdgq = False
    else:
        if jdgq:heapq.heappush(q, hq)
        if rr > hp:
            heapq.heappush(p, rr)
            hp = rr
        else:
            if jdgp:heapq.heappush(p, hp)
            r.append(rr)
            jdgp = False

print(sum(p) + sum(q))

### FIXED CODE ###

import heapq
X, Y, A, B, C = map(int, input().split())

p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort()

p = p[:X]
q = q[:Y]
heapq.heapify(p)
heapq.heapify(q)

jdg = True

while r and jdg:
    hp = heapq.heappop(p)
    hq = heapq.heappop(q)
    rr = r.pop()
    if hp > hq:
        heapq.heappush(p, hp)
        if rr > hq:
            heapq.heappush(q, rr)
        else:
            heapq.heappush(q, hq)
            r.append(rr)
            jdg = False            
    else:
        heapq.heappush(q, hq)
        if rr > hp:
            heapq.heappush(p, rr)
        else:
            heapq.heappush(p, hp)
            r.append(rr)
            jdg = False
print(sum(p) + sum(q))
### BUGGY CODE ###

############
#
# N, H
# i : Ai, Bi
#
# currentH <= max(Bi) AND max(Ai, Bi) = Bi then throw i
# currentH <= max(Ai) AND max(Ai, Bi) = Ai then Slash i
# currentH >  max(Bi) AND max(Ai, Bi) = Bi then Slash i
# currentH >  max(Ai) AND max(Ai, Bi) = Ai then Slash i
# 
# 1. select max(Ai, Bi) to Dmg
# 2. currentH = currentH - Dmg
# 2. if Dmg is belong to Bi, then discard Bi
# 3. if currentH > 0, then loop back to 1.

[N, H] = map(int, raw_input().split())

#print N, H

arrayA = []
arrayB = []

for i in range(N):
    [Ai, Bi] = map(int, raw_input().split())
    arrayA.append(Ai)
    arrayB.append(Bi)

arrayA.sort()
arrayB.sort()
arrayA.reverse()
arrayB.reverse()

count = 0

while H > 0:
    maxAi = arrayA[0]
    if len(arrayB) > 0:
        maxBi = arrayB[0]
    else:
        maxBi = 0
        
    if maxBi > maxAi:
        maxDmg = arrayB.pop(0)
        H = H -maxDmg
        count = count +1
    else:
        maxDmg = maxAi
        count = count + (H / maxDmg)
        H = H % maxDmg

#    print H, maxDmg, count

print count


### FIXED CODE ###

############
#
# N, H
# i : Ai, Bi
#
# currentH <= max(Bi) AND max(Ai, Bi) = Bi then throw i
# currentH <= max(Ai) AND max(Ai, Bi) = Ai then Slash i
# currentH >  max(Bi) AND max(Ai, Bi) = Bi then Slash i
# currentH >  max(Ai) AND max(Ai, Bi) = Ai then Slash i
# 
# 1. select max(Ai, Bi) to Dmg
# 2. currentH = currentH - Dmg
# 2. if Dmg is belong to Bi, then discard Bi
# 3. if currentH > 0, then loop back to 1.

[N, H] = map(int, raw_input().split())

#print N, H

arrayA = []
arrayB = []

for i in range(N):
    [Ai, Bi] = map(int, raw_input().split())
    arrayA.append(Ai)
    arrayB.append(Bi)

arrayA.sort()
arrayB.sort()
arrayA.reverse()
arrayB.reverse()

count = 0

while H > 0:
    maxAi = arrayA[0]
    if len(arrayB) > 0:
        maxBi = arrayB[0]
    else:
        maxBi = 0
        
    if maxBi > maxAi:
        maxDmg = arrayB.pop(0)
        H = H -maxDmg
        count = count +1
    else:
        maxDmg = maxAi
        if maxDmg < H:
            count = count + (H / maxDmg)
            H = H % maxDmg
        else:
            count = count +1
            H = H -maxDmg

#    print H, maxDmg, count

print count
### BUGGY CODE ###

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

if K==1:
  print(0)
  exit()
  
s = N
for i in range(10**18):
  s = min(s,abs(N-K))
  N -= K
print(s)

### FIXED CODE ###

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

print(min(a,K-a))
### BUGGY CODE ###

# 初期入力
import numpy as np
import sys
input = sys.stdin.readline
N = int(input())
V = np.array(list(map(int, input().split())))
C = np.array(list(map(int, input().split())))

#どの宝石を選ぶか全探索
v_c_sum=[]
from itertools import combinations
for i in range(N+1):
    for t in combinations(range(N), i):
        v =np.sum(V[list(t)])
        c =np.sum(C[list(t)])
        v_c_sum.append(v-c)
print(max(v_c_sum))

### FIXED CODE ###

# 初期入力
import numpy as np
import sys
input = sys.stdin.readline
N = int(input())
V = np.array(list(map(int, input().split())))
C = np.array(list(map(int, input().split())))

#どの宝石を選ぶか全探索
v_c_sum=0
for i in range(N):
    if 0 < V[i] -C[i]:
        v_c_sum +=V[i] - C[i]
print(v_c_sum)
### BUGGY CODE ###

N = int(input())
people = input()

counts = []
for n in range(N):
    count = 0
    for i in range(N):
        if i == n:
            continue
        if n<i and people[i]=='E':
            count +=1
        if n>i and people[i]=='W':
            count +=1
    counts.append(count)

print(min(counts))

### FIXED CODE ###

N = int(input())
people = input()

count = 0
for i in range(1, N):
    if people[i] == 'E':
        count += 1

counts = [count]
for i in range(1, N):
    if people[i - 1] == 'W':
        count += 1
    if people[i]  == 'E':
        count -= 1
    counts.append(count)

print(min(counts))
### BUGGY CODE ###

N = int(input())
A_list = list(map(int, input().split()))
bite = [0 for _ in range(60)]
for A in A_list:
    for i in range(60):
        if A & 1 << i:
            bite[i] += 1

ans = 0
on_right = N - 1
for num in range(len(A_list)):
    A = A_list[num]
    for i in range(60):
        if A & 1 << i:
            bite[i] -= 1
            ans += (on_right - bite[i]) * 1 << i
        else:
            ans += bite[i] * 1 << i
    on_right -= 1

ans %= (10 ** 9 + 7)
print(ans)

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
one = [0 for _ in range(60)]

for a in A:
    for i in range(60):
        if a & 1 << i:
            one[i] += 1
ans = 0
for a in A:
    n -= 1
    if n == 0:
        break
    for i in range(60):
        if a & 1 << i:
            one[i] -= 1

    for i in range(60):
        if a & 1 << i:
            ans += (1 << i) * (n - one[i])
        else:
            ans += (1 << i) * one[i]

print(ans % 1000000007)
### BUGGY CODE ###

inf=10**9
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
dp=[[inf]*(2**n) for _ in range(m+1)]
def two(i):
	return "0"*(n-len(bin(i)[2:]))+bin(i)[2:]
for i in range(m+1):
	dp[i][0]=0
for i in range(m):
	a,b=map(int,input().split())
	r=0
	c=list(map(int,input().split()))
	for p in c:
		r+=2**(p-1)
	for j in range(2**n):
		x=j|r
		dp[i+1][x]=min(dp[i][j]+a,dp[i][x],dp[i+1][x])
	for j in range(2**n):
		dp[i+1][j]=min(dp[i+1][j],dp[i][j])
print(dp[-1][-1]) if dp[-1][-1]!=inf else print(-1)

### FIXED CODE ###

inf=10**9
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
dp=[[inf]*(2**n) for _ in range(m+1)]
def two(i):
	return "0"*(n-len(bin(i)[2:]))+bin(i)[2:]
for i in range(m+1):
	dp[i][0]=0
for i in range(m):
	a,b=map(int,input().split())
	r=0
	c=list(map(int,input().split()))
	for p in c:
		r+=2**(p-1)
	for j in range(2**n):
		x=j|r
		dp[i+1][x]=min(dp[i][j]+a,dp[i][x],dp[i+1][x])
	for j in range(2**n):
		dp[i+1][j]=min(dp[i+1][j],dp[i][j])
print(dp[-1][-1]) if dp[-1][-1]!=inf else print(-1)
### BUGGY CODE ###

N = int(input())
# = map(int, input().split())
P = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]

count = 0

for i in range(N):
    if P[i] == min(P[:i+1]):
        count += 1

print(count)


### FIXED CODE ###

N = int(input())
# = map(int, input().split())
P = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]

count = 0
minimum = 300000

for i in range(N):
    if P[i] == 1:
        count += 1
        break
    if P[i] < minimum:
        count += 1
        minimum = P[i]

print(count)
### BUGGY CODE ###

from collections import deque
inf = 10 ** 10

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

graph = [[] for i in range(N)]
dis = [[inf]*N for i in range(N)]

for i in range(M):
    L,R,D = map(int,input().split())
    L -= 1
    R -= 1
    graph[L].append(R)
    graph[R].append(L)
    dis[L][R] = D
    dis[R][L] = (-1)*D

data = [inf]*N
det = 0

for i in range(N):
    if data[i] != inf:
        continue
    data[i] = 0
    stack = deque()
    stack.append(i)
    while stack:
        x = stack.popleft()
        for y in graph[x]:
            if data[y] == inf:
                data[y] = data[x] + dis[x][y]
                stack.append(y)
            else:
                if data[y] - data[x] != dis[x][y]:
                    det = -1
                    break
    if det == -1:
        break

if det == 0:
    print("Yes")
else:
    print("No")



### FIXED CODE ###

from collections import deque
import sys
input = sys.stdin.readline

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

graph = [[] for i in range(N)]

for i in range(M):
    L,R,D = map(int,input().split())
    L -= 1
    R -= 1
    graph[L].append([R,D])
    graph[R].append([L,-D])
    
data = [None]*N

for i in range(N):
    if data[i] != None:
        continue
    data[i] = 0
    stack = deque()
    stack.append(i)
    while stack:
        x = stack.popleft()
        for y in graph[x]:
            if data[y[0]] == None:
                data[y[0]] = data[x] + y[1]
                stack.append(y[0])
            else:
                if data[y[0]] - data[x] != y[1]:
                    print("No")
                    sys.exit()
    
print("Yes")
### BUGGY CODE ###

n = int(input())
s = [input() for i in range(n)]
s.sort()
l = sorted(set(s))
ans = 0
ans_l = []

for i in l:
    if s.count(i) > ans:
      ans_l.clear()
      ans_l.append(i)
      ans = s.count(i)
    elif s.count(i) == ans:
      ans_l.append(i)
    s.remove(i)
      

print("\n".join(ans_l))

### FIXED CODE ###

from collections import Counter

n = int(input())
S = [input() for i in range(n)]

s = Counter(S)
max_cnt = max(s.values())
names = [name for name, cnt in s.items() if cnt == max_cnt]
names.sort()

print("\n".join(names))
### BUGGY CODE ###

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    sums = []
    amount = 1
    for i in range(0, k):
        amount = amount * a[i]
    sums.append(amount)

    for i in range(1, n-k+1):
        sums.append(sums[i-1]/a[i-1]*a[i+k-1])
        if sums[i-1] < sums[i]:
            print("Yes")
        else:
            print("No")


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    for i in range(0, n-k):
        if a[i] < a[k+i]:
            print("Yes")
        else:
            print("No")


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

n, w = map(int, input().split())
things = []
things.append([0,0])
things += [list(map(int, input().split())) for _ in range(n)]

dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)]

def do_dp():
  for i,thing in enumerate(things):
    dp_1 = dp[i-1]
    for j in range(1, w + 1):      
      dp[i][j] = dp_1[j-thing[0]] + thing[1]  if (j - thing[0]) >= 0 else dp_1[j]
      dp[i][j] = max(dp[i][j], dp_1[j])

do_dp()
print(dp[n][w])

### FIXED CODE ###

n, w = map(int, input().split())
things = []
things.append([0,0])
things += [list(map(int, input().split())) for _ in range(n)]

dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)]

def do_dp():
  for i,thing in enumerate(things):
    dp_1 = dp[i-1]
    for j in range(1, w + 1):      
      dp[i][j] = dp_1[j-thing[0]] + thing[1]  if (j - thing[0]) >= 0 else dp_1[j]
      dp[i][j] = max(dp[i][j], dp_1[j])

do_dp()
print(dp[n][w])
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n - 1, -1, -1):
    count = 0
    for j in b:
        if j % (i + 1) == 0:
            count += 1
    if count % 2 != a[i]:
        b.append(i + 1)
print(len(b))
print(" ".join(list(map(str, b))))

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = set()
for i in range(n - 1, -1, -1):
    count = 0
    c = i + 1
    while c < 200000:
        if c in b:
            count += 1
        c += (i + 1)
    if count % 2 != a[i]:
        b.add(i + 1)
print(len(b))
print(" ".join(list(map(str, b))))
### BUGGY CODE ###

# ABC C - Go to school

N = int(input())
A = list(map(int, input().split()))
list = []

for i in range(1,N+1):
    print(A.index(i)+1,end= ' ')

print()


### FIXED CODE ###

# ABC C - Go to school


N = int(input())
A = list(map(int, input().split()))
Q = []

for i in range(N):
    Q.append(1)

for i in range(N):
    Q[A[i]-1] = i + 1

for i in range(N):
    print(Q[i], end = ' ')

print()
### BUGGY CODE ###

from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
g=a[0]
for i in range(1,n):
  g=g*a[i]/gcd(g,a[i])
suml=0
for i in range(n):
  suml+=g/a[i]
  suml%=10**9+7
print(int(suml))

### FIXED CODE ###

import fractions
n=int(input())
a=[int(i) for i in input().split()]
g=1
dx=fractions.gcd
for i in a:
  g*=i//dx(g,i)
suml=0
mod=10**9+7
for i in a:
  suml+=g*pow(i,mod-2,mod)
  suml%=mod
print(suml)
### BUGGY CODE ###

from itertools import *
def p(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
ans=10
a=p(int(input()))
a+=[1,1]   
c=1
for i in a:
    c*=i
for i in range(1,len(a)//2+1):
    for j in combinations(a,i):
        b=1
        for k in j:
            b*=k
        ans=min(ans,max(len(str(b)),len(str(c//b))))
print(ans)

### FIXED CODE ###

n=int(input())
ans=10
for i in range(1,int(n**.5)+1):
    if n%i==0:
        ans=min(max(len(str(i)),len(str(n//i))),ans)
print(ans)
### BUGGY CODE ###

N = int(input())

s = set()

for _ in range(N):

    A = int(input())

    if A in s:
        s = s - {A}
    else:
        s.add(A)

print(len(s))


### FIXED CODE ###

N = int(input())
d = {}
a = []
for _ in range(N):
    A = int(input())
    a.append(A)
    if not(A in d):
        d[A] = 1
        continue
    elif d[A]==1:
        d[A] = 0
    else:
        d[A] = 1

ans = 0
for key in d:
    if d[key]==1:
        ans = ans + 1
print(ans)
### BUGGY CODE ###

N = int(input())
D = [int(i) for i in input().split()]

cnt = 0
for i in range(max(D)+1):
  ARC = 0
  ABC = 0
  for d in D:
    if i <= d:
      ARC +=1
    else:
      ABC +=1
  if ARC == ABC:
    cnt +=1
print(cnt)

### FIXED CODE ###

N = int(input())
D = [int(i) for i in input().split()]

D_unique = list(set(D))
D.sort()

middle_idx = int(len(D)/2)
cnt = D[middle_idx]-D[middle_idx-1]
    
print(cnt)
### BUGGY CODE ###

s=list(map(int,list(input())))
n=len(s)
m=[[0]*(n+1) for _ in range(n+1)]
ans=0
for i in range(1,n+1):
    for j in range(i,n+1):
        m[i][j]=(m[i][j-1]*10+s[j-1])%2019
        if m[i][j]==0:
            ans+=1
print(ans)

### FIXED CODE ###

s=list(map(int,list(input())))[::-1]
n=len(s)
chk=[0]*2019
chk[0]=1
t,m,ans=1,0,0
for i in s:
    m=(m+t*i)%2019
    ans+=chk[m]
    chk[m]+=1
    t=t*10%2019
print(ans)
### BUGGY CODE ###

import numpy as np
def so(n):
    for i in range(2, n + 1):
        if i * i > n:
            break
        if n % i == 0:
            return False
    return n != 1
A, B = map(int, input().split())
M = np.min([A, B])
n = 0
for i in range(1, M + 1):
    if so(i):
        if (A % i == 0) and (B % i == 0):
            n = n + 1
print(n+1)

### FIXED CODE ###

import numpy as np
def p(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
A, B = map(int, input().split())
M = np.min([A, B])
a = p(A)
b = p(B)
t = set(a) & set(b)
print(len(t) + 1)
### BUGGY CODE ###

def unification(n,s,count):
    if n < 1:
        return count

    for i in range(n-1):
        if s[i] !=  s[i+1]:
            s.pop(i)
            s.pop(i)
            return unification(n-2,s,count+2)

    return count

if __name__ == '__main__':
    s = raw_input()
    n = len(s)
    _s = [i for i in s]

    print unification(n,_s,0)

### FIXED CODE ###

def unification(n,s):
    rc = 0
    bc = 0

    for c in s:
        if c == '0':
            rc += 1
        elif c == '1':
            bc += 1
        else:
            break

    return min(rc,bc) * 2

if __name__ == '__main__':
    s = raw_input()
    n = len(s)
    _s = [i for i in s]

    print unification(n,_s)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N = int(input())
S = [input()[:-1] for _ in [0]*N]

cnt = 0
for i, s in enumerate(S):
    for j, t in enumerate(S[i+1:]):
        if sorted(s) == sorted(t):
            cnt += 1
print(cnt)

### FIXED CODE ###

N, *S = open(0).read().split()
S = [str(sorted(s)) for s in S]
D = {}
ans = 0
for s in S:
    if s in D:
        ans += D[s]
        D[s] += 1
    else:
        D[s] = 1
print(ans)
### BUGGY CODE ###

N = int(input())
L = list(map(int,input().split()))
cand = []
bit =[[0 for i in range(20)] for _ in range(N)]
for j in range(len(L)):
    for i in range(20):
        bit[j][i] = (L[j] >> i) & 1
for j in range(1,len(L)):
    for i in range(20):
        bit[j][i] += bit[j-1][i]
bit = [[0 for i in range(20)]] + bit

#bisect( [x,y) )

def check(start,x):
    for i in range(20):
        if bit[x][i] - bit[start][i] >= 2:
            return False
    return True


def bisect(start,x,y):
    if y - x == 1: return x
    mid = (x + y) // 2
    if check(start,mid) == False:
        return bisect(start,x,mid)
    else:
        return bisect(start,mid,y)

cnt = 0
for n in range(N):
    cnt += bisect(n, n, N+1) - n
 #   print(bisect(n,n,N+1),n)
print(cnt)



### FIXED CODE ###

N = int(input())
L = list(map(int,input().split()))
cand = []
bit =[[0 for i in range(20)] for _ in range(N)]
for j in range(len(L)):
    for i in range(20):
        bit[j][i] = (L[j] >> i) & 1
for j in range(1,len(L)):
    for i in range(20):
        bit[j][i] += bit[j-1][i]
bit = [[0 for i in range(20)]] + bit

#bisect( [x,y) )

def check(start,x):
    for i in range(20):
        if bit[x][i] - bit[start][i] >= 2:
            return False
    return True


def bisect(start,x,y):
    if y - x == 1: return x
    mid = (x + y) // 2
    if check(start,mid) == False:
        return bisect(start,x,mid)
    else:
        return bisect(start,mid,y)

cnt = 0
for n in range(N):
    cnt += bisect(n, n, N+1) - n
 #   print(bisect(n,n,N+1),n)
print(cnt)
### BUGGY CODE ###

#D
s_list=list(input())
q=int(input())
r=0
for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            s_list.insert(0,ope[2])
        else:
            s_list.append(ope[2])
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))

### FIXED CODE ###

#D
s_list=list(input())
q=int(input())
r=0
sl_list=[]
for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            sl_list.append(ope[2])
        else:
            s_list.append(ope[2])
sl_list.reverse()
s_list=sl_list + s_list
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))
### BUGGY CODE ###

import sys
input = sys.stdin.readline
def main():
    n, q = map(int, input().split())
    c = list(map(int,input().split()))
    for _ in range(q):
        l, r = map(int, input().split())
        print(len(set(c[l-1:r])))
main()

### FIXED CODE ###

import numpy as np

def solve(inp):
    def bit_add(arr, n, i, x):
        while i <= n:
            arr[i] += x
            i += i & -i

    def bit_sum(arr, i):
        result = 0
        while i > 0:
            result += arr[i]
            i ^= i & -i
        return result

    N, Q = inp[:2]
    C = inp[2: 2+N]
    L, R = inp[2+N::2] - 1, inp[3+N::2] - 1

    just_left = {}
    segments = []
    for i in range(N):
        c = C[i]
        if c in just_left:
            segments.append((i, 0, just_left[c], 0))
        just_left[c] = i

    for i in range(Q):
        segments.append((R[i], 1, L[i], i))

    segments.sort()
    bit = np.zeros(N + 2, dtype=np.int64)
    ans = np.zeros(Q, dtype=np.int64)

    for r, tp, l, i in segments:
        if tp == 0:
            bit_add(bit, N + 2, l + 2, 1)
        else:
            base = r - l + 1
            ans[i] = base - (bit_sum(bit, r + 2) - bit_sum(bit, l + 1))

    return ans

def main():
    inp = np.fromstring(open(0).read(), dtype=np.int64, sep=' ')
    ans = solve(inp)
    print("\n".join(map(str,ans)))
    

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', '(i8[:],)')(solve)
    cc.compile()

if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        exit(0)
    from my_module import solve
    main()
### BUGGY CODE ###

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

min_n = n
n = abs(n - k)
#print(n, k)
if n >= min_n:
    print(min_n)

else:
    cou = 0
    while(min_n >= n):

        cou += 1
        min_n = n
        n = abs(n - k)
        #print(n)
        

    print(min_n)


### FIXED CODE ###

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

mo = n%k

mok = abs(mo - k)

if mo <= mok:
    print(mo)
else:
    print(mok)
### BUGGY CODE ###

import numpy as np

n = list(map(int, input().split()))
d = np.array(list(map(int, input().split())))
a = len(str(n[0]))
x = np.zeros(a + 1)

for i in range(a + 1):
    x[i] = (n[0] % np.power(10, a - i + 1)) // np.power(10, a - i)

i = 0
while i != a:
    i += 1
    while np.any(d == x[i]):
        x[i] += 1
        x[i + (i != a):] = 0
        if x[i] == 10:
            x[i - 1] += 1
            x[i:] = 0
            i -= 1
        
ans = 0
for i in range(a + 1):
    ans += x[i] * np.power(10, a - i)

print(int(ans))

### FIXED CODE ###

import numpy as np

n = list(map(int, input().split()))
d = np.array(list(map(int, input().split())))
a = len(str(n[0]))
x = np.zeros(a + 1)

for i in range(a + 1):
    x[i] = (n[0] % np.power(10, a - i + 1)) // np.power(10, a - i)

i = 0
while i != a:
    i += 1
    while np.any(d == x[i]):
        x[i] += 1
        if i != a:
            x[i + 1:] = 0
        if x[i] == 10:
            x[i - 1] += 1
            x[i:] = 0
            i -= 1

ans = 0
for i in range(a + 1):
    ans += x[i] * np.power(10, a - i)

print(int(ans))