Harryxun/stf_run huggingface dataset:
input_count = 0

import sympy

def func(n, history):
    try:
        l = sympy.divisors(n)
    except:
        return history
    for i in l[1:]:
        if n == i:
            history.append(i)
            return history
        if i in history:
            continue
        history.append(i)
        func(n//i, history)
        break
    return history

N = int(input())
if N == 0:
    print(0)
else:
    h = func(N, [])
    print(len(set(h)))

### FIXED CODE ###

history = set([])

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def func(n):
    if n == 1:
        return 
    l = make_divisors(n)
    for i in sorted(l[1:]):
        if i in history:
            continue
        if len(factorization(i)) > 1:
            continue
        history.add(i)
        _ = n // i
        func(_)
        break
    return

N = int(input())
if N == 0:
    print(0)
elif N == 1:
    print(0)
else:
    func(N)
    print(len(history))
N = map(int, raw_input())

S = raw_input().strip()
L = len(S)

P = 10**9 + 7

facts = [0 for _ in xrange(L + N)]
finvs = [0 for _ in xrange(L + N)]
facts[0] = 1
for i in xrange(1, L+N):
  facts[i] = (i*facts[i-1]) % P

finvs[L+N-1] = pow(facts[L+N-1], P-2, P)
for i in xrange(L+N-2, -1, -1):
  finvs[i] = (finvs[i+1]*(i+1)) % P

ans = 0
for k in xrange(0, K+1):
  ncr = (facts[k+L-1]*finvs[k]*finvs[L-1]) % P
  ans += ncr * pow(25, k, P) * pow(26, K-k, P)
  ans %= P

print ans

### FIXED CODE ###

K = int(raw_input())
S = raw_input().strip()
L = len(S)

P = 10**9 + 7

facts = [0 for _ in xrange(L + K)]
finvs = [0 for _ in xrange(L + K)]
facts[0] = 1
for i in xrange(1, L+K):
  facts[i] = (i*facts[i-1]) % P

finvs[L+K-1] = pow(facts[L+K-1], P-2, P)
for i in xrange(L+K-2, -1, -1):
  finvs[i] = (finvs[i+1]*(i+1)) % P

ans = 0
for k in xrange(0, K+1):
  ncr = (facts[k+L-1]*finvs[k]*finvs[L-1]) % P
  ans += ncr * pow(25, k, P) * pow(26, K-k, P)
  ans %= P

print ans
n, k = [int(i) for i in input().split()]

result = min(n % k, k - (n % k), k % n, n - (k % n))

print(result)

### FIXED CODE ###

n, k = [int(i) for i in input().split()]

if n >= k:
    result = min(n % k, k - (n % k))
elif 2 * n >= k > n:
    n = abs(k - n)
    result = n
else:
    result = n

print(result)
S = list(input())
K = int(input())
count = 0
if S[0] != S[-1]:
    flag = False
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = 1
            count += 1
    print(K*count)
else:
    if len(S) == 2:
        print(K)
    else:
        if S[0] == S[1] and S[-1] != S[-2]:
            S[0] = 1
            count += 1
        elif S[0] != S[1] and S[-1] == S[-2]:
            S[-1] = 1
            count += 1
        elif S[0] == S[1] and S[-1] == S[-2]:
            S[0] = 1
            count += 1
        elif S[0] != S[1] and S[-1] != S[-2]:
            S[0] = 1
            count += 1
        
        
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = 1
                count += 1
        print(K*count)

### FIXED CODE ###

from collections import Counter
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
elif len(Counter(s).values()) == 1:
    print(K*len(s)//2)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    now = s[-1]
    end= 0
    for i in range(N-2,-1,-1):
        if s[i] == now:
            bcount += 1
        else:
            end = i
            break   
    ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
    now = s[start]
    count = 1
    mcount = 0
    for i in range(start+1,end+1):
        if s[i] == now:
            count += 1
        else:
            mcount += count//2
            count = 1
            now = s[i]
    if count != 1:
        mcount += count//2
    print(ans + mcount*K)
X = int(input())

if X >= 30:
    print(Yes)
else:
    print(No)

### FIXED CODE ###

X = int(input())

if X >= 30:
    print("Yes")
else:
    print("No")
import math
N,X = map(int,input().split())
A = list(map(int,input().split()))
B = [abs(X-i) for i in A]
ans = B[0]
for j in B:
    ans = math.gcd(ans, j)
print(ans)

### FIXED CODE ###

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
  
N,X = map(int,input().split())
A = list(map(int,input().split()))
B = [abs(X-i) for i in A]
ans = B[0]
for j in B:
    ans = gcd(ans, j)
print(ans)
t=list(input())
for i in range(len(t)):
    if t[i]=="?":
        if i==0:
            if t[1]=="D":
                t[0]="P"
            else:
                t[0]="D"
        elif i==len(t)-1:
            t[i]="D"
        else:
            if t[i-1]=="P":
                t[i]="D"
            elif t[i+1]=="D":
                t[i]="P"
            elif t[i+1]=="?":
                t[i]="P"
                t[i+1]="D"
            else:
                t[i]="D"
print("".join(t))

### FIXED CODE ###

import sys
t=list(input())
for i in range(len(t)):
    if t[i]=="?":
        if len(t)==1:
            print("D")
            sys.exit()
        else:
            if i==0:
                if t[1]=="D":
                    t[0]="P"
                else:
                    t[0]="D"
            elif i==len(t)-1:
                t[i]="D"
            else:
                if t[i-1]=="P":
                    t[i]="D"
                elif t[i+1]=="D":
                    t[i]="P"
                elif t[i+1]=="?":
                    t[i]="P"
                    t[i+1]="D"
                else:
                    t[i]="D"
print("".join(t))
A, B = map(int, input().split())
print(0 if A - (B*2) < 0 else A - (B*2))A

### FIXED CODE ###

A, B = map(int, input().split())
print(0 if A - (B*2) < 0 else A - (B*2))
def popcount(x):
    bitx = bin(x)
    cnt = bitx.count('1')
    return cnt

n = int(input())
x = list(input())
original_cnt = x.count('1')
stock = [1] * n

for i in range(1, n):
    stock[i] = stock[i - 1] * 2
stock.reverse()
original_num = 0
for i in range(n):
    original_num += int(x[i]) * stock[i]
cnt_one = original_cnt + 1
cnt_zero = original_cnt - 1

for i in range(n):
    tmp = original_num
    tmp2 = popcount
    if x[i] == '1':
        tmp -= stock[i]
        tmp2 = cnt_zero
    else:
        tmp += stock[i]
        tmp2 =cnt_one
    ans = 1
    num = tmp % tmp2
    while num != 0:
        ans += 1
        num %= popcount(num)
    print(ans)
    


### FIXED CODE ###

import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

def popcount(num):
    cnt = 0
    while num > 0:
        num %= bin(num).count('1')
        cnt += 1
    return cnt

def Main():
    n = read_int()
    x = read_str()
    x_int = int(x, 2)

    cnt = x.count('1')
    minus_cnt = cnt - 1
    plus_cnt = cnt + 1
    
    minus_mod = x_int % minus_cnt if minus_cnt != 0 else 0
    plus_mod = x_int % plus_cnt

    for i in range(n):
        if x[i] == '0':
            print(popcount((plus_mod + pow(2, n - 1 - i, plus_cnt)) % plus_cnt) + 1)
        elif minus_cnt != 0:
            print(popcount((minus_mod - pow(2, n - 1 - i, minus_cnt)) % minus_cnt) + 1)
        else:
            print(0)

if __name__ == '__main__':
    Main()
l = raw_input().split()
Q = int(l[0])
X = []
D = []
N = []
for i in range(Q):
    l = raw_input().split()
    X.append(int(l[0]))
    D.append(int(l[1]))
    N.append(int(l[2]))

# print(Q, X, D, N)

for i in range(Q):
    x = X[i]
    d = D[i]
    n = N[i]
    cds = [0]*n
    cds[0] = 1
    cds_tmp = [0]*n
    ans = x
    # print(x, d, n)
    for j in range(1, n):
        # print('j={}'.format(j), cds)
        for k in range(1, j + 1):
            # print(j, k, j + 1 - k)
            cds[j + 1 - k] += cds[j - k] * j
            cds[k] %= 1000003
     #       print(k, cds[k - 1], cds_tmp)
    # print(cds)
    ans = 0
    x_n = [x]*n
    for i in range(1, n):
        x_n[i] = x_n[i - 1] * x
        x_n[i] %= 1000003
    # print('x_n', x_n)
    d_n = 1
    for i in range(n):
        # print(x_n[n - i - 1], cds[i], d_n)
        ans += x_n[n - i - 1] * cds[i] * d_n
        d_n *= d
        d_n %= 1000003
        ans %= 1000003
    print(ans)


### FIXED CODE ###

mod = 10**6 + 3

fac = [0]*mod
fac[0] = 1
for i in range(1, mod):
    fac[i] = fac[i - 1] * i
    fac[i] %= mod

def gcd(a, b):
    def calc(a, b, first, second):
        if a < b:
            a, b = b, a
        r = a % b
        q = -(a - r)//b
        if r == 0:
            return b, second
        # first += q * second
        first = [x + q * y for (x, y) in zip(first, second)]
        return calc(r, b, second, first)
    first = [1, 0]
    second = [0, 1]
    return calc(a, b, first, second)

def mod_inv(n, mod):
    table = gcd(n, mod)
    inv = table[1][1]
    if inv < 0:
        inv += mod
    return inv

def mod_pow(x, n, mod):
    ret = 1
    while n > 0:
        if n & 1 != 0:
            ret *= x
            ret %= mod
        x *= x
        x %= mod
        n >>= 1
    return ret

l = input().split()
Q = int(l[0])
X = []
D = []
N = []
for i in range(Q):
    l = input().split()
    X.append(int(l[0]))
    D.append(int(l[1]))
    N.append(int(l[2]))


for i in range(Q):
    x = X[i]
    d = D[i]
    n = N[i]
    if d == 0:
        print(mod_pow(x, n, mod))
        continue
    inv_d = mod_inv(d, mod)
    x_d = (x * inv_d) % mod
    # print(i, x, d, n, inv_d, x_d, x_d + n - 1)
    if x_d + n - 1 >= mod:
        print(0)
        continue
    ans1 = fac[x_d + n - 1]
    ans2 = fac[x_d - 1]
    # print(ans1, ans2)
    ans = ans1 * mod_inv(ans2, mod)
    ans %= mod
    d_n = mod_pow(d, n, mod)
    ans = ans * d_n
    ans %= mod
    print(ans)
def dfs(G, v):
    seen[v] = True

    for nv in G[v]:
        if seen[nv]:
            continue
        point[nv] += point[v]
        dfs(G, nv)

n, q = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(n-1):
    x, y =  map(int, input().split())
    x, y = x-1, y-1
    G[x].append(y)
    G[y].append(x)

seen = [False for _ in range(n)]
root = 0
point = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split())
    point[p-1] += x

dfs(G, root)
print(*point)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)

