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

import sys
input=sys.stdin.readline
N,C=map(int,input().split())
d=[list(map(int,input().split())) for i in range(C)]
c=[list(map(int,input().split())) for i in range(N)]
ans=10**18
mod0=[]
mod1=[]
mod2=[]
for i in range(N):
  for j in range(N):
    k=(i+j+2)%3
    if k==0:
      mod0.append((i,j))
    elif k==1:
      mod1.append((i,j))
    else:
      mod2.append((i,j))
cnt0=[0]*(C+1)
cnt1=[0]*(C+1)
cnt2=[0]*(C+1)
for c0 in range(1,C+1):
  for x,y in mod0:
    cnt0[c0]+=d[c[x][y]-1][c0-1]
for c1 in range(1,C+1):
  for x,y in mod1:
    cnt1[c1]+=d[c[x][y]-1][c1-1]
for c2 in range(1,C+1):
  for x,y in mod2:
    cnt2[c2]+=d[c[x][y]-1][c2-1]
ans=10**18
for c0 in range(1,C+1):
  for c1 in range(1,C+1):
    for c2 in range(1,C+1):
      if c1==c2 or c2==c0 or c0==c1:
        continue
      cnt=cnt0[c0]+cnt1[c1]+cnt2[c2]
      ans=min(ans,cnt)
print(ans)

### FIXED CODE ###

import sys
input=sys.stdin.readline
N,C=map(int,input().split())
d=[list(map(int,input().split())) for i in range(C)]
c=[list(map(int,input().split())) for i in range(N)]
ans=10**18
mod0=[]
mod1=[]
mod2=[]
for i in range(N):
  for j in range(N):
    k=(i+j+2)%3
    if k==0:
      mod0.append((i,j))
    elif k==1:
      mod1.append((i,j))
    else:
      mod2.append((i,j))
cnt0=[0]*(C+1)
cnt1=[0]*(C+1)
cnt2=[0]*(C+1)
for c0 in range(1,C+1):
  for x,y in mod0:
    cnt0[c0]+=d[c[x][y]-1][c0-1]
for c1 in range(1,C+1):
  for x,y in mod1:
    cnt1[c1]+=d[c[x][y]-1][c1-1]
for c2 in range(1,C+1):
  for x,y in mod2:
    cnt2[c2]+=d[c[x][y]-1][c2-1]
ans=10**18
for c0 in range(1,C+1):
  for c1 in range(1,C+1):
    for c2 in range(1,C+1):
      if c1==c2 or c2==c0 or c0==c1:
        continue
      cnt=cnt0[c0]+cnt1[c1]+cnt2[c2]
      ans=min(ans,cnt)
print(ans)
### BUGGY CODE ###

def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = inp()
m = int(n)

ans = m

while(len(n) != 1):
    n = str(ans)
    m = ans
    ans = 0
    for i in range(len(n)):
        ans += (m % 10)
        m //= 10

if ans == 9 or ans == 0:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = inp()
m = int(n)

ans = 0

for i in range(len(n)):
    ans += int(n[i])

ans %= 9

if ans == 0:
    print("Yes")
else:
    print("No")
### BUGGY CODE ###

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

dp = [[] for _ in range(k)]

days = [i for i in range(n) if s[i] == 'o']
dp[0] = [[day] for day in days]


for day in days:
  for i in range(k-1):
    for j in range(len(dp[i])):
      if dp[i][j][-1] +c < day:
        dp[i+1].append(dp[i][j]+[day])

ans = set(dp[k-1][0])
for lst in dp[k-1]:
  ans = ans.intersection(set(lst))
ans = list(ans)
ans.sort()
for a in ans:
  print(a+1)
  

### FIXED CODE ###

n,k,c= map(int,input().split())
s = input()
i = 0

def func(s,k,c):
  i = 0
  res = []
  while 1:
    if s[i] == 'o':
      res.append(i)
      if len(res)== k:
        break
      i += c
    i+= 1
  return res
