Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
q = INT()
p = [input() for i in range(q)]
flag = 0

for i in range(q):
        if int(p[i][0]) == 1:
                flag = 1 - flag
        else:
                t, f, c = p[i].split()
                if int(f) == 1:
                        if flag == 0:
                                s = c + s
                        else:
                                s = s + c
                else:
                        if flag == 0:
                                s = s + c
                        else:
                                s = c + s 
print(s if flag == 0 else s[::-1])

### FIXED CODE ###

import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
q = INT()
p = [0]*q
flag = 0
c0 = c1 = ''

for i in range(q):
        p[i] = input()
        if p[i][0] == '1':
                flag = 1 - flag
        else:
                t, f, c = p[i].split()
                if (f == '1' and flag == 0) or (f == '2' and flag == 1):
                        c0 = c + c0
                else:
                        c1 = c1 + c

s = c0 + s + c1
print(s if flag == 0 else s[::-1])
### BUGGY CODE ###

import itertools
from collections import deque
N, X = map(int, input().split())

d = deque()
current = "P"

for _ in range(1, N+1):
  d.appendleft("B")
  d.extend(current)
  d.append("P")
  d.extend(current)
  d.append("B")
  current = d

l = list(itertools.islice(d, len(d) - X, len(d)))
print(l.count("P"))


### FIXED CODE ###

N, X = map(int, input().split())

layer_size = [1]
P_cnt = [1]

for _ in range(50):
  #[-1]は末尾の要素を取得。layer_sizeで、合計
  layer_size.append(layer_size[-1]*2+3)

  #[-1]は末尾の要素を取得。P_cntはパティの枚数
  P_cnt.append(P_cnt[-1]*2+1)


def solve(level, x):
  if x == 1:
    return 0 if level > 0 else 1
  l = layer_size[level-1]
  p = P_cnt[level-1]
  if x <= 1+l:
    return solve(level-1, x-1)
  elif x == l+2:
    return p+1
  elif x <= 2*l+2:
    return p+1+solve(level-1, x-l-2)
  else:
    return 2*p+1
  
ans = solve(N, X)
print(ans)
### BUGGY CODE ###

n,m = map(int,input().split())
l = []
cnt = 0
for i in range(m):
  l.append([int(i) for i in input().split()])
  
for i in range(1,n+1):
  flag = 1
  for k in range(m):
    if i < l[k][0] or i > l[k][1]:
      flag = 0
  if flag == 1:
    cnt += 1
print(cnt)

### FIXED CODE ###

n,m = map(int,input().split())
l = []
for i in range(m):
  l.append([int(i) for i in input().split()])
maxL = l[0][0]
minR = l[0][1]
for j in range(1,m):
  if maxL <= l[j][0]:
    maxL = l[j][0]
  if minR >= l[j][1]:
    minR = l[j][1]
if maxL <= minR:
  print(minR - maxL + 1)
else:
  print(0)
### BUGGY CODE ###

a, b = map(int, input().split())
n = min(a, b)

