Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02684 (python)

Fixed submission:
import numpy as np

def main():
    N,K = map(int,input().split())
    A = [0]
    A += list(map(int,input().split()))
    A = np.array(A)
    
    i = 1
    pas = np.zeros(N)
    pas[0] = i
    k = 1
    pass_set = set(pas)
    while k <= K:
        if A[i] in pass_set:
            rps = np.where(pas==(A[i]))[0]
            ans = A[i]
            break
        pas[k] = A[i]
        pass_set.add(A[i])
        ans = A[i]
        i = A[i]
        k += 1

    if k >= K:
        print(ans)
    else:
        rpnum = (K-rps[0])%(k-rps)
        print(int(pas[rps[0]+rpnum]))

main()

Buggy submission (verdict: Runtime Error):

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

memo = [1]
setm = set(memo)
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
    else:
        break
        
idx = memo.index(to)
kidx = ((k - len(memo))%(len(memo)-idx))
if cnt<=k:
    print(memo[-2])
else:
    print(memo[-1] if kidx==0 else memo[idx:][kidx-1])

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = list(map(int,input().split()))

memo = [1]
setm = set(memo)
cnt = 1
to = 1
while cnt <= k:
    to = a[to-1]
    if to not in setm:
        memo.append(to)
        setm.add(to)
        cnt += 1
    else:
        break
        
idx = memo.index(to)
kidx = ((k - len(memo))%(len(memo)-idx))
if len(memo)>k:
    print(memo[-2])
else:
    print(memo[-1] if kidx==0 else: memo[idx:][kidx])

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

def main():
    N,K = map(int,input().split())
    A = [0]
    A += list(map(int,input().split()))
    A = np.array(A)
    
    s_time =time.time()
    i = 1
    pas = np.zeros(N)
    pas[0] = i
    k = 1
    pass_set = set(pas)
    while k <= K:
        if A[i] in pass_set:
            rps = np.where(pas==(A[i]))[0]
            ans = A[i]
            break
        pas[k] = A[i]
        pass_set.add(A[i])
        ans = A[i]
        i = A[i]
        k += 1

    if k >= K:
        print(ans)
    else:
        rpnum = (K-rps[0])%(k-rps)
        print(int(pas[rps[0]+rpnum]))

main()

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))

i = 1
pas = [i]
k = K
while k > 0:
    if A[i] in pas:
        break
    pas.append(A[i])
    i = A[i]
    k -= 1

roopindx = pas.index(A[i])

roopi = K%(len(pas)-roopindx)

print(pas[roopi])
Problem p02696 (python)

