CodeEff/ECCO huggingface dataset:
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
visited=[]
x=0
now=1
next_point=a[now-1]
for cnt in range(min(n,k)):
    now=next_point
    next_point=a[now-1]
    if now in visited:
        x=visited.index(now)
        break
    else:
        visited.append(now)
new_visited=visited[x:]
k-=len(visited)-len(new_visited)

#print(visited)
#print(new_visited)

print((new_visited[k%len(new_visited)-1]))


# Optimized solution
N,K=list(map(int,input().split()))
A=[int(a)-1 for a in input().split()]
city=0
if K<=N:
    for k in range(K):
        city=A[city]
    print((city+1))
else:
    visited=[-1]*N
    for k in range(K):
        if visited[city]!=-1:
            roop_start=visited[city]
            cycle_num=k-roop_start
            break
        visited[city]=k
        city=A[city]
    K=(K-roop_start)%cycle_num+roop_start
    city=0
    for k in range(K):
        city=A[city]
    print((city+1))
# Original solution
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))

machi = 1
toutatu = []
while not machi in set(toutatu):
	toutatu.append(machi)
	machi = a[machi-1]
saigo = machi
num = toutatu.index(saigo)
unchi = toutatu[num:]
cnt = len(unchi)
num = toutatu.index(saigo)

if num > k:
	print((toutatu[k]))
else:
	print((unchi[(k-num)%cnt]))

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

machi = 1
toutatu = []
pypyosoi = set()
while not machi in pypyosoi:
	toutatu.append(machi)
	pypyosoi.add(machi)
	machi = a[machi-1]

saigo = machi
num = toutatu.index(saigo)
unchi = toutatu[num:]
cnt = len(unchi)

if num > k:
	print((toutatu[k]))
else:
	print((unchi[(k-num)%cnt]))

#pypyが遅いのは自分のコードが悪いだけごめんねpypyちゃん
# Original solution
from collections import deque
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
roop_find_list = [0]
i = 0
count = 0
for _ in range(n):
    if a[i]-1 in roop_find_list:
        break
    else:
        roop_find_list.append(a[i]-1)
        i = a[i] - 1
        count += 1
b = roop_find_list.index(a[i]-1)

if k < b:
    print((roop_find_list[k]+1))
else:
    k-=b
    e = k % (count+1-b)
    print((roop_find_list[b+e]+1))


# Optimized solution
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
pos = 0
visit = [0] * n
move = []
loop = []
while visit[pos] != 2:
    if visit[pos] == 0:
        move.append(pos)
    else:
        loop.append(pos)
    visit[pos] += 1
    pos = a[pos] - 1
if len(move) > k:
    print((move[k]+1))
else:
    print((loop[(k-(len(move)-len(loop)))%len(loop)]+1))
# Original solution
n,k,*a=map(int,open(0).read().split())
d=[0]+[-1]*n
s=0
for i in range(k):
  s=a[s]-1
  if(j:=d[s])>-1:
    exec((k-j)%(i+1-j)*'s=a[s]-1;')
    exit(print(s+1))
  d[s]=i+1
print(s+1)


# Optimized solution
n,k,*a=list(map(int,open(0).read().split()))
d=[2*k]*n
s=0
while k:s=a[s]-1;d[s]=k=~-k%(d[s]+1-k)
print((s+1))
# Original solution
N, K = list(map(int, input().split()))
A = [[-1] * (N + 1) for i in range(100)]
A[0] = [0] + list(map(int, input().split()))


# nowから2**i先まで移動したとこ
def double(now, i):
    global A
    if A[i][now] != -1:
        return A[i][now]
    # nowから2**(i-1)移動して、そこからさらに2**(i-1)移動する
    A[i][now] = double(double(now, i - 1), i - 1)
    return A[i][now]


# nowからk移動した場所を求める
def rec(now, k):
    if k == 0:
        return now
    # if k == 1:
    #     return A[now]

    log2k = len(bin(k)) - 3
    return rec(double(now, log2k), k - 2 ** log2k)

print((rec(1, K)))


