Problem:
 Score: 400 points    Problem Statement  The Kingdom of Takahashi has N towns, numbered 1 through N .  There is one teleporter in each town. The teleporter in Town i  (1 \leq i \leq N) sends you to Town A_i .  Takahashi, the king, loves the positive integer K . The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.  Help the king by writing a program that answers this question.      Constraints   2 \leq N \leq 2 \times 10^5  1 \leq A_i \leq N  1 \leq K \leq 10^{18}         Input  Input is given from Standard Input in the following format:  N  K  A_1  A_2  \dots  A_N       Output  Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.        Sample Input 1 4 5
3 2 4 1      Sample Output 1 4  If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4 .       Sample Input 2 6 727202214173249351
6 5 2 5 3 2      Sample Output 2 2

Solution (Python):
n, k = [int(i) for i in input().split()]
al = [int(i) for i in input().split()]

index = 0
keiro = []
s_keiro = set()
loop_f = False

for i in range(k):
  index = al[index] - 1
  if index in s_keiro:
    start = keiro.index(index)
    mod = i - start
    loop_f = True
    break
  s_keiro.add(index)
  keiro.append(index)

if loop_f:
  pos = (k-start-1) % mod
  if mod == 1:
    print(keiro[start]+1)
  else:
    print(keiro[start + pos]+1)
else:
  print(keiro[-1] + 1)

Solution (Python):
N, K = map(int, input().split())
M = list(map(lambda x: int(x)-1, input().split()))
s = 0
order = [s]
sets = set(order)
while True:
  if M[s] in sets:
    
    if len(order) > K:
      print(order[K]+1)
    else:
      K -= order.index(M[s])
      order = order[order.index(M[s]):]
      print(order[(K+1)%len(order)-1]+1)
    break
  order.append(M[s])
  sets.add(M[s])
  s = M[s]

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a = [0] + a
c = [1]
x = True
y = [1, 1] + [0]*(n-1)

while x:
    c.append(a[c[-1]])
    y[c[-1]] += 1
    if y[c[-1]] == 2:
        x = False

t = c.index(c[-1])
if k <= t:
    ans = c[k]
else:
    ans = c[t + ((k-t) % (len(c)-1-t))]

print(ans)

Solution (Python):
N,K=map(int, input().split())
A=list(map(int, input().split()))

loopcount=0
tmpPoint=1
vis={1:0}
for i in range(N):
    Next=A[tmpPoint-1]
    if Next in vis:
        startat=i
        
        
        
        
        
        loopcount=i - vis[Next]+1
        break
    vis[Next]=i+1
    tmpPoint=Next

if loopcount==0:
    print(tmpPoint)
    exit()
if startat+loopcount < K:
    nokori=(K-startat) % loopcount
    K=startat+nokori

tmpPoint=1
for i in range(K):
    Next=A[tmpPoint-1]
    tmpPoint=Next
print(tmpPoint)

Solution (Python):

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

now = 1
visited = [-1]*N
visited[0] = 0
for i in range(N+1):
    new = A[now-1]
    if visited[new-1] == -1:
        visited[new-1] = i+1
        now = new
    else:
        f = visited[new-1]
        b = i+1
        break
        
if K <= N:
    now = 1
    for i in range(K):
        new = A[now-1]
        now = new
    print(now)
else:
    k = f + ((K-f)%(b-f))
    now = 1
    for i in range(k):
        new = A[now-1]
        now = new
    print(now)

Solution (Python):
def m():
    n, k = map(int, input().split())
    a = [0] + list(map(int, input().split()))
    visited = [[] for _ in range(n + 1)]
    visited[1].append(0)
    cur = 1
    i = 1
    while True:
        cur = a[cur]
        visited[cur].append(i)
        i += 1
        if len(visited[cur]) >= 2:
            break

    if k <= visited[cur][0]:
        now = 1
        for p in range(1, k+1):
            now = a[now]
        return now
    else:
        tmp = k - visited[cur][0]
        MOD = visited[cur][1] - visited[cur][0]
        count = tmp % MOD
        now = 1
        for p in range(1, count+1+visited[cur][0]):
            now = a[now]
        return now