Fixed submission:
a,b,n = map(int,input().split())
x=min(b-1,n)
print(((a*x)//b) - a*(x//b))

Buggy submission (verdict: Runtime Error):
a,b,n = map(int,input().split())

y = 0

for x in range(1,n+1):
    if x%b!=0:
        temp = ((a*x)//b) - a*(x//b)
    else:
        temp = 0
    if y < temp:
        y = temp
        ans = x
    
print(ans)
Problem p02713 (python)

Fixed submission:
from math import gcd

k = int(input())
ans = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        n = gcd(a,b)
        for c in range(1,k+1):
            ans += gcd(n,c)
print(ans)

Buggy submission (verdict: Runtime Error):
from math

k = int(input())
ans = 0

for a in range(1,k+1):
    if i == 1:
        ans += k*k
        continue
    for b in range(1,k+1):
        n = math.gcd(a,b)
        if n == 1:
            ans += k
            continue
        for c in range(1,k+1):
            ans += math.gcd(n,c)
print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd

k = int(input())
ans = 0

for a in range(1,k+1):
    if i == 1:
        ans += k*k
        continue
    for b in range(1,k+1):
        n = gcd(a,b)
        if n == 1:
            ans += k
            continue
        for c in range(1,k+1):
            ans += gcd(n,c)
print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd

k = int(input())
ans = 0

n = list(product([i for i in range(1,k+1)],repeat=3))
for a,b,c in n:
    n = gcd(a,b)
    ans += gcd(n,c)
print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd

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

k = int(input())
ans = 0

n = list(product([i for i in range(1,k+1)],repeat=3))
for a,b,c in n:
    n = gcd(a,b)
    ans += gcd(n,c)
print(ans)
Problem p02760 (python)

Fixed submission:
import numpy as np

a = [list(map(int,input().split())) for _ in range(3)]

n = int(input())

b = list([int(input()) for _ in range(n)])

c = np.zeros(9).reshape(3,3)

for k in b:
    for i in range(3):
        for j in range(3):
            if k == a[i][j]:
                c[i,j]=1

if (max(c.sum(axis=0))==3) or (max(c.sum(axis=1))==3) or \
    (sum([c[i,i] for i in range(3)])==3) or (c[2,0]+c[1,1]+c[0,2]==3):
    print("Yes")
else:
    print("No")

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

a = [list(map(int,input().split())) for _ in range(3)]

n = int(input())

b = list([int(input()) for _ in range(n)])

c = [[0]*3]*3

for k in b:
    for i in range(3):
        for j in range(3):
            if k == a[i][j]:
                c[i][j]=1

c = np.array(c)

if (max(c.sum(axis=0))==3) or (max(c.sum(axis=1))==3) or \
    (sum([a[i,i] for i in range(3)])==3) or (a[2,0]+a[1,1]+a[0,2]==3):
    print("Yes")
else:
    print("No")

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

a = [list(map(int,input().split())) for _ in range(3)]

n = int(input())

b = list([int(input()) for _ in range(n)])

c = [[0]*3]*3

for k in b:
    for i in range(3):
        for j in range(3):
            if k == a[i][j]:
                c[i][j]=1

c = np.array(c)

if (max(c.sum(axis=0))==3) or (max(c.sum(axis=1))==3) or \
    (sum([a[i,i] for i in range(3)])==3) or (sum(a[2,0]+a[1,1]+a[0,2])==3):
    print("Yes")
else:
    print("No")

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

# a = [list(map(int,input().split())) for _ in range(3)]

# n = int(input())

# b = list([int(input()) for _ in range(n)])

c = [[0]*3]*3

for k in b:
#     print("k={}".format(k))
    for i in range(3):
        for j in range(3):
#             print("  a={}".format(a[i][j]))
            if k == a[i][j]:
                c[i][j]=1

c = np.array(c)

if (max(c.sum(axis=0))==3) or (max(c.sum(axis=1))==3) or \
    (sum([a[i,i] for i in range(3)])==3) or (sum(a[2,0]+a[1,1]+a[0,2])==3):
    print("Yes")
else:
    print("No")
Problem p02761 (python)

Fixed submission:
n, m = map(int,input().split())
sc = [list(map(int,input().split())) for _ in range(m)]

ans = -1

for i in range(1000):
    s_i = str(i)
    if len(s_i) == n and all(int(s_i[s-1])==c for s,c in sc):
        ans = i
        break

print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int,input().split())

a = [""]*3
rep = []
ans = 0

for i in range(m):
    s,c = input().split()
    if s in rep:
        if c != a[int(s)-1]:
            ans = -1
    rep.append(s)
    a[int(s)-1] = c

if ans == -1:
    print(ans)
else:
    if a[0] == "":
        if a[1] == "":
            print(int(a[2]))
        elif a[2]=="":
            a[2]="0"
            print("".join(a))
    else:
        if a[1] == "":
            a[1] = "0"
            print("".join(a))
        elif a[2]=="":
            a[2]="0"
            print("".join(a))

Buggy submission (verdict: Runtime Error):
n, m = map(int,input().split())

a = [""]*3
rep = []
ans = 0

for i in range(m):
    s,c = input().split()
    if s in rep:
        if c != a[int(s)-1]:
            ans = -1
    rep.append(s)
    a[int(s)-1] = c

if ans == -1:
    print(ans)
else:
    if a[0] == "":
        if a[1] == "":
            print(int(a[2]))
        elif a[2]=="":
            a[2]="0"
            print("".join(a))
    else:
        if a[1] == "":
            print(0)
        elif a[2]=="":
            a[2]="0"
            print("".join(a))

Buggy submission (verdict: Runtime Error):
n, m = map(int,input().split())

# sc = [list(map(int,input().split())) for _ in range(m)]

a = [""]*3
rep = []
ans = 0

for i in range(m):
    s,c = input().split()
    if s in rep:
        if c != a[int(s)-1]:
            ans = -1
    rep.append(s)
    a[int(s)-1] = c
    
print(a)

if ans == -1:
    print(ans)
else:
    if a[0] == "":
        if a[1] == "":
            print(int(a[2]))
        elif a[2]=="":
            a[2]="0"
            print("".join(a))
    else:
        if a[1] == "":
            print(0)
        elif a[2]=="":
            a[2]="0"
            print("".join(a))
Problem p02786 (python)

Fixed submission:
h = int(input())
sumn = 0
i,k = 0,1
while k <= h:
    sumn+=k
    i+=1
    k = 2**i
    
print(sumn)

Buggy submission (verdict: Runtime Error):
H = int(input())
dn = [0]*(H//2+1)
dn[0] = 1

for i in range(1,H//2+1):
    dn[i] = dn[i-1]*2
print(sum(dn))
Problem p03109 (python)

Fixed submission:
s = input()
print("Heisei" if s<="2019/04/30" else "TBD")

Buggy submission (verdict: Runtime Error):
y,m,d = int(input().split("/"))
print("Heisei" if y<=2019 and m<=4 and d<=30 else "TBD")
Problem p03112 (python)

Fixed submission:
import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for i in range(A)]+[INF]
T = [-INF]+[int(input()) for i in range(B)]+[INF]
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[j-1],T[j]]:
            d1 = abs(s-x)+abs(s-t)
            d2 = abs(t-x)+abs(s-t)
            d = min(d,d1,d2)
    print(d)

Buggy submission (verdict: Runtime Error):
import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[i-1],T[i]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
for i in ans:
    print(i)

Buggy submission (verdict: Runtime Error):
import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[i-1],T[i]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
print(*ans,sep="\n")

Buggy submission (verdict: Runtime Error):
import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_left(S,x)
    j = bisect.bisect_left(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[i-1],T[i]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
print(*ans,sep="\n")
Problem p03166 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**5+10)
def memo(v):
    if dp[v]!=-1:return dp[v]
    ans = 0
    for next_v in g[v]:
        ans = max(ans,memo(next_v)+1)
    dp[v] = ans
    return ans

n,m = map(int,input().split())
g = [[] for _ in range(n+1)]
for _ in range(m):
    x,y = map(int,input().split())
    g[x].append(y)
    
dp = [-1]*(n+1)

ans = 0
for v in range(1,n+1):
    ans = max(ans, memo(v))
    
print(ans)

Buggy submission (verdict: Runtime Error):
def memo(v):
    if dp[v]!=-1:return dp[v]
    ans = 0
    for next_v in g[v]:
        ans = max(ans,memo(next_v)+1)
    dp[v] = ans
    return dp[v]

n,m = map(int,input().split())
g = [[] for _ in range(n+1)]
for _ in range(m):
    x,y = map(int,input().split())
    g[x].append(y)
    
dp = [-1]*(n+1)

ans = 0
for v in range(1,n+1):
    ans = max(ans, memo(v))
    
print(ans)

Buggy submission (verdict: Runtime Error):
def memo(v):
    if dp[v]!=-1:return dp[v]
    ans = 0
    for next_v in g[v]:
        ans = max(ans,memo(next_v)+1)
    dp[v] = ans
    return dp[v]

n,m = map(int,input().split())
g = [[] for _ in range(n+1)]
for _ in range(m):
    x,y = map(int,input().split())
    g[x].append(y)
    
dp = [-1]*(n+1)

ans = 0
for v in range(1,n+1):
    print("v={}".format(v))
    ans = max(ans, memo(v))
    
print(ans)

Buggy submission (verdict: Runtime Error):
def memo(v):
    if dp[v]!=-1:return dp[v]
    ans = 0
    for next_v in g[v]:
        ans = max(ans,memo(next_v)+1)
    dp[v] = ans
    return dp[v]

n,m = map(int,input().split())
g = [[] for _ in range(n+1)]
for _ in range(m):
    x,y = map(int,input().split())
    g[x].append(y)
    
dp = [-1]*(n+1)

ans = 0
for v in range(n):
    ans = max(ans, memo(v))
    
print(ans)

Buggy submission (verdict: Runtime Error):
def main():
    n,m = map(int,input().split())
    g = [[] for _ in range(n+1)]
    for _ in range(m):
        x,y = map(int,input().split())
        g[x].append(y)

    dp = [-1]*(n+1)

    def memo(v):
        if dp[v]!=-1:return dp[v]
        ans = 0
        for next_v in g[v]:
            ans = max(ans,memo(next_v)+1)
        dp[v] = ans
        return dp[v]

    ans = 0
    for v in range(n):
        ans = max(ans, memo(v))

    print(ans)

main()

Buggy submission (verdict: Runtime Error):
def main():
    n,m = map(int,input().split())
    g = [[] for _ in range(n)]
    for _ in range(m):
        x,y = map(int,input().split())
        g[x].append(y)

    dp = [-1]*(n+1)

    def memo(v):
        if dp[v]!=-1:return dp[v]
        ans = 0
        for next_v in g[v]:
            ans = max(ans,memo(next_v)+1)
        dp[v] = ans
        return dp[v]

    ans = 0
    for v in range(n):
        ans = max(ans, memo(v))

    print(ans)

main()
Problem p03212 (python)

Fixed submission:
from itertools import product

n = input()

if int(n) < 100:
    print(0)

else:
    ans = 0

    for i in range(3,len(n)+1):
        s = list(product("753",repeat=i))
        for combi in s:
            n_s = "".join(combi)
            if int(n_s) > int(n):
                continue
            if ("7" in combi) and ("5" in combi) and ("3" in combi):
                ans += 1
    
    print(ans)

Buggy submission (verdict: Runtime Error):
from itertools import product

n = input()

if int(n) < 100:
    print(0)

else:
    ans = 0

    for i in range(3,len(n)+1):
        s = list(product("753",repeat=i))
        for combi in s:
            n_s = "".join(combi)
            if int(n_s) > int(n):
                continue
            if ("7" in combi) and ("5" in combi) and ("3" in combi):
                ans += 1
    
	print(ans)

Buggy submission (verdict: Runtime Error):
from itertools import product

n = input()

if int(n) < 100:
    print(0)

else:
    ans = 0

    for i in range(3,len(n)+1):
        s = list(product("753",repeat=i))
        for combi in s:
            n_s = "".join(combi)
            if int(n_s) > int(n):
                continue
            if ("7" in combi) and ("5" in combi) and ("3" in combi):
                ans += 1
    
print(ans)
Problem p03239 (python)

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

m = 1001

for i in range(N):
    
    c,t = map(int,input().split())
    
    if t <= T:
        m=min(m,c)
        
print("TLE" if m==1001 else m)

Buggy submission (verdict: Runtime Error):
n,t = map(int,input().split())

m = 1001

for i in range(N):
    
    c,t = map(int,input().split())
    
    if t <= T:
        m=min(m,c)
        
print("TLE" if m==1001 else m)
Problem p03352 (python)

Fixed submission:
x = int(input())
c=1
for b in range(1,x):
    for p in range(2,x):
        if b**p<=x:c=max(c,b**p)
        else:break
print(c)

Buggy submission (verdict: Runtime Error):
x = int(input())
for b in range(1,x):
    for p in range(2,x):
        if b**p<=x:c=max(c,b**p)
        else:break
print(c)
Problem p03719 (python)

Fixed submission:
a,b,c = map(int,input().split())
print("Yes" if c>=a and c<=b else "No")

Buggy submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
print("Yes" if c>=a and c<=B else "No")
Problem p03738 (python)

Fixed submission:
a=int(input())
b=int(input())
print("GREATER" if a>b else "LESS" if a<b else "EQUAL")

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
print("GREATER" if a>b else "LESS" if a<b else "EQUAL")
Problem p03796 (python)

Fixed submission:
import math
print(math.factorial(int(input()))%(10**9+7))

Buggy submission (verdict: Runtime Error):
print(math.factorial(int(input()))%(10**9+7))
Problem p02640 (python)

Fixed submission:
N = input().split()
X = int(N[0])
Y = int(N[1])

flag = False

for i in range(X):
    if flag:
        break

    if (X-i)*2 + i*4 == Y:
        flag = True
    
    if (X-i)*4 + i*2 == Y:
        flag = True

if flag:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N = input().split()
X = int(N[0])
Y = int(N[1])

flag = False

if Y % 2 != 0:
    prunt("No")
    exit()

def memo(x, y):
    global flag
    if x == 0:
        if y == 0:
            flag = True
        return flag

    
    memo(x-1, y-2)
    memo(x-1, y-4)

memo(X, Y)

if flag:
    print("Yes")
else:
    print("No")
Problem p02789 (python)

Fixed submission:
line = input().split(" ")
N = int(line[0])
M = int(line[1])

if(N == M):
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
M = int(input())

if(N == M):
    print("Yes")
else:
    print("No")
Problem p03434 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
Alice = 0
Bob = 0
for i in range(N):
  if i%2 == 0:
    Alice += A[i]
  else:
    Bob += A[i]
ans = Alice - Bob
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
a.sort()
Alice = 0
Bob = 0
for i in range(n):
  if i%2 == 0:
    Alice += A[i]
  else:
    Bob += A[i]
ans = Alice - Bob
print(ans)
Problem p03555 (python)

Fixed submission:
t1=raw_input()
t2=raw_input()
if t1[0]==t2[2] and t1[1]==t2[1] and t1[2]==t2[0]:
 print "YES"
else:
 print "NO"

Buggy submission (verdict: Runtime Error):
t1=raw_input()
t2=raw_input()
if t1[0]==t2[2] and t1[1]==t2[1] and t1[2]==t2[0]:
 return "YES"
else
 return "NO"

Buggy submission (verdict: Runtime Error):
t1=raw_input().split(' ')
t2=raw_input().split(' ')
if t1[0]==t2[2] and t1[1]==t2[1] and t1[2]==t2[0]:
 return "YES"
else
 return "NO"
Problem p02630 (python)

Fixed submission:
# -*- coding: utf-8 -*-
N = int(input())
A_list = list(map(int, input().split()))
Q = int(input())

count_list = [0 for i in range(10 ** 5 + 1)]
for i in range(len(A_list)):
    count_list[A_list[i]] += 1

ans = sum(A_list)
for i in range(Q):
    B, C = map(int, input().split())
    ans = ans - count_list[B] * B + count_list[B] * C
    print(ans)
    count_list[C] += count_list[B]
    count_list[B] = 0

Buggy submission (verdict: Runtime Error):
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
  dic.setdefault(a[i],1)
  dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,0)
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

Buggy submission (verdict: Runtime Error):
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
  dic.setdefault(a[i],1)
  dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,0)
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)
Problem p02660 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import collections
# factrize
def factorize(target_number):
    factor = 2
    factor_list = []
    while factor * factor <= target_number:
        while target_number % factor == 0:
            target_number //= factor
            factor_list.append(factor)
        factor = factor + 1
    if target_number > 1:
        factor_list.append(target_number)
    return factor_list