ans1 = func(s,k,c)
ans2 = func(s[::-1],k,c)
ans2 = [n-a-1 for a in ans2]
ans2 = ans2[::-1]

for i in range(k):
  if ans1[i]==ans2[i]:
    print(ans1[i]+1)
### BUGGY CODE ###

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

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

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


ans = 0

aa = 0

for i in range(n+1):
    cnt = 0
    x = k
    x -= aa
    if i != n:
        aa += a[i]
    
    if x<0:
        break
    
    while x>0 and cnt<m:
        x -= b[cnt]
        cnt += 1
        
    if x<0:
        cnt -= 1
        
    ans = max(ans,cnt+i)
    
print(ans)

### FIXED CODE ###

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

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

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

x = [0]

y = [0]

for i in range(n):
    x.append(x[i]+a[i])

for i in range(m):
    y.append(y[i]+b[i])

ans = 0

cnt = m

for i in range(n+1):
    
    if k-x[i]<0:
        break
    
    while y[cnt]>k-x[i]:
        cnt -= 1
        
    ans = max(ans,cnt+i)
    
print(ans)
### BUGGY CODE ###

from math import gcd
n = int(input())
a = list(map(int, input().split()))
max_a = max(a)
cnt_a = [0] * (max_a + 1)
for ai in a:
  cnt_a[ai] += 1
ans = 'pairwise'
for i in range(2, max_a + 1):
  cnt = 0
  for j in range(i, max_a + 1, i):
    cnt += cnt_a[j]
  if cnt > 1:
    ans = 'setwise'
g = 0
for ai in a:
  g = gcd(g, ai)

if ans == 'pairwise':
  print(ans, 'coprime')
elif ans == 'setwise' and g == 1:
  print(ans, 'coprime')
else:
  print('not coprime')

### FIXED CODE ###

from math import gcd
n = int(input())
a = list(map(int, input().split()))
max_a = max(a)
cnt_a = [0] * (max_a + 1)
for ai in a:
  cnt_a[ai] += 1
ans = 'pairwise'
for i in range(2, max_a + 1):
  cnt = 0
  for j in range(i, max_a + 1, i):
    cnt += cnt_a[j]
  if cnt > 1:
    ans = 'setwise'
g = 0
for ai in a:
  g = gcd(g, ai)

if ans == 'pairwise':
  print(ans, 'coprime')
elif ans == 'setwise' and g == 1:
  print(ans, 'coprime')
else:
  print('not coprime')
### BUGGY CODE ###

def main():
  n=int(input())
  l=sorted(list(map(int,input().split())))
  ans=0
  for a in range(n-2):
    for b in range(a+1,n-1):
      p,q=l[a]+l[b],l[b+1:]
      ans+=sum(x<p for x in q)
  print(ans)
if __name__=='__main__':
  main()

### FIXED CODE ###

def main():
  import bisect
  n=int(input())
  l=sorted(list(map(int,input().split())))
  ans=0
  for a in range(n-2):
    for b in range(a+1,n-1):
      ans+=bisect.bisect_left(l,l[a]+l[b])-b-1
  print(ans)
if __name__=='__main__':
  main()
### BUGGY CODE ###

from itertools import permutations
import heapq

def dijkstra(n, tc_edges, s):
    
    min_dist = [float('inf')] * n
    min_dist[s] = 0
    next_v = []
    heapq.heappush(next_v, (0, s))
    path = [-1] * n
    
    while True:
        if len(next_v) == 0: break
        d, v = heapq.heappop(next_v)
        
        for u, d in tc_edges[v]:
            if min_dist[u] > min_dist[v] + d:
                min_dist[u] = min_dist[v] + d
                heapq.heappush(next_v, (min_dist[u], u))
                path[u] = v
                
    return min_dist, path
    
n, m, r = map(int, input().split())
r = list(map(int, input().split()))
edge = [[] for _ in range(n)]

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