def dfs(G, v):
    seen[v] = True

    for nv in G[v]:
        if seen[nv]:
            continue
        point[nv] += point[v]
        dfs(G, nv)

n, q = map(int, input().split())
G = [[] for _ in range(n)]
for _ in range(n-1):
    x, y =  map(int, input().split())
    x, y = x-1, y-1
    G[x].append(y)
    G[y].append(x)

seen = [False for _ in range(n)]
root = 0
point = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split())
    point[p-1] += x

dfs(G, root)
print(*point)
import numpy as np
h,w=map(int,input().split())
a=[np.array(input().split()) for i in range(h)]
a=np.array(a)

h_score=[0]*h
w_score=[0]*w
for i in range(h):
    for j in range(w):
        if a[i][j]=='.':
            h_score[j]+=1
            w_score[i]+=1

for i in range(h):
    if h_score[i]==h:
        a=np.delete(a,i,1)
for i in range(w):
    if w_score[i]==w:
        a=np.delete(a,i,0)

for i in a:#行列を入力と同じ形式で出力
    print(*i)

### FIXED CODE ###

h,w=map(int,input().split())
l=[list(input()) for i in range(h)]
#h+w回確認する
import numpy as np
change_h=0
change_w=w
ans=[]
for i in range(h):
    if l[i].count('.')!=w:
        ans+=[l[i][:]]
        change_h+=1