print(m())

Solution (Python):
N, K = list(map(int,input().split()))
move_list = list(map(int,input().split()))
 
  
test = 1
find_f = {}
for i in range(N):
  find_f[i+1]=[]
 
find_f[1].append(0)
for i in range(K):
  test = move_list[test-1]
  find_f[test].append(i+1)
  if len(find_f[test]) == 2 and find_f[test][1]-find_f[test][0]<=1000000:
    state ,f = test, find_f[test]
    break
try:
  K = K-f[0]
  f = f[1]-f[0]
  remain = K%f
  for i in range(remain):
    state = move_list[state-1]
 
  print(state)
except:
  print(test)

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

visited = [0] * (n + 1)
now = 1
check = 2 * n
loop_start = 1
while check > 0:
    if visited[now - 1] == 1:
        loop_start = now
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1

visited = [0] * (n + 1)
now = loop_start
check = n
loop = 0
while check > 0:
    if visited[now - 1] == 1:
        break
    else:
        visited[now - 1] += 1
        now = a[now - 1]
        check -= 1
        loop += 1

now = 1
while k > 0:
    if now == loop_start:
        k %= loop
        if k == 0:
            break
    now = a[now - 1]
    k -= 1

print(now)

Solution (Python):
N, K = map(int, input().split())
list_A = list(map(int, input().split()))

l = [1]
s = {1}

while True:
    next = list_A[l[-1] - 1]

    if not next in s:
        l.append(next)
        s.add(next)

    else:
        start = l.index(next)
        roop = len(l) - start
        break

if len(l) == 1:
    print(1)
    exit()

elif len(l) > K:
    print(l[K])
    exit()

else:
    roop_remainder = (K + 1 - start) % roop
    if roop_remainder == 0:
        print(l[-1])
    else:
        print(l[start + roop_remainder -1])

Solution (Python):
nk = input().split()
n , k = int(nk[0]), int(nk[1])
a = [int(i) for i in input().split()]

t = 1
tl = [1]
ts = {1}


for i in range(len(a)):
    
    t = a[t-1]
    if t in ts:
        break
    else:
        tl.append(t)
        ts.add(t)

tl2 = tl[tl.index(t):len(tl)]
         



ans = 0

if k < len(tl):
    ans = tl[k]
else:
    k = k - len(tl)
    j = k % len(tl2)
    ans = tl2[j]

print(ans)

Solution (Python):
def L_search(A,key):
    for i in range(len(A)):
        if A[i] == key:
            return i

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

    Loop_set,Loop_list = set([1]),[1]
    pt = 1 
    while True:
        pt = A[pt-1]
        if pt not in Loop_set:
            Loop_set.add(pt)
            Loop_list.append(pt)
        else:
            break

    gosa = L_search(Loop_list,pt)
    shuki = len(Loop_list) - gosa
    if K < gosa:
        print(Loop_list[K])
    else:
        K -= gosa
        K %= shuki
        K += gosa
        print(Loop_list[K])

if __name__ == "__main__":
    main()

Solution (Python):

def q4():
    N, K = [int(i) for i in input().split(" ")]
    A = [-1] + [int(i) for i in input().split(" ")]

    arrived = []
    arrived_set = set()
    current = 1
    loop_start_station = -1
    while True:
        arrived.append(current)
        arrived_set.add(current)
        current = A[current]
        if current in arrived_set:
            loop_start_station = arrived.index(current)
            break

    if K <= loop_start_station:
        print(arrived[K])
        return
    K = K - loop_start_station
    arrived = arrived[loop_start_station:]
    K = K % len(arrived)
    print(arrived[K])
        

q4()

Solution (Python):


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

def compute(city, K):
    for i in range(K):
        
        city = As[city-1]
    print(city)

cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}

while not cities_visited[city - 1]:
    
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1

loop_start = city_visited_time[city-1]
loop_end = i


div = (K - loop_start) // (loop_end - loop_start)
if K - loop_start < 0 or div == 0:
    compute(1, K)
else:
    compute(city, K - loop_start - div * (loop_end - loop_start))

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
  a[i]-=1
