CodeEff/ECCO huggingface dataset:
# Original solution
a,b,c=list(map(int,input().split()))
ans=0
poison=False
while(True):
    if(poison):
        if(b>0):
            b-=1
            ans+=1
        elif(a>0):
            a-=1
        else:
            break
        poison=False
    else:
        if(c>0):
            c-=1
            ans+=1
            poison=True
        elif(b>0):
            b-=1
            ans+=1
        elif(a>0):
            a-=1
        else:
            break
print(ans)

# Optimized solution
a,b,c=list(map(int,input().split()))
if(c<=a+b):
	print((c+b))
else:
	print(((a+b)+b+1))
# Original solution
import sys
input=sys.stdin.readline

a,b,c = list(map(int, input().split()))
hp = 2
ans = 0
while True:
    if a == 0 and b == 0 and c == 0:
        break
    if hp == 1 and a == 0 and b == 0:
        break
    if hp > 1 and c > 0:
        ans += 1
        c -= 1
        hp -= 1
    if hp < 2 or c == 0:
        if a > 0:
            a -= 1
            hp = max(2, hp+1)
        elif b > 0:
            b -= 1
            hp = max(2, hp+1)
            ans += 1

print(ans)


# Optimized solution
import sys
input=sys.stdin.readline

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

if a >= c-1 or b >= c-1 or a+b >= c-1:
    print((b+c))
else:
    print((b+(a+b)+1))
# Original solution
a,b,c=list(map(int, input().split()))
count=0

while(True):
    if c>0:
        if b>0:
            c-=1
            b-=1
            count+=2
        else:
            if a>0:
                c-=1
                a-=1
                count+=1
            else:
                c-=1
                count+=1
                break
    else:
        count+=b
        break
print(count)

# Optimized solution
a,b,c=list(map(int, input().split()))
count=0

if c>=b:
    count+=2*b
    c=c-b
    if c>a:
        count+=a
        c=c-a
        if c>0:
            count+=1
    else:
        count+=c
else:
    count+=c+b

print(count)
# Original solution
A, B, C = list(map(int, input().split()))
ans = 0
c = C
for i in range(C) :
    if c > 0 and B > 0 :
        ans += 2
        c -= 1
        B -= 1
    elif c > 0 and A > 0 :
        ans += 1
        c -= 1
        A -= 1
if c > 0 :
    ans += 1
print((ans + B))


# Optimized solution
A, B, C = list(map(int, input().split()))
ans = 0
if B >= C :
    print((B + C))
    exit()
ans = 2 * B
C -= B
if A >= C :
    print((ans + C))
    exit()
print((ans + A + 1))
# Original solution
A,B,C=(int(i) for i in input().split())
counter=0

while C>0:
  C-=1
  counter+=1
  if B>0:
    B-=1
    counter+=1
  elif A>0:
    A-=1
  else:
    break
    
if B>0:
  counter+=B
  
print(counter)


# Optimized solution
A,B,C=(int(i) for i in input().split())
counter=0

#BとCから
if C>=B:
  counter+=B*2
  C=C-B
  B=0
  if C>A:
    counter+=A+1
  else:
    counter+=C
  
else:
  counter+=C*2
  B-=C
  C=0
  counter+=B
  
print(counter)
# Original solution
A,B,C = list(map(int,input().split()))
can = min(A+B,C)
eat = 0
for i in range(can):
    C -= 1
    eat += 1
    if A > 0:
        A -= 1
    else:
        eat += 1
        B -= 1

if C > 0:
    eat += 1

print((eat+B))

# Optimized solution
A,B,C = list(map(int,input().split()))
can = min(A+B,C)
eat = 0
C -= can
eat += can*2-A
B = A+B -can

if C > 0:
    eat += 1

print((eat+B))
# Original solution
a,b,c = (int(i) for i in input().split())  
if a + b >= c:
  print((b + c))
else:
  print((a + b + b + 1))

# Optimized solution
a, b, c = (int(i) for i in input().split())  
if a + b >= c:
  print((b + c))
else:
  print((b + a + b + 1))
# Original solution
A, B, C = list(map(int, input().split()))
jyotai = 0
ans = 0
while True:
    if A == 0 and B == 0 and jyotai == 1:
        break
    if B == 0 and C == 0:
        break

    if jyotai == 0:
        if C != 0:
            ans += 1
            C -= 1
            jyotai += 1
        else:
            ans += 1
            B -= 1
            jyotai -= 1

    else:
        if B != 0:
            ans += 1
            B -= 1
        else:
            A -= 1
        jyotai -= 1