l_count=[]
for i in range(w):
    count=0
    for j in range(h):
        if l[j][i]=='.':
            count+=1
    
    if count==h:
        l_count+=[i]
    
l_count=sorted(l_count,reverse=True)#削除したい列番号を後ろから削除する
for i in l_count:
    ans=np.delete(ans,i,axis=1)
    #np.delete(a,b,c):a:配列,b:削除したい行・列番号,c:列か行か

#最後の出力
for i in range(change_h):
    print(*ans[i],sep='')
N = int(input()) 

list = []
for i in range(N) :
    a = input()
    list.append(a)

list.sort()

print(int(list[N-1])-int(list[0]))


### FIXED CODE ###

input()
a = sorted(map(int, input().split()))
print(a[-1]-a[0])
a, b = map(decimal, input().split())
b = b*1000
c = a*b
print(math.floor(c/1000))

### FIXED CODE ###

a,b = input().split()
a = int(a)
b = int(b.replace(".",""))
print(a*b//100)
n, m = map(int, input().split())
AB = [None] * n
maxa = 0
for i in range(n):
    AB[i] = tuple(map(int, input().split()))
    if AB[i][0] > maxa:
        maxa = AB[i][0]
AB.sort(key=lambda ab: -ab[1])
cands = [AB[i][1] for i in range(m - maxa)]
counts = [0] * m
for i in range(m - maxa, n):
    if AB[i][0] <= m and sum(counts[AB[i][0] - 1:]) <= m - AB[i][0]:
        cands.append(AB[i][1])
        counts[AB[i][0] - 1] += 1
print(sum(cands[:m]))


### FIXED CODE ###

import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)
n = int(input())
for i in range(3, n+1):
    x = i
    if x % 3 == 0:
        print(' {0}'.format(i), end=' ')
        continue

    while x != 0:
        if x % 10 == 3:
            print(' {0}'.format(i), end=' ')
            break
        x // = 10