visit=[False for i in range(n)]
visit[0]=True
looplist=[False for i in range(n)]
mark=-1
i=0
num=0
loop=0
count=0
while True:
  num+=1
  i=a[i]
  if mark<0 and visit[i]:
    mark=a[i]
  else:
    visit[i]=True
  if mark>-1 and looplist[i]:
    count=num-loop*2
    break
  elif mark>-1:
    loop+=1
    looplist[i]=True
if k < count:
  pass
else:
  k-=count
  k%=loop
  k+=count
ans=0
for i in range(k):
  ans=a[ans]
print(ans+1)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import sys
sys.setrecursionlimit(4100000)

n,m= map(int,readline().split())
a = list(map(int,readline().split()))

if a[0] == 1:
  print(1)
  exit()
  
d = set([0])
def dfs(now,cc,d):
    if cc == m:
      return now
    else:
      next = a[now]
      next -= 1
      if not next in d:
        d.add(next)
        return dfs(next,cc+1,d)
      else:
        d = list(d)
        for i,di in enumerate(d[::-1]):
          if di == next:
            return (i+1,cc,now)

def f(now,cc):
    global mar
    if cc == mar:
      return now
    else:
      next = a[now]
      next -= 1
      return f(next,cc+1)
    
res = dfs(0,0,d)
if type(res) == int:
  print(res+1)
else:
  m -= res[1]
  mar = m % res[0]
  print(f(res[2],0)+1)

Solution (Python):
N , K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

T = {0: 0}

for i in range(N):
    T.update({i + 1: A[i]})

i = 1

MOD_ = 0

VISIT = [0] * (N+1)

for j in range(K):
    i = T[i]

    if VISIT[i] != 0:
        K0 = VISIT[i] 
        MOD_ = j+1 - VISIT[i] 
        break

    VISIT[i] = j+1


if MOD_ !=0:
    i = T[i]
    if ((K - K0 - 1) % MOD_ ) != 0:
        for j in range((K-K0-1) % MOD_ ):
            i = T[i]


print (i)

Solution (Python):
l = [int(x) for x in input().split()]
n = l[0]
k = l[1]
pos = 1

go = [int(x) for x in input().split()]
ref = {}



i = 0

while i < k:
    pos = go[pos-1]
    i = i+1

    if pos in ref:
        if ref[pos][1] == -1:
            ref[pos][1] = i
        else:
            temp = ref[pos][1] - ref[pos][0]
            tempy = ((k-i)//temp)*temp
            i = i + tempy

    else:
        ref[pos] = [i, -1]

print(pos)

Solution (Python):
N,K=list(map(int,input().split()))
s=[0]*N
src=list(map(int,input().split()))
x=0
y=0
z=-1
ind=1
for i in range(K):
  q=src[ind-1]
  s[ind-1]+=1
  if s[ind-1]==1:
    x+=1
    ind=q
  elif s[ind-1]==2:
    y+=1
    ind=q
  elif s[ind-1]==3:
    z+=1
    break
if z==-1 and y==0:
  print(ind)
  exit()
elif z==-1 and y!=0:
  print(ind)
  exit()
else:
  p=x-y
  K-=p
  K%=y
  if K==0:
    print(ind)
    exit()
  for k in range(K):
    q=src[ind-1]
    ind=q
  print(q)

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))

d = {}
s_list = []
count = 0
idx = 1
while count < N:
    idx = a[idx-1]
    if idx in d.keys():
        break
    else:
        d[idx] = count
    s_list.append(idx)
    count += 1
loop_idx = len(s_list) - d[idx]

if d[idx] > K:
    print(s_list[K-1])
elif loop_idx != 0:
    t_idx = (K - d[idx])% loop_idx
    print(s_list[d[idx]:][t_idx-1])
else:
    t_idx = K % N
    print(s_list[t_idx-1])