print(ans)

# Optimized solution
A, B, C = list(map(int, input().split()))
ans = 2 * B
C -= B
if A >= C:
    ans += C
else:
    ans += A + 1

print(ans)
# Original solution
# https://atcoder.jp/contests/agc030/tasks/agc030_a

def main():
    A, B, C = list(map(int, input().split()))
    deli = 0
    while A > 0 and C > 0:
        deli += 1
        A -= 1
        C -= 1
    # 美味しくないクッキーがなくなった
    while B > 0 and C > 0:
        deli += 2
        B -= 1
        C -= 1

    if C > 0:
        deli += 1

    deli += B
    print(deli)


if __name__ == '__main__':
    main()



# Optimized solution
# https://atcoder.jp/contests/agc030/tasks/agc030_a

def main():
    A, B, C = list(map(int, input().split()))
    deli = 0
    if A > 0 and C > 0:
        eat = min(A, C)
        deli += eat
        A -= eat
        C -= eat

    # 美味しくないクッキーがなくなった, お腹壊してない
    if B > 0 and C > 0:
        eat = min(B, C)
        deli += eat * 2
        B -= eat
        C -= eat
    # お腹壊してない

    if C > 0:
        deli += 1

    deli += B
    print(deli)


if __name__ == '__main__':
    main()
# Original solution
from collections import defaultdict as dd
from collections import Counter
from pprint import pprint as pp

YN = {True: 'Yes', False: 'No'}
ri = lambda: int(eval(input()))
ria = lambda: list(map(int, input().split()))
rian = lambda n: [ria() for _ in range(n)]
rs = lambda: eval(input())
rsa = lambda: input().split()
rsn = lambda n: [rs() for _ in range(n)]

A, B, C = ria()

umai = 0
for _ in range(C):
  umai += 1
  if B > 0:
    B -= 1
    umai += 1
  elif A > 0:
    A -= 1
  else:
    break
umai += B

print(umai)

# Optimized solution
from collections import defaultdict as dd
from collections import Counter
from pprint import pprint as pp

YN = {True: 'Yes', False: 'No'}
ri = lambda: int(eval(input()))
ria = lambda: list(map(int, input().split()))
rian = lambda n: [ria() for _ in range(n)]
rs = lambda: eval(input())
rsa = lambda: input().split()
rsn = lambda n: [rs() for _ in range(n)]

A, B, C = ria()

umai = 0
if C <= B:
  umai = C+B
else:
  umai = B*2
  C -= B
  if C <= A:
    umai += C
  else:
    umai += A+1

print(umai)
# Original solution
a, b, c = list(map(int, input().split()))

if c > (a + b):
    print((a + b + 1 + b))
else:
    print((c + b))


# Optimized solution
a, b, c = list(map(int, input().split()))
print((b + min(a + b + 1, c)))
# Original solution
A, B, C = list(map(int, input().split()))
print((min(C, A + B + 1) + B))


# Optimized solution
A, B, C = list(map(int, input().split()))
print((B + min(C, A + B + 1)))
# Original solution
# -*- coding: utf-8 -*-
# 整数の入力
a, b, c = list(map(int, input().split()))
sum = 0
while (1 == 1):
    #Cがある時
    if c > 0:
        c = c - 1
        sum = sum + 1
        if a > 0:
            a = a - 1
        elif b > 0:
            b = b -1
            sum = sum + 1
        else :
            break
    # Cがない時
    else :
        if b > 0:
            b = b -1
            sum = sum + 1
        else :
            break

# 出力
print(("{}" .format(sum)))

# Optimized solution
# -*- coding: utf-8 -*-
# 整数の入力
a, b, c = list(map(int, input().split()))
result = 0
if a + b + 1 >= c:
  result = b + c
else:
  result = b + (a + b + 1)
# 出力
print(("{}" .format(result)))
# Original solution
'''
AtCoder Grand Contest 030
A - Poisonous Cookies
'''


def main():
    a, b, c = list(map(int, input().split()))
    oishii = b + c

    # クッキーを食べる
    while a + b > 0:

        c = c - 1 if c > 0 else 0

        if b > 0:
            b -= 1
        elif a > 0:
            a -= 1

    # まだcが残ってるなら一枚食べれる
    if c > 0:
        c -= 1

    print((oishii - (b + c)))