print()

### FIXED CODE ###

n = int(input())
for i in range(3, n + 1):
  if i % 3 == 0 or i % 10 == 3 or '3' in str(i):
      print('',i,end='')
print()
inf = 1 << 28
N = int(raw_input())
p = [int(raw_input()) for i in xrange(N)]
to = [i for i in xrange(N+6)]
used = [False]*N
for i in xrange(N):
        if used[i]: continue
        seq = []
        while 1:
            used[i] = True            
            if i in seq:
                for j in seq: to[j] = 0
                break
            elif p[i] == 0:
                for j in seq: to[j] = i
                break
            else:
                seq.append(i)
                i += p[i]
dp = [inf]*(N+6)
dp[0] = 0
for i in xrange(N):
    for j in xrange(1,7):
        dp[to[i+j]] = min(dp[to[i+j]], dp[i]+1)
print dp[N-1]

### FIXED CODE ###

inf = 10**9
N = int(raw_input())
p = [int(raw_input()) for i in xrange(N)]
to = [i for i in xrange(N)]
for i in xrange(N):
        if to[i] != i: continue
        seq = set([])
        while 1:
            if to[i] != i:
                for j in seq: to[j] = to[i]
                break
            elif i in seq:
                for j in seq: to[j] = -1
                break
            elif p[i] == 0:
                for j in seq: to[j] = i
                break
            else:
                seq.add(i)
                i += p[i]