Solution (Python):
def main():
    N, K = map(int, input().split())
    A    = [int(i)-1 for i in input().split()]

    visited_town = []
    current_town = 0
    loop_flg     = False
    visited_town.append(current_town)
    set_visted_town = set(visited_town)
    for i in range(K):
        current_town = A[current_town]
        if current_town in set_visted_town:
            visited_town.append(current_town)
            loop_flg = True
            break
        visited_town.append(current_town)
        set_visted_town.add(current_town)


    if loop_flg:
        loop_start = visited_town.index(current_town)
        loop_end   = len(visited_town) - 1
        loop_length = loop_end - loop_start
        last_index = (K-loop_start)%loop_length
        current_town = visited_town[loop_start+last_index]

    print(current_town+1)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
*A, = map(int, input().split())

n = 0
loc = 1
flag_visit = [-1]*N
flag_when = [-1]*N
flag_visit[0] *= -1
flag_when[0] = 0
move_hist = [1, ]
while 1:
    loc = A[loc-1]
    move_hist.append(loc)
    n += 1
    if flag_visit[loc-1] == -1:
        flag_visit[loc-1] *= -1
        flag_when[loc-1] = n
    else:
        m = flag_when[loc-1]
        break
if K > m:
    print(move_hist[m+(K-m) % (n-m)])
else:
    print(move_hist[K])

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

result = 1
L = 2
current = A[0]
visited = [0] * N
visited_loop_cnt = 1
flag = True

while L < K:
    if visited[current - 1] == 3:
        J = K - L
        J %= visited_loop_cnt
        K = L + J
    elif visited[current - 1] == 2 and flag:
        visited[current - 1] += 1
        visited_loop_cnt = 1
        flag = False
    else:
        visited[current - 1] += 1
        visited_loop_cnt += 1
    current = A[current - 1]
    L += 1

print(A[current - 1])

Solution (Python):
def main():
    n, k = [int(x) for x in input().split()]
    teleporter = [int(x) - 1 for x in input().split()]
    route = []
    route_append = route.append
    seen = [False] * n
    traveled, now_i = 0, 0

    while True:
        if seen[now_i]:
            
            break
        route_append(now_i)
        seen[now_i] = True
        now_i = teleporter[now_i]
        traveled += 1

    
    
    intro_length = route.index(now_i)
    loop_length = len(route) - intro_length
    if k >= intro_length:
        arriving_i = intro_length + (k - intro_length) % loop_length
    else:
        
        arriving_i = k
    return route[arriving_i] + 1


if __name__ == '__main__':
    print(main())

Solution (Python):
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 = 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()

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
adj_list=[0]*N
for i in range(N):
    adj_list[i]=(A[i])
k=0
node = 1
c=[0]*N
count=1
flag=0
ROOP=[]
roopstart=0
while k<K:
    k+=1
    c[node-1]+=1
    node = adj_list[node-1]
    if c[node-1] ==1:
        if flag ==0:
            roopstart = k
            flag=1
        ROOP.append(node)
    if c[node-1] ==2:
        break
l = len(ROOP)
if l != 0:
    num = (K-l-roopstart) % (l)
    print(ROOP[num])
else:
    print(node)

Solution (Python):

import sys


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]

    done = [int()]*(N)
    done_k = [0] * N


    i = 0
    q = 1
    while done_k[q-1] == 0:
        done[i] = q
        done_k[q-1] = 1
        i+=1
        q = A[q-1]

    f_q = done.index(q)
    r = i - f_q

    if K <= f_q:
        t = K
    else:
        k = (K - f_q)%r
        t = k + f_q

    print(done[t])        

    
    
    
    



if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split(' '))
a=list(map(int,input().split(' ')))
loc=1
begin=0
end=0
d={1:0}
for i in range(n+1):
    loc=a[loc-1]
    if loc in d.keys():
        begin=d[loc]
        end=i+1
        break
    d[loc]=i+1
if k<=end:
    end=0
if end==begin+1:
    k=begin
    end=0
if end != 0:
    x=(k-begin+1)%(end-begin)
    if x==0:
        x=end-begin
    k=x+begin-1
loc=1
for _ in range(k):
    loc=a[loc-1]
print(loc)

Solution (Python):
N, K = map(int, input().split())
K += 1
A = [int(i) for i in input().split()]
s = []
i = 0
length = 0