if __name__ == "__main__":
    main()


# Optimized solution
'''
AtCoder Grand Contest 030
A - Poisonous Cookies
'''


def main():
    a, b, c = list(map(int, input().split()))

    '''
    A + B + 1 ≥ C ならすべての毒入りの
    クッキーを食べることができるので B + C。
    そうでないなら答えはB + (A + B + 1) 。
    ※「+1」は最後にCを食べるので
    '''
    print((b + min(c, a + b + 1)))


if __name__ == "__main__":
    main()
# Original solution
A, B, C = [int(s) for s in input().split()]

poisonous = False
eaten_cookies = 0

while True:
  if not poisonous and C > 0:
    eaten_cookies += 1
    C -= 1
    poisonous = True
  elif poisonous and A > 0:
    A -= 1
    poisonous = False
  elif B > 0:
    eaten_cookies += 1
    B -= 1
    poisonous = False
  else:
    break

print(eaten_cookies)

# Optimized solution
A, B, C = [int(s) for s in input().split()]
print((min([C, A + B + 1]) + B))
# Original solution
a, b, c = list(map(int, input().split()))

cnt = 0
while c > 0 or b > 0:
    if c > 0:
        c -= 1
        cnt += 1
    elif b > 0:
        b -= 1
        cnt += 1
        continue

    if b > 0:
        b -= 1
        cnt += 1
        continue
    elif a > 0:
        a -= 1
        continue
    else:
        break

print(cnt)

# Optimized solution
a, b, c = list(map(int, input().split()))

cnt = 0
if a + b + 1>= c:
    ans = b + c
else:
    ans = b + a + b + 1

print(ans)
# Original solution
A,B,C=list(map(int,input().split()))
poisoned=False
ans=0
while True:
  if C>0 and not poisoned:
    ans+=1
    C-=1
    poisoned=True
  if B>0:
    ans+=1
    poisoned=False
    B-=1
  if A>0 and poisoned:
    A-=1
    poisoned=False
  if (B==0 and A==0 and poisoned) or (B==0 and C==0):
    break
print(ans)

# Optimized solution
A,B,C=list(map(int,input().split()))
if A+B>=C:
  print((B+C))
else:
  print((B*2+A+1))
# Original solution
a,b,c = list(map(int, input().split()))
x = 0
if a<=c:
  x += a
  c -= a
  a = 0
elif a>c:
  x += c
  c = 0
  a -= c
if c<=b:
  x += (c+b)
  c = 0
  b = 0
elif c>b:
  x += (2*b)
  b = 0
  c -= b
if c>0:
  x += 1
print(x)
  

# Optimized solution
a,b,c = list(map(int, input().split()))
print((b+min(c,a+b+1)))
# Original solution
a,b,c=[int(i) for i in input().split()]
count=0
while c > 0:
    c -= 1
    count += 1
    if b > 0:
        b -= 1
        count += 1
    elif a>0:
        a -= 1
    else:
        break
if b>0:
    count+=b
print(count)

# Optimized solution
a,b,c=[int(i) for i in input().split()]
if c<b or c<=a+b:
    print((b+c))
else:
    print((a+b*2+1))
# Original solution
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    def dfs(level=0, u=0):
        if len(to[(level, u)]) == 1:
            cnt_tree[l - level] += 1
        for kl, ku in to[(level, u)]:
            dfs(kl, ku)

    to = defaultdict(set)
    n, l = map(int, input().split())
    ss = [input()[:-1] for _ in range(n)]

    # 各sをパスと見て、辺を張っていく
    for s in ss:
        now_l = 0
        now_u = 0
        for c in s:
            nxt_l = now_l + 1
            nxt_u = (now_u << 1) + int(c)
            to[(now_l, now_u)].add((nxt_l, nxt_u))
            now_l, now_u = nxt_l, nxt_u
    # print(to)

    # パスで分離された木のレベルと個数を数える
    cnt_tree = defaultdict(int)
    dfs()
    #print(cnt_tree)

    # Grundy数をlv & -lvで求め、xorをとる
    sum_xor = 0
    for lv, cnt in cnt_tree.items():
        if cnt % 2 == 0: continue
        sum_xor ^= lv & -lv

    if sum_xor == 0:
        print("Bob")
    else:
        print("Alice")