# Optimized solution
N, K = list(map(int, input().split()))
A = [[-1] * (N + 1) for i in range(100)]
A[0] = [0] + list(map(int, input().split()))


# nowから2**i先まで移動したとこ
def double(now, i):
    global A
    if A[i][now] != -1:
        return A[i][now]
    # nowから2**(i-1)移動して、そこからさらに2**(i-1)移動する
    A[i][now] = double(double(now, i - 1), i - 1)
    return A[i][now]


# nowからk移動した場所を求める
def rec(now, k):
    if k == 0:
        return now
    log2k = len(bin(k)) - 3
    return rec(double(now, log2k), k - 2 ** log2k)

print((rec(1, K)))
# Original solution
N, K = list(map(int, input().split()))
A = [[-1] * (N + 1) for i in range(100)]
A[0] = [0] + list(map(int, input().split()))


# nowから2**i先まで移動したとこ
def double(now, i):
    global A
    if A[i][now] != -1:
        return A[i][now]
    # nowから2**(i-1)移動して、そこからさらに2**(i-1)移動する
    A[i][now] = double(double(now, i - 1), i - 1)
    return A[i][now]


# nowからk移動した場所を求める
def rec(now, k):
    if k == 0:
        return now
    log2k = len(bin(k)) - 3
    return rec(double(now, log2k), k - 2 ** log2k)

print((rec(1, K)))


# Optimized solution
n, k = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))

# 訪問順
B = [1]

# 訪問済み
C = [False] * (n + 1)
C[1] = True


while True:
    B.append(A[B[-1]])
    if C[B[-1]]:
        break
    C[B[-1]] = True

s = B.index(B[-1])

if k < s:
    print((B[k]))
    exit()

B = B[B.index(B[-1]):-1]
print((B[(k - s) % len(B)]))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

pos = 0
visit = [0]*n
move = []
roop = []
while visit[pos] != 2:
    if visit[pos] == 0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = a[pos] - 1
if len(move) > k:
    print((move[k]+1))
else:
    print((roop[(k-(len(move)-len(roop))) % len(roop)]+1))


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

num = 1
li = [1]
flag = [True]*n
flag[0] = False

for i in range(k):
    num = a[num-1]
    if flag[num-1]:
        li.append(num)
        flag[num-1] = False
    else:
        break

d = li.index(num)
ans = (k-d) % (len(li)-d)+d
print((li[ans]))
# Original solution
def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))

    # ループしているところを見つける
    path = [1]
    current = 0
    looping = False
    loop_st = -1
    loop_ed = -1
    for i in range(n):
        _next = a[current]

        if _next in path:
            loop_st = path.index(_next)
            loop_ed = len(path)
            looping = True
            break
        path.append(_next)
        current = _next-1

    if looping:
        if loop_ed - 1 >= k:
            print((path[k]))
        else:
            len_loop = loop_ed - loop_st
            z = (k - loop_st) % len_loop
            print((path[loop_st + z]))
    else:
        print((path[k]))


main()


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

    # ループしているところを見つける
    path = [1]
    dic = [0] * n
    dic[0] = 1
    current = 0
    looping = False
    loop_st = -1
    loop_ed = -1
    for i in range(n):
        _next = a[current]
        if dic[_next-1] == 1:
            loop_st = path.index(_next)
            loop_ed = len(path)
            looping = True
            break
        dic[_next-1] = 1
        # if _next in path:
        #     loop_st = path.index(_next)
        #     loop_ed = len(path)
        #     looping = True
        #     break
        path.append(_next)
        current = _next-1

    if looping:
        if loop_ed - 1 >= k:
            print((path[k]))
        else:
            len_loop = loop_ed - loop_st
            z = (k - loop_st) % len_loop
            print((path[loop_st + z]))
    else:
        print((path[k]))


main()
# Original solution
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
import sys
sys.setrecursionlimit(10**9)

def f(L):
    if A[L[-1]-1] not in L:
        L_next = L + [A[L[-1]-1]]
        return f(L_next)
    else:
      return L
L = [1]
P = f(L)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j
if k+1 <= j:
    print((P[k]))
else:
    print((P[j+(k-j)%l]))

# Optimized solution
import sys
sys.setrecursionlimit(10**6)