dist = [dijkstra(n, edge, i)[0] for i in range(n)]

ans = float('inf')
for pr in permutations(r):
    temp = 0
    for i in range(len(r)-1):
        temp += dist[pr[i]-1][pr[i+1]-1]
        if temp >= ans: break
    
    ans = min(ans, temp)
    
print(ans)

### FIXED CODE ###

import heapq
from itertools import permutations
def dijkstra(n, tc_edges, s):
    
    min_dist = [float('inf')] * n
    min_dist[s] = 0
    next_v = []
    heapq.heappush(next_v, (0, s))
    
    while True:
        if len(next_v) == 0: break
        d, v = heapq.heappop(next_v)
        
        for u, d in tc_edges[v]:
            if min_dist[u] > min_dist[v] + d:
                min_dist[u] = min_dist[v] + d
                heapq.heappush(next_v, (min_dist[u], u))
                
    return min_dist
    
n, m, r = map(int, input().split())
r = list(map(int, input().split()))
edge = [[] for _ in range(n)]

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

dist = [dijkstra(n, edge, i) for i in range(n)]

ans = float('inf')
for pr in permutations(r):
    temp = 0
    for i in range(len(r)-1):
        temp += dist[pr[i]-1][pr[i+1]-1]
        if temp >= ans: break
    
    ans = min(ans, temp)
    
print(ans)
### BUGGY CODE ###

a,b,c,d = map(int,input().split())
ans = 0
for i in range(a,b+1):
  if i % c != 0 and i % d != 0:
    ans += 1
print(ans)


### FIXED CODE ###

A,B,C,D = map(int,input().split())

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

def F(N,C,D):
  x = N
  x -= N//C
  x -= N//D
  lcm = C*D//gcd(C,D)
  x += N//lcm
  return x

answer = F(B,C,D) - F(A-1,C,D)
print(answer)
### BUGGY CODE ###

n=int(input())
list=[]
i=0
sum=0
for i in range(n):
    c=input()
    result=list.count(c)
    if result==0:
        list.append(c)
        sum=sum+1
print(sum)

### FIXED CODE ###

n=int(input())
list=[]
for i in range(n):
    list.append(input())
list=set(list)
print(len(list))
### BUGGY CODE ###

from collections import Counter

N = int(input())

S = [input() for _ in range(N)]

dic = Counter(S)

ans = [key for key, value in dic.items() if value == max(dic.values())]

for key in sorted(ans):
    print(key, end="")
    print("")


### FIXED CODE ###

from collections import Counter

N = int(input())
S = [input() for _ in range(N)]

dic = Counter(S)

tmp = max(dic.values())
ans = [key for key, value in dic.items() if value == tmp]

# もうちょいかっこいい書き方があるはず。リストを1行1str出力 and 最後に改行しないように
for key in sorted(ans):
    print(key, end="")
    print("")
### BUGGY CODE ###

import sys
readline = sys.stdin.readline

H, W = map(int, readline().split())
S = [readline() for i in range(H)]
P = [[0]*W for i in range(H)]
rW = range(W); rH = range(H)
for i in rH:
    p = -1
    Si = S[i]
    Pi = P[i]
    for j in rW:
        if Si[j] == '#':
            c = j - p - 1
            Pi[p+1:j] = (c for k in range(c))
            p = j
    c = W - p - 1
    Pi[p+1:W] = (c for k in range(c))
for j in rW:
    p = -1
    for i in rH:
        if S[i][j] == '#':
            c = i - p - 1
            for k in range(p+1, i):
                P[k][j] += c
            p = i
    c = H - p - 1
    for k in range(p+1, H):
        P[k][j] += c
ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            ans = max(ans, P[i][j])
sys.stdout.write("%d\n" % (ans - 1))

### FIXED CODE ###