main()


# Optimized solution
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n, l = map(int, input().split())
    ss = [input()[:-1] for _ in range(n)] + ["$"]
    ss.sort()
    # print(ss)

    # 隣同士のsを先頭から比べて、異なるところでそのlevelの木を消して、それ以降の木を追加する
    cnt_tree = defaultdict(int)
    cnt_tree[l] = 2
    for s0, s1 in zip(ss, ss[1:]):
        for depth, (c0, c1) in enumerate(zip(s0, s1)):
            if c0 == c1: continue
            cnt_tree[l - depth] -= 1
            for lv in range(l - depth - 1, l - len(s1), -1):
                cnt_tree[lv] += 1
            # print(cnt_tree)
            break

    # Grundy数をlv & -lvで求め、xorをとる
    sum_xor = 0
    for lv, cnt in cnt_tree.items():
        if cnt % 2 == 0: continue
        sum_xor ^= lv & -lv

    if sum_xor:
        print("Alice")
    else:
        print("Bob")

main()
# Original solution
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N, L = list(map(int, input().split()))
    dic = defaultdict(int)
    for _ in range(N):
        s = input().rstrip('\n')
        for i in range(len(s)):
            dic[s[:i+1]] = 1

    g = 0
    key = list(dic.keys())
    for s in key:
        t = [si for si in s]
        t[-1] = '0' if t[-1] == '1' else '1'
        t = ''.join(t)
        if not dic[t]:
            tmp = L - len(s) + 1
            cnt = 0
            while tmp % 2 == 0:
                tmp //= 2
                cnt += 1
            g ^= (2**cnt)

    if g:
        print('Alice')
    else:
        print('Bob')


if __name__ == '__main__':
    main()

# Optimized solution
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N, L = list(map(int, input().split()))
    dic = defaultdict(int)
    for _ in range(N):
        s = input().rstrip('\n')
        s = [si for si in s]
        for i in range(len(s)):
            dic[''.join(s[:i+1])] = 1

    g = 0
    key = list(dic.keys())
    for s in key:
        t = [si for si in s]
        t[-1] = '0' if t[-1] == '1' else '1'
        t = ''.join(t)
        if not dic[t]:
            tmp = L - len(s) + 1
            cnt = 0
            while tmp % 2 == 0:
                tmp //= 2
                cnt += 1
            g ^= (2**cnt)

    if g:
        print('Alice')
    else:
        print('Bob')


if __name__ == '__main__':
    main()
# Original solution
s = input().split()

n = int(s[0])
k = int(s[1])

a = input().split()

siyouzumi = [1]
t = 1

for i in range(1,k+1):

    t = int(a[t-1])
    if t in siyouzumi:
        haba = i - siyouzumi.index(t)
        kaisu = int((k - i) / haba)

        l = k - (i + (haba * kaisu))

        for j in range(l):
            t = int(a[t-1])
        break

    siyouzumi.append(t)

print(t)

# Optimized solution
s = input().split()

n = int(s[0])
k = int(s[1])

a = input().split()

siyouzumi = {1}
y = [1]
t = 1

for i in range(1,k+1):

    t = int(a[t-1])
    if t in siyouzumi:
        haba = i - y.index(t)
        kaisu = int((k - i) / haba)

        l = k - (i + (haba * kaisu))

        for j in range(l):
            t = int(a[t-1])
        break

    siyouzumi.add(t)
    y.append(t)


print(t)
# Original solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
town = 1
record = [1]
for i in range(N):
    town = A[town-1]
    if town in record:
        break
    record.append(town)
    
newrecord = record[record.index(town):]
roop = len(newrecord)
newK = K-(len(record)-roop)
if newK>=0:
    answer = newrecord[newK%roop]
else:
    answer = record[K]

print(answer)

# Optimized solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
town = 1
record = [1]
record2 = [0]*N
for i in range(N):
    town = A[town-1]
    if record2[town-1]==1:
        break
    record.append(town)
    record2[town-1] += 1
    
newrecord = record[record.index(town):]
roop = len(newrecord)
newK = K-(len(record)-roop)
if newK>=0:
    answer = newrecord[newK%roop]
else:
    answer = record[K]

print(answer)
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

c = 0
loc = 0
locs = [0]
ini = 0