n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [1]
S = {1}

def f(L, S):
    last = L[-1]-1
    if A[last] not in S:
        L.append(A[last])
        S |= {A[last]}
        return f(L, S)
    else:
        return L
P = f(L, S)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j

if k+1 <= j:
    print((P[k]))
else:
    print((P[j+(k-j)%l]))
# Original solution
import sys
sys.setrecursionlimit(10**6)

n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [1]
S = {1}

def f(L, S):
    last = L[-1]-1
    if A[last] not in S:
        L.append(A[last])
        S |= {A[last]}
        return f(L, S)
    else:
        return L
P = f(L, S)
a = A[P[-1]-1]
j = P.index(a)
r = len(P)
l = r - j

if k+1 <= j:
    print((P[k]))
else:
    print((P[j+(k-j)%l]))

# Optimized solution
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [1]
S = {1}

while A[L[-1]-1] not in S:
  last = L[-1]-1
  L.append(A[last])
  S |= {A[last]}

a = A[L[-1]-1]
j = L.index(a)
r = len(L)
l = r - j

if k+1 <= j:
    print((L[k]))
else:
    print((L[j+(k-j)%l]))
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, k = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
def f(i):
  if i == 1:
    return A
  L = [-1]*(n+1)
  if i & 1:
    F = f(i-1)
    for j in range(n+1):
      L[j] = A[F[j]]
  else:
    F = f(i//2)
    for j in range(n+1):
      L[j] = F[F[j]]
  return L
F = f(k)
ans = F[1]
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
D = [1]
seen = [0]*n
seen[0] = 1
now = 0
while True:
    now = A[now] - 1
    D.append(now+1)
    if seen[now]:
        break
    seen[now] = 1
f = D.index(D[-1])
if k < f:
    print((D[k]))
else:
    cycle = len(D) - f - 1
    k -= f
    print((D[f + k%(cycle)]))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split())) 
b = 0
c = [0]
for i in range(k):
  b = a[b]-1
  if b in c:
    d = c.index(b)
    e = i-d+1
    k = k - len(c)+e
    c = c[-e:]
    print((c[k%e]+1))
    exit(0)
  else:
    c.append(b)
print((b+1))

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split())) 
b = 0
if k<=n:
  for i in range(k):
    b = a[b]-1
  print((b+1))
  exit(0)
c = [0]
for i in range(n):
  b = a[b]-1
  c.append(b)
d = c[:-1].index(c[-1])
c = c[d:-1]
k = k-d
print((c[k%len(c)]+1))
# Original solution
n, k = list(map(int, input().split()))

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

res = [0]
tmp = 0

tf = [-1 for i in range(n)]
tf[0] = 0

for i in range(n):
    tmp = a[tmp] - 1
    if tf[tmp] != -1:
        start = tmp
        break
    else:
        res = res + [tmp]
        tf[tmp] = i+1

lenret = i+1
stridx = tf[start]

cycle = lenret - stridx

if stridx >= k:
    print((res[k]+1))
    exit()
else:
    k = k - stridx

    tmp = k % cycle

    print((res[stridx+tmp]+1))


# Optimized solution
n, k = list(map(int, input().split()))

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

res = [0]
tmp = 0

tf = [-1 for i in range(n)]
tf[0] = 0

for i in range(n):
    tmp = a[tmp] - 1
    if tf[tmp] != -1:
        start = tmp
        break
    else:
        res.append(tmp)
        tf[tmp] = i+1

lenret = i+1
stridx = tf[start]

cycle = lenret - stridx

if stridx >= k:
    print((res[k]+1))
    exit()
else:
    k = k - stridx

    tmp = k % cycle

    print((res[stridx+tmp]+1))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

dist = [-1 for i in range(n)]
dist[0] = 0

now_town = 0
flg = 0  # ループが生じているかを判定するフラグ
for i in range(k):
    # print(i)
    next_town = a[now_town] - 1
    if dist[next_town] != -1:
        flg = 1
        diff = (i+1) - dist[next_town]
        const = dist[next_town]
        break
    dist[next_town] = i + 1
    now_town = next_town