H, W = map(int, input().split())
S = [input() for i in range(H)]
P = [[0]*W for i in range(H)]
Q = [[0]*W for i in range(H)]
for i in range(H):
    p = -1
    for j in range(W):
        if S[i][j] == '#':
            c = j - p - 1
            for k in range(p+1, j):
                P[i][k] = c
            p = j
    c = W - p - 1
    for k in range(p+1, W):
        P[i][k] = c
for j in range(W):
    p = -1
    for i in range(H):
        if S[i][j] == '#':
            c = i - p - 1
            for k in range(p+1, i):
                Q[k][j] = c
            p = i
    c = H - p - 1
    for k in range(p+1, H):
        Q[k][j] = c
ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            ans = max(ans, P[i][j] + Q[i][j])
print(ans - 1)
### BUGGY CODE ###

Q=int(input())
queries=[tuple(map(int,input().split())) for _ in range(Q)]

memo=[-1]*(10**5+1)
memo[1]=0
def is_prime1(n):
    if memo[n]!=-1:
        return(memo[n]==1)
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            memo[n]=0
            return False
    memo[n]=1
    return True

p=[True]*(10**5+1)
def is_prime2(n):
    return(p[n])

def sleve():
    p[0]=False
    p[1]=False
    primes=[]
    for i in range(2,10**5+1):
        if p[i]:
            primes.append(i)
            for j in range(2*i, 10**5+1, i):
                p[j]=False