while True:
    c += 1
    loc = a[loc] - 1
    
    if c == k:
        print((loc + 1))
        break
    if loc in locs:
        ini = locs.index(loc)
        break
    locs.append(loc)

if c != k:
    rest = int((k - ini) % (c - ini))
    if rest <= 0:
        print((loc + 1))
    else:
        d = 0
        while True:
            d += 1
            loc = a[loc] - 1
            if d == rest:
                print((loc + 1))
                break


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

c = 0
loc = 0
locs = {0: 0}
ini = 0

while True:
    c += 1
    loc = a[loc] - 1
    
    if c == k:
        print((loc + 1))
        break
    if loc in locs:
        ini = locs.get(loc)
        break
    locs[loc] = c

if c != k:
    rest = int((k - ini) % (c - ini))
    if rest <= 0:
        print((loc + 1))
    else:
        d = 0
        while True:
            d += 1
            loc = a[loc] - 1
            if d == rest:
                print((loc + 1))
                break
# Original solution
N, K = list(map(int, input().split()))
A = [1] + list(map(int, input().split()))

p = 1
points = [p]
for _ in range(N):
    p = A[p]
    if p in points:
        break
    points.append(p)

idx = points.index(p)

if idx > K:
    print((points[K]))
    exit()

cycle = points[idx:]

print((cycle[(K - idx) % len(cycle)]))

# Optimized solution
N, K = list(map(int, input().split()))
A = [1] + list(map(int, input().split()))

p = 1
points = [p]
visited = set([p])

for _ in range(N):
    p = A[p]
    if p in visited:
        break
    points.append(p)
    visited.add(p)

idx = points.index(p)

if idx > K:
    print((points[K]))
    exit()

cycle = points[idx:]

print((cycle[(K - idx) % len(cycle)]))
# Original solution

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

town_loop = [1]

for i in range(k):
    next_town = alist[town_loop[i]-1]

    try:
        loop_start = town_loop.index(next_town)
        break
    except ValueError:
        town_loop.append(next_town)

if len(town_loop) == k+1:
    print((town_loop[k]))
else:
    o = len(town_loop) - loop_start
    print((town_loop[((k-loop_start) % o) + loop_start]))


# Optimized solution

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

town_loop = [1]
used = [-1] * n

for i in range(k):
    next_town = alist[town_loop[i]-1]

    if used[next_town-1] == -1:
        town_loop.append(next_town)
        used[next_town-1] = 1
    else:
        break

loop_start = town_loop.index(next_town)
if len(town_loop) == k+1:
    print((town_loop[k]))
else:
    o = len(town_loop) - loop_start
    print((town_loop[((k-loop_start) % o) + loop_start]))
# Original solution
import sys
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [1]
cnt = 0
next_town = 0

l = len(a)
for j in range(l):
    cnt += 1
    if j==0:
        b.append(a[0])
        next_town = a[0]
        continue
    
    b.append(a[next_town - 1])
    next_town = a[next_town -1]
    for i in range(l):
        if b.count(i+1)==2:
            k -= cnt
            idx_f = b.index(i+1)
            roop_list = b[idx_f:-1]
            roop_num = len(b[idx_f:-1])
            last = k %roop_num

            print((roop_list[last]))
            sys.exit()



# Optimized solution
N, K = list(map(int, input().split()))
A = [int(a) - 1 for a in input().split()]

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

p = 0
t = [-1] * N
t[0] = 0
for i in range(1, N):
    p = A[p]
    if t[p] != -1:
        break
    t[p] = i

d = i - t[p]
K -= i
K %= d

for i in range(K):
    p = A[p]
print((p + 1))
# Original solution
def main():

    N_machi, K_teleport = list(map(int, input().split()))
    to_teleport = list(map(int, input().split()))

    def find_double_town():
        visited_city = [0]
        first_twice = N_machi
        
        next_city = 0
        
        for i in range(N_machi):
            next_city = to_teleport[next_city] - 1
            visited_city.append(next_city)
            if (len(visited_city) != len(set(visited_city))):
                first_twice = i + 1
                break
        
        return (next_city, first_twice)


    index_city, second_visit = find_double_town()
    if (index_city == 0):
        first_visit = 0
    else:
        next_city = 0        
        for i in range(N_machi):
            next_city = to_teleport[next_city] - 1
            
            if (next_city == index_city):
                first_visit = i + 1
                break

    if (K_teleport >= first_visit):
        mod = (K_teleport - first_visit) % (second_visit - first_visit)
        next_city = index_city
        
    else:
        mod = K_teleport
        next_city = 0

    for i in range(mod):
        next_city = to_teleport[next_city] - 1

    ans = next_city + 1

    print(ans)
    # print(first_visit,second_visit)