# print(dist)
# print("diff", diff)
# print("const", const)

if flg == 0:
    print((next_town+1))
elif flg == 1:
    c = (k - const) % diff
    # print("c =", c)
    # print("next_town = ", next_town)
    if c == 0:
        print((next_town+1))
    else:
        now_town = next_town
        # print(now_town)
        for i in range(c):
            next_town = a[now_town] - 1
            now_town = next_town
            # print(now_town)
        print((now_town+1))


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

dist = [-1 for i in range(n)]
dist[0] = 0

now_town = 0
freq = 0
for i in range(k):
    next_town = a[now_town] - 1
    if dist[next_town] != -1:
        freq = (i+1) - dist[next_town]
        const = dist[next_town]
        break
    dist[next_town] = i + 1
    now_town = next_town

if freq == 0:  # ループが発生していない場合
    print((next_town+1))
else:  # ループが発生している場合
    freq_ = (k - const) % freq
    if freq_ == 0:
        print((next_town+1))
    else:
        now_town = next_town
        for i in range(freq_):
            now_town = a[now_town] - 1
        print((now_town+1))
# Original solution
# import numpy as np
# import queue
# import heapq


def main():
    N, K = list(map(int, input().split()))
    As = list(map(int, input().split()))

    f_loop = [1]
    passed = set(f_loop)

    next_idx = 1
    while True:
        next_town = As[next_idx-1]
        if next_town in passed:
            idx_head = f_loop.index(next_town)
            n_loop = len(f_loop) - idx_head
            break

        passed.add(next_town)
        f_loop.append(next_town)
        next_idx = next_town

    if idx_head + n_loop <= K:
        target = idx_head + (K - idx_head - n_loop) % n_loop
    else:
        target = K

    print((f_loop[target]))


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    _, n_teleport = list(map(int, input().split()))
    towns = [0] + list(map(int, input().split()))

    visited_list = []
    visited_set = set()

    next_town = 1
    while 1:
        if next_town in visited_set:
            loop_start_at = visited_list.index(next_town)
            loop_length = len(visited_list) - loop_start_at
            break

        visited_list.append(next_town)
        visited_set.add(next_town)
        next_town = towns[next_town]

    if n_teleport < loop_start_at:
        end_point = n_teleport
    else:
        end_point = loop_start_at + (n_teleport - loop_start_at) % loop_length

    print((visited_list[end_point]))


if __name__ == "__main__":
    main()
# Original solution

n,k=list(map(int,input().split()))
m=list(map(int,input().split()))
a=[]
p=1
while p not in a:
    a.append(p)
    p=m[p-1]

l=a.index(p)

if k<len(a):
    print((a[k]))
else:
    k-=l
    print((a[l+k%(len(a)-l)]))

            
            
        
    

# Optimized solution

n,k=list(map(int,input().split()))
m=list(map(int,input().split()))
a=[]
p=1
vis=[0]*n
while vis[p-1]==0:
    a.append(p)
    vis[p-1]=1
    p=m[p-1]

l=a.index(p)

if k<len(a):
    print((a[k]))
else:
    k-=l
    print((a[l+k%(len(a)-l)]))
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(100000000)

stp = -1 
def main():
    n, k = list(map(int, readline().split()))
    a = [int(i)-1 for i in readline().split()]

    order = []
    ex = set()
    def dfs(node):
        global stp
        order.append(node)
        ex.add(node)
        nx = a[node]
        if nx in ex:
            stp = nx
            return
        dfs(nx)
    dfs(0)

    cnt = order.index(stp)
    if k < cnt:
        print((order[k]+1))
    else:    
        m = len(order[cnt:])
        idx = (k-cnt)%m
        print((order[cnt:][idx]+1))
if __name__ == '__main__':
    main()


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10000000)
s = 0 
def main():
    n, k = list(map(int, readline().split()))
    a = [int(i)-1 for i in readline().split()]
    done = [0 for _ in range(n)] 
    cycle = []
    def dfs(x):
        global s
        if done[x] == 1:
            s = x
            return
        nx = a[x]
        done[x] = 1
        cycle.append(x)
        dfs(nx)

    dfs(0)
    start_idx = cycle.index(s)
    ln = len(cycle)
    if k < ln: 
        print((cycle[k]+1))
    else:
        k -= start_idx
        idx = start_idx+(k%(ln-start_idx))
        print((cycle[idx]+1))