while True:
    if not A[i]:
        start = i + 1
        break
    s.append(i + 1)
    dest = A[i]
    A[i] = None
    i = dest - 1
    length += 1

if K == 2:
    print(s[1])
elif start == s[0]:
    amari = K % length
    if amari == 0:
        print(s[-1])
    else:
        print(s[amari - 1])
else:
    idx = s.index(start)
    s2 = s[idx:]
    length2 = len(s2)
    tmp = length - length2
    if K < tmp:
        print(s[K-1])
    else:
        K -= tmp
        amari = K % length2

        if amari == 0:
            print(s2[-1])
        else:
            print(s2[amari - 1])

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    dict[i]=a[i]-1 
tmp=0
num2city={}
city2num={}
city2num[0]=0
num2city[0]=0
alre=[-1]*n
alre[0]=1
for j in range(1,k+1):
    if tmp==dict[tmp]:
        break
    tmp=dict[tmp]
    if alre[tmp]==-1:
        alre[tmp]=1
        num2city[j]=tmp
        city2num[tmp]=j
    else:
        mae=city2num[tmp]
        term=j-mae 
        res=k-j
        r=res%term
  
        tmp=num2city[mae+r]
 
        break
   
    


print(tmp+1)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
x = 1
step = 0
count = [0]*N
count[0] += 1
memo = [-1]*N
memo[0] = 0
M = K
flag = 0
for i in range(M):
    x = A[x-1]
    count[x-1] += 1
    step += 1
    K -= 1
    if memo[x-1] != -1:
        s = memo[x-1]
        t = step-s

        flag = 1
        break
    memo[x-1] = step

if flag == 0:
    print(x)
else:
    if t>= 1:
        K %= t
    else:
        K = 1
    for i in range(K):
        x = A[x-1]
    print(x)

Solution (Python):
import sys











stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18


n,k = map(int, stdin.readline().rstrip().split())
A = list(map(int, stdin.readline().rstrip().split()))






visited = [0]*n
visited[0] = 1
next = A[0] - 1
loop = 0
v = 0
for i in range(n):
    if visited[next] != 0:
        loop = i-visited[next]+2
        v = i
        break
    else:
        visited[next]= i+2
        next = A[next] - 1

road =  v+1 - loop

if k <= road:
    print(visited.index(k+1) + 1)
    exit()
k = k - road
k = k%loop
print(visited.index(k+1+road) +1)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
dict={}
for i in range(n):
    dict[i]=a[i]-1 
tmp=0
num2city={}
city2num={}
city2num[0]=0
num2city[0]=0
alre=[-1]*n
alre[0]=1
for j in range(1,k+1):
    if tmp==dict[tmp]:
        break
    tmp=dict[tmp]
    if alre[tmp]==-1:
        alre[tmp]=1
        num2city[j]=tmp
        city2num[tmp]=j
    else:
        mae=city2num[tmp]
        term=j-mae 
        res=k-j
        r=res%term
  
        tmp=num2city[mae+r]
 
        break
   
    


print(tmp+1)

Solution (Python):








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

Arrival = [0]*N
NowTown = 0
Arrival[NowTown] = 1
ArrivalStep = [0]*N

Step = 0

if K <= N:
    for i in range(K):
        NowTown = A[NowTown]
    print(NowTown+1)
    exit()

while True:
    Step += 1
    NowTown = A[NowTown]
    if Arrival[NowTown] == 1:
        break
    ArrivalStep[NowTown] = Step
    Arrival[NowTown] = 1

RoopNumper = Step - ArrivalStep[NowTown]

Rest = K-Step

Rest %= RoopNumper

if Rest == 0:
    print(NowTown+1)
else:
    for i in range(Rest):
        NowTown = A[NowTown]
    print(NowTown+1)

Solution (Python):
import sys

n, k = map(int, input().split())
a = list(map(lambda x: int(x)-1, input().split()))

is_visited = [0] * n
visited = list()
i = 0
cnt = 0
while True:
    if cnt == k:
        print(i+1)
        sys.exit()
    if is_visited[i]:
        break
    is_visited[i] = 1
    visited.append(i)
    i = a[i]
    cnt += 1

