Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
di = [1]
fi = [-1]*(n+1)
a = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(a)

Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
di = [1]
fi = [0]*(n+1)
a = 0
for i in range(k):
    if fi[ls[a]] == 0:
        fi[ls[a]] = i
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(a)

Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
di = [1]
fi = [0]*(n+1)
a = 0
for i in range(k+1):
    if fi[ls[a]] == 0:
        fi[ls[a]] = i
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(a)

Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
i = 1
di = [-1]*10
di[0] = 0
if k >= 10**9+7:
  k%= 10**9+7
while True:
    if i == 1:
        a = ls[0]
        di[a-1] = i
        i += 1
        continue
    if i == k+1:
        print(a)
        break
    a = ls[a-1]
    if di[a-1] != -1:
        p = (k+1-di[a-1])%(i-di[a-1])
        s = i-di[a-1]
        if p ==0:
            q = di.index(i-1)+1
            print(q)
        else:
            q = di.index(i-s-1+p)+1
            print(q)
        break
    di[a-1] = i
    i += 1

Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
i = 1
di = [-1]*10
di[0] = 0
while True:
    if i == 1:
        a = ls[0]
        di[a-1] = i
        i += 1
        continue
    if i == k+1:
        print(a)
        break
    a = ls[a-1]
    if di[a-1] != -1:
        p = (k+1-di[a-1])%(i-di[a-1])
        s = i-di[a-1]
        if p ==0:
            q = di.index(i-1)+1
            print(q)
        else:
            q = di.index(i-s-1+p)+1
            print(q)
        break
    di[a-1] = i
    i += 1
print(di)

Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
i = 1
di = [-1]*10
di[0] = 0
while True:
    if i == 1:
        a = ls[0]
        di[a-1] = i
        i += 1
        continue
    if i == k+1:
        print(a)
        break
    a = ls[a-1]
    if di[a-1] != -1:
        p = (k+1-di[a-1])%(i-di[a-1])
        s = i-di[a-1]
        if p ==0:
            q = di.index(i-1)+1
            print(q)
        else:
            q = di.index(i-s-1+p)+1
            print(q)
        break
    di[a-1] = i
    i += 1

Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n,k = map(int,input().split())
ls = list(map(int,input().split()))
def sub(k,s):
    return k-s
p = [1]
i = 0
a = 0
t = 0
ans1 = 0
ans2 = 0
e = 3
while i<k-1:
    if i ==0:
        a = ls[i]
        p.append(a)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        b = ls[a-1]
        p.append(b)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
    else:
        a = b
        b = ls[a-1]
        p.append(b)
        e += 1
        f = len(list(set(p)))
        if e != f:
            q = [p[-1]]*(e)
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = e-1 - ans1
            ansl = p[t:]
            print(ansl[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
print(b)




Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n,k = map(int,input().split())
ls = list(map(int,input().split()))
def sub(k,s):
    return k-s
p = [1]
i = 0
a = 0
t = 0
ans1 = 0
ans2 = 0
e = 3
while i<k-1:
    if i ==0:
        a = ls[i]
        p.append(a)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        b = ls[a-1]
        p.append(b)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
    else:
        a = b
        b = ls[a-1]
        p.append(b)
        e += 1
        f = len(list(set(p)))
        if e != f:
            q = [p[-1]]*(e)
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = e-1 - ans1
            ansl = p[t:]
            print(ansl[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
print(b)




Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n,k = map(int,input().split())
ls = list(map(int,input().split()))
new = []
def sub(k,s):
    return k-s
p = [1]
i = 0
a = 0
t = 0
ans1 = 0
ans2 = 0
while i<k-1:
    if i ==0:
        a = ls[i]
        p.append(a)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        b = ls[a-1]
        p.append(b)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            print(p[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
    else:
        a = b
        b = ls[a-1]
        p.append(b)
        if len(p) != len(list(set(p))):
            q = [p[-1]]*(len(p))
            p.pop()
            new = list(map(sub,p,q))
            t = new.index(0)
            ans1 = len(p[:t])
            ans2 = len(p[t:])
            ansl = p[t:]
            print(ansl[((k+1-ans1)%ans2)-1])
            sys.exit()
        i += 1
print(b)




Fixed submission:
n,k = map(int,input().split())
ls = list(map(int,input().split()))
fi = [-1]*(n+1)
fi[1] = 0
a = 0
aa = 0
for i in range(k):
    if fi[ls[a]] == -1:
        fi[ls[a]] = i + 1
        aa = a
        a = ls[a] - 1
    else:
        ind = fi[ls[a]]
        t = (k - ind) % (max(fi) + 1 - ind)
        t += ind
        print(fi.index(t))
        break
else:
    print(ls[aa])