if __name__ == '__main__':
    main()
# Original solution
N,K=list(map(int,input().split()))
P=list(map(int,input().split()))

A={}

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

f=True
U=[]

v=1

k=0

while (v not in set(U)) and (k<=K):
    U.append(v)
    v=A[v]
    k+=1
    
if v not in U:
    print((U[-1]))
else:
    g=U.index(v)
    U=U[g:]
    K-=g

    m=len(U)
    print((U[K%m]))


# Optimized solution
N,K=list(map(int,input().split()))
P=list(map(int,input().split()))

A={k+1:P[k] for k in range(N)}


U=list()
S=set()
x=1
k=0

while (x not in S) and (k<=K):
    U.append(x)
    S.add(x)
    
    x=A[x]
    k+=1

if k>K:
    print((U[-1]))
else:
    d=U.index(x)
    V=U[d:]
    print((V[(K-d)%len(V)]))
# Original solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [0] + [int(x) for x in stdin.readline().rstrip().split()]

m = 0
b = [1]

c = 1
for i in range(n):
    c = a[c]
    if c not in b:
        b.append(c)
        if i == k-1:
            print(c)
            sys.exit()
    else:
        break

idx = b.index(c)
bn = len(b[idx:])
sn = len(b[:idx])

# print(b,idx,sn,bn)
kk = (k-sn) % bn
print((b[idx+kk]))

# Optimized solution
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

n,k = [int(x) for x in stdin.readline().rstrip().split()]
a = [0] + [int(x) for x in stdin.readline().rstrip().split()]

m = 0
b = [1]
visited = [-1 for i in range(n+1)]

c = 1
visited[1] = 1
for i in range(n):
    c = a[c]
    if visited[c] != 1:
        b.append(c)
        visited[c] = 1
        if i == k-1:
            print(c)
            sys.exit()
    else:
        break

idx = b.index(c)
bn = len(b[idx:])
sn = len(b[:idx])

# print(b,idx,sn,bn)
kk = (k-sn) % bn
print((b[idx+kk]))
# Original solution
n,k =list(map(int,input().split()))
a=list(map(int,input().split()))
a = [1] + a
def next(i):
    return a[i]

z = [0] * (n+1)

place = 1
T = 0
interval = 0
if k < n:
    for i in range(k):
        place = next(place)
    print(place);exit()
place = 0
k += 1
for i in range(n):
    place = next(place)
    if z[place] != 0:
        interval = T - z[place]  + 1
        break
    z[1] = 1 
    T += 1
    z[place] = T

m = (k-z[place])%interval
for i in range(m):
    place = next(place)
print(place)


# Optimized solution
import sys
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9 + 7
input = sys.stdin.readline

def inp():
    return int(eval(input()))
def inpl():
    return list(map(int,input().split()))
def inpm():
    return list(map(int,input().split()))

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

a=[1] + inpl()
b=[-1] * (n+1); b[1] = 0
place = 1
turn = 0

while b[a[place]] == -1:
    place = a[place]
    turn += 1
    b[place] = turn
r = (k - turn) % (b[place] - b[a[place]] + 1)

if k < turn:
    print((b.index(k)))
else:
    for _ in range(r):
        place = a[place]
    print(place)
# Original solution
n, k = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])
bit = k.bit_length()
table = [[0]*n for _ in range(bit)]
table[0] = A

for i in range(1, bit):
    for j in range(n):
        table[i][j] = table[i-1][table[i-1][j]]

ans = 0
for i in reversed(list(range(bit))):
    if (1 << i) <= k:
        k -= 1 << i
        ans = table[i][ans]
print((ans+1))

# Optimized solution
n, k = list(map(int, input().split()))
A = list([int(x)-1 for x in input().split()])

history = [-1]*n
cur = 0
for i in range(n):
    if history[cur] != -1:
        init_cnt = history[cur]
        loop_cnt = i - history[cur]
        break
    history[cur] = i
    cur = A[cur]