l = []
for i in range(n//2):
    if a % (i+2) == 0 and b % (i +2) == 0:
        l.append(i+2)

if a % n == 0 and b % n == 0 and n != 1:
    l.append(n)
    
primes = [2]
for x in l:
    if all(x % prime !=0 for prime in primes):
        primes.append(x)

if  a % 2 == 0 or b % 2 == 0:
    ans = len(primes) +1
else:
    ans = len(primes)
print(ans)

### FIXED CODE ###

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

def gcd(a, b):
    while b > 0:
        a, b = b, a%b
    return a
n = gcd(a, b)

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

ans = set(prime_factorize(n))
print(len(ans)+1)
### BUGGY CODE ###

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

scale = 1
while scale <= K:
    scale *= 2
scale /= 2

X = 0
while scale >= 1:
    count = 0
    for i in range(N):
        if A[i] >= scale:
            A[i] -= scale
            count += 1
        else:
            count -= 1
    if count > 0:
        X += scale

ans = 0
for a in A:
    ans += X^a
print(ans)


### FIXED CODE ###

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

X = 0
ans = 0
for i in range(40, -1, -1):
    bit = 1 << i
    count = 0
    for a in A:
        if bit & a:
            count += 1
    if N >= count * 2 and (X^bit) <= K:
        X ^= bit
        count = N - count
    ans += count * bit

print(ans)
### BUGGY CODE ###

n = int(input())
x = list(map(int, input().split()))
#y = sorted(set(x))

def getMedian(a):
    #a = sorted(set(a))
    a = sorted(a)
    return a[(len(a)+1)//2-1]
    # if len(a) % 2 == 0:
    #     return a[(len(a))//2-1]
    # else:
    #     return a[(len(a)+1)//2-1]

for i in range(n):
    temp = list(x)
    del temp[i]
#    print(temp)
    print(getMedian(temp))

### FIXED CODE ###

n = int(input())
x = list(map(int, input().split()))
xx = sorted(x, reverse=True)

m1 = xx[n//2-1]
m2 = xx[n//2]

# print(m1, m2)

for i in range(len(x)):
    if x[i] < m1:
        print(m1)
    elif x[i] >= m1:
        print(m2)
### BUGGY CODE ###

n=int(input())
li=list(map(int, input().split()))
ans="YES"
for i in range(n):
  for j in range(i+1, n):
    if li[i]==li[j]:
      ans="NO"
      break
  else:
    continue
  break
print(ans)

### FIXED CODE ###

n=int(input())
li=list(map(int, input().split()))
if n==len(set(li)):
  print("YES")
else:
  print("NO")
### BUGGY CODE ###

n, k = map(int, input().split())
p = [int(i) - 1 for i in input().split()]
c = list(map(int, input().split()))

def mymax(max_variable, candidate):
    if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate)

max_cost = None
for start in range(n):
    seen = []
    next_ = p[start]
    cost_sum = []
    cost = 0
    while next_ != start:
        seen.append(next_)
        cost += c[next_]
        cost_sum.append(cost)
        next_ = p[next_]

    seen.append(start)
    cost += c[start]
    cost_sum.append(cost)

    l = len(seen)
    s = cost_sum[-1]
    for end in range(len(seen)):
        moves = end + 1
        remain_moves = k - moves
        if remain_moves < 0:
            continue
        cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l)
        max_cost = mymax(max_cost, cand_cost)
print(max_cost)



### FIXED CODE ###

n, k = map(int, input().split())
p = [int(i) - 1 for i in input().split()]
c = list(map(int, input().split()))

def mymax(max_variable, candidate):
    if max_variable is None:
        return candidate
    else:
        return max(max_variable, candidate)

max_cost = None
for start in range(n):
    seen = []
    next_ = p[start]
    cost_sum = []
    cost = 0
    while next_ != start:
        seen.append(next_)
        cost += c[next_]
        cost_sum.append(cost)
        next_ = p[next_]

    seen.append(start)
    cost += c[start]
    cost_sum.append(cost)

    l = len(seen)
    s = cost_sum[-1]
    for end in range(len(seen)):
        moves = end + 1
        remain_moves = k - moves
        if remain_moves < 0:
            continue
        cand_cost = cost_sum[end] + max(0, s) * int(remain_moves / l)
        max_cost = mymax(max_cost, cand_cost)
print(max_cost)
### BUGGY CODE ###

a,b,q = list(map(int,input().split()))
s = [int(input()) for i in range(0,a)]
t = [int(input()) for i in range(0,b)]
x = [int(input()) for i in range(0,q)]

s.sort()
t.sort()

def bifind(r,z):
    y = len(r)//2
    halftime = 2
    if z < r[0]:
        return [10**11,r[0]]
    elif z > r[-1]:
        return [r[-1],10**11]
    while True:
        if r[y] <= z:
            y += len(r)//(2**halftime)+1
        else:
            y -= len(r)//(2**halftime)+1
        halftime += 1
        if y+1  >= len(r):
            y = len(r)-2
        if y  < 0:
            y = 0
        if r[y] < z < r[y+1]:
            break
    return [r[y],r[y+1]]

for i in range(0,q):
    ls = bifind(s,x[i])
    lt = bifind(t,x[i])
    ans = 10**11
    for j in range(0,2):
        for k in range(0,2):
            ans = min(ans,abs(ls[j]-x[i])+abs(lt[k]-ls[j]))
            ans = min(ans,abs(lt[k]-x[i])+abs(lt[k]-ls[j]))
    print(ans)

### FIXED CODE ###

a,b,q = list(map(int,input().split()))
s = [int(input()) for i in range(0,a)]
t = [int(input()) for i in range(0,b)]
x = [int(input()) for i in range(0,q)]
import bisect

def bifind(r,z):
    if z < r[0]:
        return [r[0],r[0]]
    elif z > r[-1]:
        return [r[-1],r[-1]]
    y = bisect.bisect_left(r,z)-1
    return [r[y],r[y+1]]

for i in range(0,q):
    ls = bifind(s,x[i])
    lt = bifind(t,x[i])
    ans = list()
    for j in range(0,2):
        for k in range(0,2):
            ans.append(abs(ls[j]-x[i])+abs(lt[k]-ls[j]))
            ans.append(abs(lt[k]-x[i])+abs(lt[k]-ls[j]))
    print(min(ans))
### BUGGY CODE ###

import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(input())
    a = sorted(list(map(int,input().split())))
    dp = [0]*(max(a)+1)
    for i in range(n):
        j = 2
        dp[a[i]]+=1
        while True:
            b = a[i]*j
            if b>max(a):
                break
            dp[b]+=1
            j+=1
    cnt = 0
    for i in range(n):
        if dp[a[i]] == 1:
            cnt+=1
    print(cnt)

### FIXED CODE ###

import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(input())
    a = sorted(list(map(int,input().split())))
    M = 1000005
    dp = [0]*(M)
    for i in range(n):
        if dp[a[i]] != 0:
            dp[a[i]] = 2
            continue
        for j in range(a[i],M,a[i]):
            dp[j]+=1
    cnt = 0
    for i in range(n):
        if dp[a[i]] == 1:
            cnt+=1
    print(cnt)
### BUGGY CODE ###

N = int(input())
A = (int(x) for x in input().split())
sum  = 1
flag = 0
for i in A:
    sum = sum * i
    if(i == 0):
        print(0)
        flag=1
        break
if(flag==0):
    if(sum > (10**18)):
        print(-1)
    else:
        print(sum)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
sum  = 1
flag = 0
for i in A:
    if(i == 0):
        print(0)
        flag=2
        break
if(flag==0):
    for i in A:
        sum = sum * i
        if(sum > 10**18):
            flag=1
            break
if(flag==0):
    print(sum)
elif(flag==1):
    print(-1)
### BUGGY CODE ###

a,b=map(int, input().split())
ans=1
if a <= 0 and b >= 0:
  print('Zero')
  exit(0)
for i in range(a, b+1):
  ans = ans*i
if ans>0:
  print('Positive')
else:
  print('Negative')

### FIXED CODE ###

a,b=map(int, input().split())
ans=1
if a <= 0 and b >= 0:
  print('Zero')
  exit(0)
if a>0 and b > 0:
  print('Positive')
  exit(0)
if a <0 and b<0:
  d=abs(b-a)+1
  if d %2==0:
    print('Positive')
    exit(0)
  else:
    print('Negative')
    exit(0)
### BUGGY CODE ###

import numpy as np

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

dp = np.zeros(N+1, dtype=int)

ans = 0
for h, a in zip(H, A):
  dp[h] = np.max(dp[:h]) + a
  ans = max(ans, dp[h])

print(ans)

### FIXED CODE ###

class SegmentTree:
    def __init__(self, a, mode='min'):
        self.mode = mode
        if self.mode == 'min':
            self.padding = float('inf')
        elif mode == 'max':
            self.padding = -float('inf')
        elif mode == 'sum':
            self.padding = 0
        
        n = len(a)
        self.N = 2 ** (n-1).bit_length()
        self.data = [self.padding]*(self.N-1) + a + [self.padding]*(self.N-n)
        for i in range(2*self.N-2, 0, -2):
            self.data[(i-1)//2] = self._segfunc(*[self.data[i], self.data[i-1]])
    
    def _segfunc(self, *v_list):
        if self.mode == 'min':
            return min(v_list)
        elif self.mode == 'max':
            return max(v_list)
        elif self.mode == 'sum':
            return sum(v_list)
    
    def update(self, idx, x):
        idx = self.N - 1 + idx
        self.data[idx] = x
        while idx:
            idx = (idx-1) // 2
            self.data[idx] = self._segfunc(
                *[self.data[2*idx+1], self.data[2*idx+2]]
            )
    
    def search(self, i, j):
        # [i, j)の区間の最小値を求める
        idx1 = self.N-1+i
        idx2 = self.N-1+j-1
        result = self.padding
        while idx1 + 1 < idx2:
            if idx1&1 == 0: # idx1が偶数
                result = self._segfunc(*[result, self.data[idx1]])
            if idx2&1 == 1: # idx2が奇数
                result = self._segfunc(*[result, self.data[idx2]])
                idx2 -= 1
            
            idx1 //= 2
            idx2 = (idx2 - 1)//2
        
        if idx1 == idx2:
            result = self._segfunc(result, self.data[idx1])
        else: # idx1 + 1 == idx2
            result = self._segfunc(*[result, self.data[idx1], self.data[idx2]])
        
        return result


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

dp = [0] * (N+1)
st = SegmentTree(dp, mode='max')

ans = 0
for h, a in zip(H, A):
  M = st.search(0, h)
  st.update(h, M + a)

print(st.search(0, N+1))
### BUGGY CODE ###

N=int(input())
E=[tuple(sorted(map(int,input().split()))) for i in range(N-1)]

DICT={x:ind for ind,x in enumerate(E)}

EDGE=[[] for i in range(N+1)]
for x,y in E:
    EDGE[x].append(y)
    EDGE[y].append(x)


M=int(input())
Q=[tuple(map(int,input().split())) for i in range(M)]

def dfs(x,y):
    USE=[-1]*(N+1)
    USE[x]=0

    NOW=[x]

    while NOW:
        n=NOW.pop()

        for to in EDGE[n]:
            if USE[to]==-1:
                USE[to]=n
                NOW.append(to)

    ANS=[y]
    while y!=x:
        y=USE[y]
        ANS.append(y)

    RET=[]

    for i in range(1,len(ANS)):
        RET.append(DICT[tuple(sorted([ANS[i-1],ANS[i]]))])

    return set(RET)

Q2=[dfs(x,y) for x,y in Q]

DP=[0]*(1<<M)
DP[0]=1

import copy
for i in range(N-1):
    NDP=copy.deepcopy(DP)
    B=0
    for k in range(M):
        if i in Q2[k]:
            B+=1<<k
        
    for j in range(1<<M):
        NDP[j|B]+=DP[j]

    DP=NDP

print(DP[-1])

### FIXED CODE ###

N=int(input())
E=[tuple(sorted(map(int,input().split()))) for i in range(N-1)]

DICT={x:ind for ind,x in enumerate(E)}

EDGE=[[] for i in range(N+1)]
for x,y in E:
    EDGE[x].append(y)
    EDGE[y].append(x)


M=int(input())
Q=[tuple(map(int,input().split())) for i in range(M)]

def dfs(x,y):
    USE=[-1]*(N+1)
    USE[x]=0

    NOW=[x]

    while NOW:
        n=NOW.pop()

        for to in EDGE[n]:
            if USE[to]==-1:
                USE[to]=n
                NOW.append(to)

    ANS=[y]
    while y!=x:
        y=USE[y]
        ANS.append(y)

    RET=[]

    for i in range(1,len(ANS)):
        RET.append(DICT[tuple(sorted([ANS[i-1],ANS[i]]))])

    return set(RET)

Q2=[dfs(x,y) for x,y in Q]

DP=[0]*(1<<M)
DP[0]=1

for i in range(N-1):
    B=0
    for k in range(M):
        if i in Q2[k]:
            B+=1<<k
        
    for j in range((1<<M)-1,-1,-1):
        DP[j|B]+=DP[j]

print(DP[-1])
### BUGGY CODE ###

N, Q = [int(v) for v in input().split()]

class Node:
    def __init__(self):
        self.list = []
        self.score = 0

    def add(self, node):
        self.list.append(node)

nodes = []
for i in range(0, N):
    nodes.append(Node())

for i in range(0, N - 1):
    a, b = [int(v) - 1 for v in input().split()]

    nodes[a].add(nodes[b])

for i in range(0, Q):
    p, x = [int(v) for v in input().split()]

    nodes[p-1].score += x

l = [nodes[0]]

i = 0
while i < len(l):
    node = l[i]
    i += 1
    for child in node.list:
        child.score += node.score
        l.append(child)

print(' '.join(map(lambda n: str(n.score), nodes)))

### FIXED CODE ###

N, Q = [int(v) for v in input().split()]

class Node:
    def __init__(self):
        self.list = []
        self.score = 0

    def add(self, node):
        self.list.append(node)

nodes = []
score = [0] * N

for i in range(0, N):
    nodes.append([])

for i in range(0, N - 1):
    a, b = [int(v) - 1 for v in input().split()]
    nodes[a].append(b)

for i in range(0, Q):
    p, x = [int(v) for v in input().split()]
    score[p-1] += x

l = [0]

i = 0
while i < len(l):
    node = l[i]
    for child in nodes[node]:
        score[child] += score[l[i]]
        l.append(child)
    i += 1

print(' '.join(map(str, score)))
### BUGGY CODE ###

import sys
from collections import deque

H, W = map(int, sys.stdin.readline().split())
grid = [["#"] * (W+2)]
for _ in range(H):
    grid.append(["#"] + list(sys.stdin.readline().strip()) + ["#"])
grid.append(["#"] * (W+2))
# print(grid)

q = deque()
for j in range(1, H+1):
    for i in range(1, W+1):
        if grid[j][i] == "#":
            q.append((i, j, 0))

ans = 0
while q:
    x, y, count = q.popleft()
    for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        if grid[y+dy][x+dx] != ".":
            continue
        grid[y+dy][x+dx] = count + 1
        ans = count + 1
        q.append((x+dx, y+dy, count + 1))

# print(grid)
print(ans)

### FIXED CODE ###

import sys
from pprint import pprint

from collections import deque

h, w = map(int, sys.stdin.readline().strip().split(" "))
field = []
for _ in range(h):
    field.append(list(sys.stdin.readline().strip()))

# pprint(field)

q = deque()
for i in range(h):
    for j in range(w):
        if field[i][j] == "#":
            q.append((0, i, j))

while q:
    count, y, x = q.popleft()
    for dy, dx in ((-1, 0), (1, 0), (0, 1), (0, -1)):
        if 0 <= y+dy < h and 0 <= x+dx < w:
            # print(y+dy, x+dx, field[y+dy][x+dx])
            if field[y+dy][x+dx] == "#":
                continue
            else:
                field[y+dy][x+dx] = "#"
                q.append((count+1, y+dy, x+dx))

# pprint(field)
print(count)
### BUGGY CODE ###

from sys import stdin
import re
from itertools import combinations


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    S = _in[0]  # type:str
    T = _in[1]  # type:str
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    ans = 0
    if T in S:
        ans = 0
    else:
        for i in range(len(T)):
            ans = i + 1
            index = [j for j in range(len(T))]
            comb = list(combinations(index, i+1))

            for _comb in comb:
                t = list(T)
                for _i in _comb:
                    t[_i] = r'(\w)'
                t = r''.join(t)
                if re.search(t, S):
                    break
            else:
                continue
            break
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

from sys import stdin


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    S = _in[0]  # type:str
    T = _in[1]  # type:str
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    ans = float('inf')
    S_ = list(S)
    T_ = list(T)

    for i in range(len(S_)-len(T_)+1):
        s_ = S_[i:i+len(T_)]
        cnt = 0
        for a,b in zip(s_, T_):
            if a == b:
                pass
            else:
                cnt += 1
        ans = min(ans, cnt)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(ans)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

MX = 401

N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N
for i in range(N):
    a, b, c = map(int, input().split())
    A[i] = a
    B[i] = b
    C[i] = c
INF = 10**18
dp = [[[INF]*MX for i in range(MX)] for i in range(N+1)]
dp[0][0][0] = 0
for i in range(1, N+1):
    for j in range(0, MX):
        for k in range(0, MX):
            dp[i][j][k] = dp[i-1][j][k]
            if j < A[i-1] or k < B[i-1]: continue
            dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-A[i-1]][k-B[i-1]]+C[i-1])
ans = INF
for i in range(1, MX):
    if Ma*i > MX-1 or Mb*i > MX-1: break
    ans = min(ans, dp[N][Ma*i][Mb*i])
if ans == INF:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

MX = 401

N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N
for i in range(N):
    a, b, c = map(int, input().split())
    A[i] = a
    B[i] = b
    C[i] = c
INF = 10**18
dp = [[[INF]*MX for i in range(MX)] for i in range(N+1)]
dp[0][0][0] = 0
for i in range(1, N+1):
    for j in range(0, MX):
        for k in range(0, MX):
            dp[i][j][k] = dp[i-1][j][k]
            if j < A[i-1] or k < B[i-1]: continue
            dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-A[i-1]][k-B[i-1]]+C[i-1])