N = int(input())

fact_list = factorize(N)

fact_counter_dict = collections.Counter(fact_list)
ans = 0
for fact, freq in fact_counter_dict.items():
    freq_copy = freq
    for i in range(1, freq + 1):
        freq_copy -= i
        if freq_copy < 0:
            break
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import collections
# factrize
def factorize(target_number):
    factor_list = []
    while target_number % 2 == 0:
        factor_list.append(2)
        n //= 2
    factor = 3
    while factor * factor <= target_number:
        while target_number % factor == 0:
            target_number //= factor
            factor_list.append(factor)
        factor += 2
    if target_number > 1:
        factor_list.append(target_number)
    return factor_list

N = int(input())

fact_list = factorize(N)

fact_counter_dict = collections.Counter(fact_list)
ans = 0
for fact, freq in fact_counter_dict.items():
    freq_copy = freq
    for i in range(1, freq + 1):
        freq_copy -= i
        if freq_copy < 0:
            break
        ans += 1
print(ans)
Problem p02684 (python)

Fixed submission:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1:
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) > K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1:
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A_list = list(map(int, input().split()))

ans_list = [1]
history_list = [0 for _ in range(N)]
before_town = 1
loop_start_town = 10 ** 10
for i in range(N):
    now_town = A_list[before_town - 1]
    if history_list[now_town - 1] == 1
        break
    ans_list.append(now_town)
    before_town = now_town

if len(ans_list) >= K:
    print(ans_list[K])
else:
    loop_start_idx = ans_list.index(A_list[ans_list[-1] - 1])
    loop_len = len(ans_list)
    ans_list = ans_list[loop_start_idx:]
    mod_num = (K - loop_len) % (loop_len - loop_start_idx)
    print(ans_list[mod_num])