loop = k if k <= init_cnt else init_cnt+((k-init_cnt) % loop_cnt)
print((history.index(loop)+1))
# Original solution
n,k = (int(a) for a in input().split())
A = list(map(int,input().split()))
t = [1]
x = 1
while len(t) == len(set(t)) :
    t.append(A[x-1])
    x = t[-1] 
l = [i for i , x in enumerate(t) if x == t[-1]]
if k < l[0] :
    print((t[k]))
else :
    k = k-l[0]
    ans = k%(l[1]-l[0])
    print((t[l[0] + ans]))

# Optimized solution
n,k = (int(a) for a in input().split())
A =  [-1] + list(map(int,input().split()))
t = [1]
flag =[0,1] + [0]*(n-1)
x = A[1]
while flag[x] == 0 :
    t.append(x)
    flag[x] = 1
    x = A[x] 

if k <= len(t) -1 :
    print((t[k]))
else :
    l = t[t.index(x):]
    k = k - (len(t) - 1)
    k %= len(l)
    print((l[k-1]))
# Original solution
n,k = list(map(int,input().split()))

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [0]*n
seen[0] = 1
#load = []
res = 0
flag = 0
for i in range(k-1):
    #print(seen)
    #print(nxt)
    if not(nxt in seen):
        seen[i+1] = nxt
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        #print(root)
        #print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)

# Optimized solution
n,k = list(map(int,input().split()))

a  = [0]*n

a = [int(s) for s in input().split()]

nxt = a[0]

seen = [1]
load = set()
load.add(1)
res = 0
flag = 0
for i in range(k-1):
    #print(seen)
    #print(nxt)
    if not(nxt in load):
        seen.append(nxt)
        load.add(nxt)
        nxt = a[nxt-1]
    else:
        root = i - seen.index(nxt) + 1
        #print(root)
        #print(k-1-i)
        zansa = (k-1-i)%root
        res = seen[seen.index(nxt)+zansa]
        flag = 1
        break


if(flag == 1):
    print(res)
else:
    print(nxt)
# Original solution
import sys

sys.setrecursionlimit(10**9)

def dfs(num,lis,count,target):
    visited[num]=count
    n_city=A[num]
    if visited[n_city]!=-1:
        lis.append(num)
        if target<=count:
              print((lis[target]+1))
        else:
            rep=len(lis)-visited[n_city]
            target-=visited[n_city]
            amari=target%rep
            temp=lis[visited[n_city]:]
            print((temp[amari]+1))
        exit()
    else:
        dfs(n_city,lis+[num],count+1,target)
    

N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
A=list([x-1 for x in A])

visited=[-1]*N

dfs(0,[],0,K)

# Optimized solution
import sys

sys.setrecursionlimit(10**9)

def dfs(num,lis,count,target):
    visited[num]=count
    n_city=A[num]
    if visited[n_city]!=-1:
        lis.append(num)
        if target<=count:
              print((lis[target]+1))
        else:
            rep=len(lis)-visited[n_city]
            target-=visited[n_city]
            amari=target%rep
            temp=lis[visited[n_city]:]
            print((temp[amari]+1))
        exit()
    else:
        lis.append(num)
        dfs(n_city,lis,count+1,target)
    

N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
A=list([x-1 for x in A])

visited=[-1]*N

dfs(0,[],0,K)
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

s = [-1] * N
i = 0
cur = 0
m = 0
while True:
    if s[cur] != -1 and m != 1:
        m = i - s[cur]
        K = i + (K - i) % m
    else:
        s[cur] = i
    if i == K:
        print((cur+1))
        exit()
    i += 1
    cur = A[cur] - 1


# Optimized solution
N, K = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
 
s = [0] * (N+1)
p = 1
h = []
for i in range(K):
    h.append(p)
    s[p] = 1
    p = A[p]

    if s[p] != 0:
        break

if i == K-1:
    print(p)
    exit()
else: 
    period = len(h) - h.index(p)
    rem = (K - h.index(p)) % period
    print((h[h.index(p):][rem]))