if __name__ == '__main__':
    main()

# Optimized solution
def main():

    N_machi, K_teleport = list(map(int, input().split()))
    to_teleport = list(map(int, input().split()))

    visited_city = [1]
    order = [-1 for i in range(N_machi + 1)]
    order[1] = 0
    next_city = 1
    
    for i in range(N_machi):
        next_city = to_teleport[next_city - 1]
        tmp = order[next_city]
        
        if (tmp == -1):
            visited_city.append(next_city)
            order[next_city] = i + 1
        else:
            break
    
    first = order[next_city]
    len_loop = len(visited_city) - first
    
    if (K_teleport < first):
        ans = visited_city[K_teleport]
    else:
        mod = (K_teleport - first) % len_loop
        ans = visited_city[first + mod]
    
    print(ans)
        

if __name__ == '__main__':
    main()
# Original solution
import sys

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

L = [1]
tmp = 1
ct = 0
check = 0
while True:
    x = A[tmp - 1]
    ct += 1
    if x in L or ct > k:
        check = x
        break
    else:
        L.append(x)
        tmp = x
if ct > k:
    print((L[k]))
    sys.exit()
p = L.index(check)
l = len(L)
roop = l - p
if k < p:
    print((L[k]))
else:
    k -= p
    k %= roop
    print((L[p + k]))

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

visit = [-1] * n   #到達しているかどうかを管理

D = [1]  # 循環するもののindexを格納

tmp = A[0]  #初期位置
ct = 1

while visit[tmp - 1] == -1:
    D.append(tmp)
    visit[tmp - 1] = ct  #到達済に変更
    if k == ct:
        print(tmp)
        exit()
    ct += 1
    tmp = A[tmp - 1]

#循環している場合の残りの処理
k -= visit[tmp - 1]
k %= (ct - visit[tmp - 1])
print((D[visit[tmp - 1] + k]))
# Original solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
kiseki=[]
u=1
kiseki.append(1)
for i in l:
  u=l[u-1]
  if kiseki.count(u)==0:
    kiseki.append(u)
  else:
    kiseki.append(u)
    break
kuri=kiseki[kiseki.index(u):len(kiseki)-1]
if k<=len(kiseki)-(len(kuri)+1):
  print((kiseki[k]))
else:
  K=k-(len(kiseki)-(len(kuri)+1))
  amari=K%len(kuri)
  print((kuri[amari]))

# Optimized solution
n,k=list(map(int,input().split()))
l=list(map(int,input().split()))
kiseki=[-1]*n
i=0
v=1
while kiseki[v-1]==-1:
  kiseki[v-1]=i
  v=l[v-1]
  i+=1
saisyo=kiseki[v-1]
kuri=i-saisyo

if saisyo>k:
  print((kiseki.index(k)+1))
else:
  amari=(k-saisyo)%kuri
  print((kiseki.index(amari+saisyo)+1))
# Original solution
def main():
  N, K = list(map(int, input().split()))
  A_list = input().split()

  root = [1]

  break_flag = 0
  for n in range(K):
    new = A_list[ int(root[n])-1 ]
    if new not in root:
      root.append(new)
    else:
      ind = root.index(new)
      break
  else:
    print(new)
    break_flag = 1
    
  if break_flag == 0:  
	  pre = len(root[:ind])
	  roop = len(root)-pre

	  print((root[ind:][(K-pre)%roop]))
   
if __name__ == '__main__':
  main()

# Optimized solution
def main():
  N, K = list(map(int, input().split()))
  A_list = input().split()

  root = [1]

  record = [-1]*N
  break_flag = 0
  for n in range(K):
    new = int(A_list[ int(root[n])-1 ])
    if record[new-1] <0:
      record[new-1]=1
      root.append(new)
    else:
      ind = root.index(new)
      break
  else:
    print(new)
    break_flag = 1
    
  if break_flag == 0:  
	  pre = len(root[:ind])
	  roop = len(root)-pre

	  print((root[ind:][(K-pre)%roop]))
   
if __name__ == '__main__':
  main()