visited = [False]*N
visited[0] = True
que = [[0,0]]
while 1:
    cur,step = que.pop(0)
    if cur == N-1:
        print step
        break
    for nxt in range(cur+1,min(N,cur+7)):
        if to[nxt] > 0 and not visited[to[nxt]]:
            visited[to[nxt]] = True
            que.append([to[nxt], step+1])
class UnionFind:
    def __init__(self, n):
        self.par = list(range(n)) #親
        self.rank = [0] * n #根の深さ

    # xの属する根を求める
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            # if they have the same parent, no need to union
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # xとyが同じ集合に属するかを判定（同じ根に属するか）
    def same(self, x, y):
        return self.find(x) == self.find(y)


N, M = map(int, input().split())
uf = UnionFind(N)

for _ in range(M):
    a, b = map(int, input().split())
    uf.unite(a-1, b-1)

# from collections import Counter
# c = Counter(uf.par)

# print(max(c.values()))

cnt = [0] *n
for i in range(N):
    cnt[uf.par[i]] += 1
print(max(cnt))

### FIXED CODE ###

class UnionFind:
    # 作りたい要素数nで初期化
    # 使用するインスタンス変数の初期化
    def __init__(self, n):
        self.n = n
        # root[x]<0ならそのノードが根かつその値が木の要素数
        # rootノードでその木の要素数を記録する
        self.root = [-1]*(n+1)
        # 木をくっつける時にアンバランスにならないように調整する
        self.rank = [0]*(n+1)

    # ノードxのrootノードを見つける
    def findRoot(self, x):
        if(self.root[x] < 0): # 根
            return x
        else:
            # ここで代入しておくことで、後の繰り返しを避ける
            self.root[x] = self.findRoot(self.root[x])
            return self.root[x]
    # 木の併合、入力は併合したい各ノード
    def unite(self, x, y):
        # 入力ノードのrootノードを見つける
        x = self.findRoot(x)
        y = self.findRoot(y)
        # すでに同じ木に属していた場合
        if x == y:
            return 
        # 違う木に属していた場合rankを見てくっつける方を決める
        if self.rank[x] > self.rank[y]:
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            # rnkが同じ（深さに差がない場合）は1増やす
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1
    # xとyが同じグループに属するか判断
    def isSameGroup(self, x, y):
        return self.findRoot(x) == self.findRoot(y)

    # ノードxが属する木のサイズを返す
    def count(self, x):
        return -self.root[self.findRoot(x)]

n,m = map(int,input().split())
union = UnionFind(n)

for i in range(m):
    x, y = map(int,input().split())
    union.unite(x, y)

ans = 0

for i in range(n):
    ans = max(union.count(i), ans)

print(ans)
n = int(input())
min_r, max_d = int(input()), - 10**9
for _ in range(n - 1):
    r = int(input())
    max_d = max(max_d, r - min)
    min_r = min(min_r, r)
print(max_d)



### FIXED CODE ###

n = int(input())
min_r, max_d = int(input()), - 10**9
for _ in range(n - 1):
    r = int(input())
    max_d = max(max_d, r - min_r)
    min_r = min(min_r, r)
print(max_d)
from itertools import product
H, W, K = map(int, input().split())
c = [list(input()) for _ in range(H)]

ans = 0

for row_bit in product(range(2), repeat=H):
    for col_bit in product(range(2), repeat=H):
        count = 0
        for h in range(H):
            for w in range(W):
                if c[h][w] == "#" and (row_bit[h] == 0 and col_bit[w] == 0):
                    count += 1
        if count == K:
            ans += 1


print(ans)

### FIXED CODE ###

from itertools import product
H, W, K = map(int, input().split())
c = [list(input()) for _ in range(H)]

ans = 0

for row_bit in product(range(2), repeat=H):
    for col_bit in product(range(2), repeat=W):
        count = 0
        for h in range(H):
            for w in range(W):
                if c[h][w] == "#" and (row_bit[h] == 0 and col_bit[w] == 0):
                    count += 1
        if count == K:
            ans += 1


print(ans)
N = input()

if N%2 = 0:
  print(1/2)
else:
  print(((N+1)/2)/N)