sleve()
is_prime=is_prime2
for l,r in queries:
    ans=0
    for n in range(l,r+1):
        if n&1:
            if is_prime(n) and is_prime((n+1)//2):
                ans+=1
    print(ans)


### FIXED CODE ###

Q=int(input())
queries=[tuple(map(int,input().split())) for _ in range(Q)]

#エラトステネスの篩
p=[True]*(10**5+1)
p[0]=False
p[1]=False
for i in range(2,10**5+1):
    if p[i]:
        for j in range(2*i, 10**5+1, i):
            p[j]=False

#like2017[n]=n以下の「2017に似た数」の数
like2017=[0]*(10**5+1)
for i in range(1,10**5+1):
    like2017[i]=like2017[i-1]+(1 if i&1 and p[i] and p[(i+1)//2] else 0)

for l,r in queries:
    print(like2017[r]-like2017[l-1])
### BUGGY CODE ###

def nyu():
    num1,num2 = input().split()
    num1 = int(num1)
    num2 = int(num2)
    #   print("x = " ,x[i]  ,"y = ",y[i])
    return num1,num2

def otoshidama(n,y):
    n = n+1
    success_flg = 0
    ichiman = 0
    gosen = 0
    senen = 0
    y_tmp = y
    for i in reversed(range(n)):
#        print(i)
        ichiman  = 0
        gosen  = 0
        senen = 0
        y_tmp = y
        g_ymp = y_tmp 
        if y_tmp >= 10000*i: 
            y_tmp = y_tmp - 10000*i
            ichiman = i
            g_ymp = y_tmp
            if check(y_tmp,ichiman,gosen,senen) == True:
             return ichiman,gosen,senen
        for g in reversed(range(n)):
            gosen = 0
            senen = 0
            y_tmp = g_ymp
            if y_tmp >= 5000*g:
                y_tmp = y_tmp - 5000 * g
                gosen = g
                if check(y_tmp,ichiman,gosen,senen) == True:
                   return ichiman,gosen,senen
            senen = int(y_tmp / 1000)
            y_tmp = y_tmp % 1000
            if check(y_tmp,ichiman,gosen,senen) == True:
                return ichiman,gosen,senen
    return -1,-1,-1

def check(y_tmp,ichiman,gosen,senen):
    #print("y_tmp:",y_tmp,"ichiman:",ichiman,"gosen:",gosen,"senen:",senen,"goukei",ichiman + gosen + senen,n)
    if y_tmp == 0 and ichiman + gosen + senen  == n:
        return  True    
    return False


n,y =nyu()
num1,num2,num3 = otoshidama(n,y)
print(num1,num2,num3)



### FIXED CODE ###

def nyu():
    num1,num2 = input().split()
    num1 = int(num1)
    num2 = int(num2)
    #   print("x = " ,x[i]  ,"y = ",y[i])
    return num1,num2

def otoshidama(n,y):
    n = n+1
    ichiman = 0
    gosen = 0
    senen = 0
    y_tmp = y
    for i in reversed(range(n)):
#        print(i)
        ichiman  = 0
        gosen  = 0
        senen = 0
        y_tmp = y
        g_ymp = y_tmp 
        if y_tmp >= 10000*i: 
            y_tmp = y_tmp - 10000*i
            ichiman = i
            g_ymp = y_tmp
 #           if check(y_tmp,ichiman,gosen,senen) == True:
 #            return ichiman,gosen,senen
        for g in reversed(range(n-i)):
            gosen = 0
            senen = 0
            y_tmp = g_ymp
            if y_tmp >= 5000*g:
                y_tmp = y_tmp - 5000 * g
                gosen = g
#                if check(y_tmp,ichiman,gosen,senen) == True:
#                   return ichiman,gosen,senen
            senen = int(y_tmp / 1000)
            y_tmp = y_tmp % 1000
            if check(y_tmp,ichiman,gosen,senen) == True:
                return ichiman,gosen,senen
    return -1,-1,-1

def check(y_tmp,ichiman,gosen,senen):
    #print("y_tmp:",y_tmp,"ichiman:",ichiman,"gosen:",gosen,"senen:",senen,"goukei",ichiman + gosen + senen,n)
    if y_tmp == 0 and ichiman + gosen + senen  == n:
        return  True    
    return False


n,y =nyu()
num1,num2,num3 = otoshidama(n,y)
print(num1,num2,num3)
### BUGGY CODE ###

def resolve():
    import heapq
    n = int(input())
    a = list(map(int, input().split()))
    a = sorted(a)
    while True:
        mini = a[0]
        for i in range(1, len(a)):
            a[i] %= mini
            if sum(a[1:]) == 0:
                print(a[0])
                exit()

        a =[i for i in a if i!= 0]
        a = sorted(a)
resolve()


### FIXED CODE ###

def resolve():
    import heapq
    n = int(input())
    a = list(map(int, input().split()))
    a = sorted(a)
    while True:
        mini = a[0]
        others = a[1:]
        others = [x % mini for x in others]
        if sum (others) == 0:
            print(a[0])
            exit()
        a = [mini] + others
        a =[i for i in a if i!= 0]
        a = sorted(a)
resolve()
### BUGGY CODE ###

N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
time = 0
count = 0
if(min(min(A),min(B))>K):
    print(0)
    exit()
while time<=K:
    if(len(A)!=0 and len(B)!=0):
        if(A[0]<B[0]):
            time+= A[0]
            A.pop(0)
            count +=1
        elif(A[0]>B[0]):
            time += B[0]
            B.pop(0)
            count+=1
    elif((len(A)==0) and (len(B)==0)):
        break
    elif(len(A)==0):
        time += B[0]
        B.pop(0)
        count += 1
    elif(len(B)==0):
        time += A[0]
        A.pop(0)
        count += 1
if(time == K):
    print(count)
else:
    print(count-1)

### FIXED CODE ###

N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
A_sum = [0]
B_sum = [0]
A_sum.extend(A)
B_sum.extend(B)
for i in range(1,N+1):
    A_sum[i]+=A_sum[i-1]
for i in range(1,M+1):
    B_sum[i]+=B_sum[i-1]
ans,j=0,M
for i in range(N+1):
    if A_sum[i]>K:
        break
    while B_sum[j]+A_sum[i]>K:
        j-=1
    ans = max(ans,i+j)
print(ans)
### BUGGY CODE ###

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


diff = ''
for i in range(1, len(H)):
  d = H[i] - H[i-1]
  if d <= 0:
    diff += '1'
  else:
    diff += '0'
ans = max([len(s) for s in diff.split('0') ])
print(ans)
  



### FIXED CODE ###

N = int(input())
H = [int(x) for x in input().split()]
dp = [0] * len(H) 
for i in range(1, len(H)):
  if H[i-1] >= H[i]: 
    dp[i] = dp[i-1] + 1
ans = max(dp)
print(ans)
### BUGGY CODE ###

N=int(input())
*p,=map(float,input().split())
dp=[[0 for _ in range(N+1)] for _ in range(N+1)]
dp[0][0]=1

for i in range(1,N+1):
    for j in range(N+1):
        if j:
            dp[i][j]=dp[i-1][j]*(1-p[i-1])+dp[i-1][j-1]*p[i-1]
        else:
            dp[i][j]=dp[i-1][j]*(1-p[i-1])

t=N//2+1 if N%2 else N//2
ans=0
for i in range(t,N+1):
    ans+=dp[N][i]

print(ans)

### FIXED CODE ###

N=int(input())
*p,=map(float,input().split())
dp=[[0 for _ in range(N+1)] for _ in range(N+1)]
dp[0][0]=1

for i in range(1,N+1):
    for j in range(N+1):
        if j:
            dp[i][j]=dp[i-1][j]*(1-p[i-1])+dp[i-1][j-1]*p[i-1]
        else:
            dp[i][j]=dp[i-1][j]*(1-p[i-1])

t=N//2+1 if N%2 else N//2
ans=0
for i in range(t,N+1):
    ans+=dp[N][i]

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

def resolve():
    N, K, Q = list(map(int, input().split()))
    A = [int(input()) for _ in range(Q)]
    points = [K] * N
    for i in range(N):
        points[i] += A.count(i+1)
        points[i] -= Q
        if points[i] > 0:
            print('Yes')
        else:
            print('No')

    return


resolve()

### FIXED CODE ###

def resolve():
    N, K, Q = list(map(int, input().split()))
    A = [int(input()) for _ in range(Q)]
    points = [0] * N
    for i in range(Q):
        points[A[i]-1] += 1

    for i in range(N):
        if K + points[i] - Q > 0:
            print('Yes')
        else:
            print('No')

    return

resolve()
### BUGGY CODE ###

#d,t,s=map(int,input().split())
n=int(input())
A=list(map(int,input().split()))
ans=0
r=10**9+7
for i in range(n):
  for j in range(i+1,n):
    ans+=(A[i]*A[j])%r
    ans%=r
print(ans)

### FIXED CODE ###

import itertools
#h,w=map(int,input().split())
#S=[list(map(int,input().split())) for _ in range(h)]
n=int(input())
A=list(map(int,input().split()))
Ar=list(itertools.accumulate(A))
ans=0
for i in range(n-1):
    ans+=A[i]*(Ar[n-1]-Ar[i])
    ans%=10**9+7
print(ans)
### BUGGY CODE ###

import time
import copy

d,g = map(int,input().split())
P = []
C = []
for i in range(d):
    p,c = map(int,input().split())
    P.append(p)
    C.append(c)

start = time.time()

m = 10**9
def score(point = 0, n = 0, now=0, done=[]):
    global m
    donecp = copy.deepcopy(done)
    if point >= g:
        if m > n: m = n
    else:
        pointcp = point
        ncp = n
        maxp = d - 1
        while (maxp >= 0):
            if not maxp in donecp:
                break
            else:
                maxp -= 1 
        for i in range(1,P[maxp]+1):
            pointcp += 100*(maxp+1)
            ncp += 1
            if pointcp >= g:
                if m > ncp:
                    m = ncp
                break
        donecp.append(now)
        for i in range(d):
            if not i in donecp:
                score(point+P[now]*100*(now+1) + C[now], n + P[now], i, donecp)

for i in range(d):
    done = []
    score(0,0,i,done)

elapsed_time = time.time() - start

print(m)

### FIXED CODE ###

import math
 
d,g = [int(i) for i in input().split()]
P = []
C = []
for i in range(d):
    p,c = [int(i) for i in input().split()]
    P.append(p)
    C.append(c)

# cnt = 0
# def count():
#     global cnt
#     cnt+=1
#     print(cnt)

def flag(point,P,now):
    if point+100*(now+1)*(P-1) >= g: return 1
    elif point+100*(now+1)*P + C[now] >= g: return 2
    else: return 0

m = 10**9
def score(point = 0, n = 0, now=-1, remain = list(range(d))):
    global m
    if m <= n: return
    elif now == -1:
        for i in remain:
            score(point, n, i, remain)
    elif point >= g: m = n
    elif flag(point,P[now],now) == 1:
        n += math.ceil((g-point)/(100*(now+1)))
        if m > n: m = n
    elif flag(point,P[now],now) == 2:
        n += P[now]
        if m > n: m = n
    else:
        if m <= n+P[now]: return
        else:
            remain = remain[:]
            remain.remove(now)
            nxtp = point + P[now]*100*(now+1) + C[now]
            for i in remain:
                score(nxtp, n + P[now], i, remain)

score()
print(m)
### BUGGY CODE ###

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

cost = []
open = []
for i in range(m):
    a, b = map(int, input().split())
    c = set(list(map(int, input().split())))
    cost.append(a)
    open.append(c)

dp = [10**7] * (2**n)
dp[0] = 0
for i in range(m):
    key = []
    for j in range(1, n+1):
        if j in open[i]:
            key.append("1")
        else:
            key.append("0")
    key = "".join(key)
    key = int(key, 2)
    for j in range(2**n):
        next = j | key
        dp[next] = min(dp[next], dp[j] + cost[i])

if dp[2**n-1] == 10**7:
    print(-1)
else:
    print(dp[2**n-1])


### FIXED CODE ###

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

cost = []
open = []
for i in range(m):
    a, b = map(int, input().split())
    c = set(list(map(int, input().split())))
    cost.append(a)
    open.append(c)

dp = [10**7] * (2**n)
dp[0] = 0
for i in range(m):
    key = []
    for j in range(1, n+1):
        if j in open[i]:
            key.append("1")
        else:
            key.append("0")
    key = "".join(key)
    key = int(key, 2)
    for j in range(2**n):
        next = j | key
        dp[next] = min(dp[next], dp[j] + cost[i])

if dp[2**n-1] == 10**7:
    print(-1)
else:
    print(dp[2**n-1])
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
max_value = 0
ans_p = 0
for i in range(n):
    sum_ = sum(p[i:i+k])
    if (sum_ > max_value):
        max_value = sum_
        ans_p = p[i:i+k]
print((max_value+k)/2)


### FIXED CODE ###

# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
max_value = 0
ans_p = 0

s = [0]*(n+1)
for i, _p in enumerate(p):
    s[i+1] = s[i] + _p

for i in range(n-k+1):
    sum_ = s[i+k] - s[i]
    if (max_value < sum_):
        max_value = sum_

print((max_value+k)/2)
### BUGGY CODE ###

import numpy as np
import math 
N, K=map(int,input().split())
A2 = np.zeros(N)
A = list(map(int,input().split()))
A = list(map(lambda x: x -1 , A))
current_town = 0



i=0
while i<K:
    current_town = A[current_town]
    if  A2[current_town] != 0:
        cycle = i - A2[current_town] 
        i = K - (K-i) % cycle
    else:
        A2[current_town] = i

print(current_town+1)


### FIXED CODE ###

import numpy as np
n, k=map(int,input().split())

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

 
s = list()
ord = np.full(n+1, -1)
c=1
l=0

v=1
while ord[v]==-1:
    ord[v]=len(s)
    s.append(v)
    v=a[v-1]
c=len(s)-ord[v]
l=ord[v]

if k<l:
    print(s[k])  
else:
    k-=l
    k%=c
    print(s[l+k])