si = visited.index(i)
loop = visited[si:]
n_loop = len(loop)
left_k = k - cnt
print(loop[left_k % n_loop] + 1)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
check = [-1]*N
cnt = 0
check[0] = cnt
index = A[0]

while True:
    if check[index-1] != -1:
        break
    cnt += 1
    if K-1 == cnt:
        print(A[index-1])
        exit()
    check[index-1] = cnt
    index = A[index-1]

length = cnt - check[index-1] + 1
K -= (len(A) - check.count(-1) -length)
l = []
for _ in range(length):
    l.append(A[index-1])
    index = A[index-1]
ans = K % len(l)
print(l[ans-1])

Solution (Python):
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
 
position = 1
exp1 = [0] * n
exp1[0] = 1
exp = [1]
count = 0
 
if n >= k:
  for i in range(k):
    position = a[position - 1]
  print(position)
else:
  for i in range(k):
    position = a[position - 1]
    count += 1
    if exp1[position - 1] == 0:
      exp.append(position)
      exp1[position - 1] += 1
    else:
      break
  init = exp.index(position)
  res = (k - init) % (count - init)
  print(exp[init + res])

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

next = 1
exist=[0]*n
ans=[]
ans.append(1)


for i in range(n):
    if i+1==k:
        print(a[next-1])
        exit()
    if exist[a[next-1]-1]!=0:
        flag=a[next-1]
        break
    else:
        ans.append(a[next-1])
        exist[next-1]=1
        next=a[next-1]

aa=ans.index(flag)
cycle = len(ans)-aa
po = (k-aa)%cycle

print(ans[po+aa])

Solution (Python):
n, k = map(int, input().split())
A = list(map(int, input().split()))
d = {}
for i, a in enumerate(A):
    d[i + 1] = int(a)

l = 1
nxt = d[1]
is_visit = [False for _ in range(n + 1)]
is_visit[1] = 0
while is_visit[nxt] is False:
    is_visit[nxt] = l
    l += 1
    nxt = d[nxt]
c = is_visit[nxt]
lc = l - is_visit[nxt]

if k <= c:
    r = 1
    for _ in range(k):
        r = d[r]
    print(r)
else:
    r = nxt
    m = (k - c) % lc
    for _ in range(m):
        r = d[nxt]
        nxt = d[nxt]
    print(r)

Solution (Python):
n, k=input().split()
a=input().split()
a=list(map(int,a))
a=list(map(lambda x: x-1,a))
n=int(n)
k=int(k)
cycle=0
step=0
now=0
ini=0
now2=n
count=[0]*n
count[0]=1

while k>0:
  now=a[now]
  k+=-1
  if count[now]==1:
    ini=now
    while now2!=ini:
      if cycle==0:
        now2=ini
      now2=a[now2]
      cycle+=1
    else:
      k=k%cycle
      for j in range(k):
        now=a[now]
      break
  count[now]=1



       

print(now+1)

Solution (Python):
n, k = map( int, input().split() )
a = [ int( i ) for i in input().split() ]

leg = [ 0 ] * n
leg[ 0 ] = 0

p = 0
city_old = 1
for x in range( k ):
    city_new = a[ city_old-1 ]
    if leg[ city_new-1 ] == 0 and city_new != 1:
        leg[ city_new-1 ] = x + 1
    else:
        loop = x + 1 - leg[ city_new-1 ]
        modk = ( k - ( x + 1 ) ) % loop

        break
    city_old = city_new
    p += 1

if p == k:
    print( city_old )
    exit()


city_old = city_new
for x in range( modk ):
    city_new = a[ city_old-1 ]
    city_old = city_new

print( city_old )

Solution (Python):
n, k = map(int,input().split())
a = list(map(int, input().split()))
c = 1
s = 0
b = [0]*(n+1)
b[1] = 1
d = [0]*(n+1)
d[1] = 0

for i in range(n*10):
    c = a[c-1]
    b[c] += 1
    if b[c] == 2:
        s = i+1 - d[c]
        f = d[c]
        break
    d[c] = i+1