ans = INF
for i in range(1, MX):
    if Ma*i > MX-1 or Mb*i > MX-1: break
    ans = min(ans, dp[N][Ma*i][Mb*i])
if ans == INF:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

n = int(input())
li = list(map(int,input().split()))
ans = 0

import math
import bisect
import collections
from collections import deque
import heapq
c = collections.Counter(li)
li = list(set(li))
heapq.heapify(li)
b = []
for l in li:
    heapq.heappush(b,(-1)*l)
while len(b)>0:
    ba = heapq.heappop(b) * (-1)
    #print(ba)
    if c[ba]>1:
        continue
    #s_ba = math.ceil(math.sqrt(ba))+1
    #index = bisect.bisect_right(li, s_ba)
    tmp_li = li
    #print(ba,tmp_li)
    for tt in b:
        if ba%tt == 0:
            break
    else:
        ans += 1
print(ans)

### FIXED CODE ###

n = int(input())
li = list(map(int,input().split()))
ans = 0
cnt_li = [0]*(10**6+1)

import math
import bisect
import collections
c = collections.Counter(li)
li = list(set(li))
li.sort()

for l in li:
    for x in range(2,10**7):
        if x*l > (10**6):
            break
        else:
            cnt_li[x*l] += 1


while len(li)>0:
    ba = li.pop(-1)
    if c[ba]>1:
        continue
    if cnt_li[ba] == 0:
        ans += 1
print(ans)
### BUGGY CODE ###

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

loop_checker = [1]
before_loop_len = 0
loop_len = 0

now = 0
while True:
    if A[now] in loop_checker:
        index_val = loop_checker.index(A[now])
        loop_slice = loop_checker[index_val:]
        before_loop_len = loop_checker.index(A[now])
        loop_len = len(loop_slice)
        break
    else:
        loop_checker.append(A[now])
        now = A[now] - 1
if K <= before_loop_len:
    print(loop_checker[K])
else:
    checkK_prep = (K - before_loop_len)
    checkK = checkK_prep % loop_len
    print(loop_slice[checkK])


### FIXED CODE ###

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

flag = [False] * N
counted = [1]

nextT = A[0]
flag[0] = True
for x in range(N):
    if flag[nextT - 1] is False:
        counted.append(nextT)
        flag[nextT - 1] = True
        nextT = A[nextT - 1]
    else:
        break
before_loop_len = counted.index(nextT)
loop_len = x - before_loop_len + 1

if K <= before_loop_len:
    print(counted[K])
else:
    checkK_prep = K - before_loop_len
    checkK = checkK_prep % loop_len
    print(counted[before_loop_len + checkK])