### FIXED CODE ###

N = input()
N = int(N)

if N%2 == 0:
  print(1/2)
else:
  print(((N+1)/2)/N)
n,m = list(map(int, input().split(" ")))

A = list(map(int, input().split(" ")))
bc = []
for _ in range(m):
  b,c = list(map(int, input().split(" ")))
  bc.append((b,c))

count = 0
bc = sorted(bc,key=lambda x:x(1),reverse = True)
big_list = []
for b,c in bc:
  big_list += [c for _ in range(b)]
  count += b
  if count >= n:
    break
print(sum( sorted((A+big_list),reverse=True)[:n] ))

### FIXED CODE ###

n,m = list(map(int, input().split(" ")))

A = list(map(int, input().split(" ")))
bc = []
for _ in range(m):
  b,c = list(map(int, input().split(" ")))
  bc.append((b,c))

count = 0
bc = sorted(bc,key=lambda x:x[1],reverse = True)
big_list = []
for b,c in bc:
  big_list += [c for _ in range(b)]
  count += b
  if count >= n:
    break
print(sum( sorted((A+big_list),reverse=True)[:n] ))
A, B, K = map(int, input().split())

import sympy as sym
list_A = sym.divisors(A)
list_B = sym.divisors(B)

list_and = set(list_A) & set(list_B)

list_and_sort = sorted(list_and, reverse=True)

print(list_and_sort[K-1])

### FIXED CODE ###

A, B, K = map(int, input().split())

def make_divisor_list(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list

list_A = make_divisor_list(A)
list_B = make_divisor_list(B)

list_and = set(list_A) & set(list_B)

list_and_sort = sorted(list_and, reverse=True)

print(list_and_sort[K-1])
n = int(input())
mod = 10 ** 9 + 7
import numpy as np
a = np.array(map(int, input().split())
s=0
ss=0
for i in range(n):
    ss += a[i]%mod
    s += ss * a[i] % mod
print(s)

### FIXED CODE ###

n = int(input())
mod = 10 ** 9 + 7
import numpy as np
a = list(map(int, input().split()))
s=0
ss=0
for i in range(n):
    if i > 0:
        ss += a[i-1]%mod
    s += ss * a[i] % mod
print(s%mod)
n,k=map(int,input().split())
lst1=input()
lst2=lst1.split(" ")
lst=[]
for i in lst2:
    lst.append(int(i))

minr=[]
minl=[]

for i in range(n-k+1):
    l=i
    r=i+k-1
    minr.sppend(abs(lst[r])+abs(lst[r]-lst[l]))
    minl.append(abs(lst[l])+abs(lst[r]-lst[l]))

minr.sort()
minl.sort()

m=min(minr[0],minl[0])
print(m)

### FIXED CODE ###

n,k=map(int,input().split())
lst1=input()
lst2=lst1.split(" ")
lst=[]
for i in lst2:
    lst.append(int(i))

minr=[]
minl=[]

for i in range(n-k+1):
    l=i
    r=i+k-1
    minr.append(abs(lst[r])+abs(lst[r]-lst[l]))
    minl.append(abs(lst[l])+abs(lst[r]-lst[l]))

minr.sort()
minl.sort()

m=min(minr[0],minl[0])
print(m)
def main():

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

    zzz=[]
    oioi=0
    flg=True

    for i in range(n):
        oioi = a[oioi]-1
        
        if oioi == 0:
            zzz.append(oioi)
            break
        if oioi in zzz:
            flg=False
            ppp=zzz.index(oioi)
            del zzz[0:ppp+1]
            zzz.append(oioi)
            break
    
    if flg == False:
        print(zzz[(k+ppp)%len(zzz)-1]+1)
    else:
        print(zzz[k%len(zzz)-1]+1)

    
if __name__ == '__main__':
    main()

### FIXED CODE ###

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

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

p = l.index(l[n])

if k <= n:
    print(l[k])
else:
    x = (k - n + 1) % (n - p)
    if x == 0:
        print(l[n - 1])
    else:
        print(l[x + p - 1])