e = c

h = 1
if k < f:
    for i in range(k):
        h = a[h-1]
    print(h)
else:
    S = (k-f)%s
    for i in range(S):
        e = a[e-1]
        
    
    
    
    print(e)

Solution (Python):
import sys
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.insert(A, 0, 0)

flg = [False for _ in range(N + 1)]
town = []
i = 1
cnt = 0
for _ in range(N):
    town.append(i)
    flg[i] = True
    i = A[i]

if len(town) >= K:
    print(town[K])
else:
    s = i
    to_s = town.index(s)
    loop = len(town) - to_s
    print(town[(K - to_s) % loop + to_s])

Solution (Python):
N, K = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
 
import numpy as np
 
 
 
Nstock = np.zeros(N)
 
cpos = 1
Nstock[cpos - 1] = 1
stock = [cpos]
loop = []
 
idx = 0
for i in range(K):
    cpos = A[cpos-1]
    if Nstock[cpos - 1]  == 1:
        idx = stock.index(cpos)
        loop = stock[idx:]
        break
    Nstock[cpos - 1] = 1
    stock.append(cpos)
    
if loop != []:
    v = (K - idx) 
    a = v %len(loop)
    print(loop[a])
else:
    print(stock[K])

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,K = LI()
    A = [0] + LI()

    his = [0] * (N+1)
    s = 1
    cnt = 1
    his[1] = 1
    while his[A[s]] == 0:
        s = A[s]
        cnt += 1
        his[s] = cnt


    if K >= cnt:
        s = A[s]
        cnt -= his[s] - 1

        k = (K - his[s] + 1) % cnt
    else:
        k = K
        s = 1
    for i in range(k):
        s = A[s]
    print(s)



if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

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

town_dict = dict()
for a,town in enumerate(town_list, 1):
    town_dict[a] = town


if k <= n:
    now_place = 1
    for _ in range(k):
        now_place = town_dict[now_place]

    print(now_place)

if k > n:
    now_place = 1
    visit_town = dict()
    visit_town[1] = 0
    tmp_last_visit = -1
    tmp_visit = -1
    for j in range(n):
        now_place = town_dict[now_place]

        if now_place in visit_town:
            tmp_last_visit = visit_town[now_place]
            tmp_visit = j+1
            break

        visit_town[now_place] = j+1

    if tmp_last_visit == -1:
        print(now_place)
    else:
        ans = (k-tmp_visit)%(tmp_visit-tmp_last_visit)
        
        for _ in range(ans):
            now_place = town_dict[now_place]

        print(now_place)

Solution (Python):
n, k = map(int, input().split())
A = [0] + list(map(int, input().split()))

here = 1
log = [1]
loop = []
num_log = [0] * (n + 1)
num_log[1] = 1
for i in range(k):
    here = A[here]
    if num_log[here] == 1:
        pre_loop = log.index(here)
        k = k - pre_loop
        loop = log[pre_loop:]
        here = loop[k % len(loop)]
        break
    else:
        num_log[here] = 1
    log += [here]
print(here)

Solution (Python):
import sys
input = sys.stdin.readline

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

for i in range(n):
    a[i] -= 1

used = [-1]*n

now = 0
tmp = 0
while True:
    used[now] = tmp
    now = a[now]
    tmp += 1
    if tmp == k:
        print(now+1)
        exit()    
    if used[now] != -1:
        break

k -= tmp
tank = []
p = now
while True:
    tank.append(p)
    p = a[p]
    if p == now:
        break

print(tank[k%len(tank)]+1)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
n,k=map(int,input().split())
a=list(map(int,input().split()))
seen=[False]*n
l=[1]
now=1
while 1:
    now=a[l[-1]-1]
    l.append(now)
    if seen[now-1]:
        break
    seen[now-1]=True
now=l[-1]
until=l.index(now)
if until>k:
    print(l[k])
    sys.exit()
k-=until
loop=len(l[until:-1])

k%=loop
print(l[until+k])

Solution (Python):
n, k = map(int, input().split())

li = [0]
li.extend(list(map(int, input().split())))
tuita = [0]*(n+1)
cnt = 0
town = 1

lastTown = 1
while True:
  town = li[town]
  cnt += 1
  if tuita[town] == 0:
    tuita[town] = cnt
  else:
    if k < tuita[town]:
      lastTown = 1
      times = k
    elif k == tuita[town]:
      lastTown = town
      times = 0
    else:
      lastTown = town
      times = (k - tuita[town]) % (cnt - tuita[town])
    break

town = lastTown
while times > 0:
  if times <= 0:
    break
  times -= 1
  town = li[town]
print(town)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
x = [1]
y = [False]*n
i = 0
s = 0
t = 0
xa = x.append
while True:
  if y[i] == True:
    for j in range(len(x)):
      if i == x[j]-1:
        s = j
        t = len(x)-j-1
        break
    break
  else:
    y[i] = True
    xa(a[i])
    i = a[i]-1
if k < s:
  print(x[k])
  exit()
x = x[s:-1]
print(x[(k-s)%t])

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = []
check = {}
key = -1
p = 0
c = 0
t = 0
while True:
    ans.append(p)
    check[p] = 0
    p = a[p]-1
    c += 1
    if c == k:
        print(p+1)
        t = 1
        break
    if p in check:
        key = ans.index(p)
        break
if t == 0:
    if len(ans) == 1:
        print(a[0])
    else:
        r = ans[key:]
        mod = (k-key)%len(r)
        if mod == 0:
            print(r[0]+1)
        else:
            print(r[mod]+1)

Solution (Python):
N, M = map(int, input().split())
P = [i -1 for i in map(int,input().split())]
ss = set([])
point = 0
for i in range(N):
    if point in ss:
        break
    ss.add(point)
    point = P[point]
S = [0]
points = 0
for i in range(len(ss)-1):
    points = P[points]
    S.append(points)
loop = S[S.index(point):]
ll = len(S)
l = len(loop)
if ll > M:
    print(S[M]+1)
elif ll == M:
    print(point+1)
else:
    print(loop[(M-ll)%l]+1)

Solution (Python):
import sys

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

i = 1
l = [1] * N
s = set([1])
n = 1
while True:
    n = A[n-1]
    if n in s:
        l = l[:i]
        mod = i - l.index(n)
        init_len = l.index(n)
        break
    else:
        l[i] = n
        s.add(n)
        i += 1

if K < init_len:
    print(l[K])
else:
    rest = ((K-init_len) % mod)
    print(l[init_len+rest])

Solution (Python):
[n,k]=list(map(int,input().split()))
alist=list(map(int,input().split()))


footprint=[0]*n

omoide=[]
now=0
counter=1
while True:
    if footprint[now]!=0:
        break
    footprint[now]=counter
    omoide.append(now)
    goto=alist[now]-1
    now=goto
    counter+=1

joso=footprint[now]-1
shuki=counter-footprint[now]

if k>joso:
    k-=joso
    k=k%shuki

    ans=omoide[joso+k]+1

else:
    ans=omoide[k]+1
    
print(ans)

Solution (Python):
from time import time
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [-1 for i in range(N)]

visited = []
now = 0
loopstarts = None
looplength = None
t0 = time()

for i in range(N):
    if L[now] != -1:
        break
    else:
        L[now] = i
        visited.append(now)
        now = A[now]-1

loopstarts = visited.index(now)
looplength = len(visited)-loopstarts

if K < loopstarts:
    print(visited[K]+1)
else:
    print(visited[loopstarts+(K-loopstarts)%looplength]+1)

Solution (Python):
n,k=map(int,input().split())
mati=list(map(int,input().split()))
count=0
i=mati[0]
matidic={}
frag=1
while frag:
    count+=1

    if i in matidic:
        frag=0
        bef=matidic[i]
        roop=count-bef

    else:
        matidic[i]=count
    i = mati[i - 1]



if bef<k:
    ans=[i for i,j in matidic.items() if j==(k-bef)%roop+bef]
else:
    ans=[i for i,j in matidic.items() if j==k]
print(ans[0])