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

from itertools import accumulate
N, M = map(int, input().split())
A = list(map(int, input().split()))

B = [0] + A
B = list(accumulate(B))

ans = []
cnt = 0
for i in range(1, len(B)):
    for j in range(i):
        if (B[i] - B[j])%M == 0:
            cnt += 1
print(cnt)

### FIXED CODE ###

from itertools import accumulate
N, M = map(int, input().split())
A = list(map(int, input().split()))

B = [0] + A
C = {}
B = list(accumulate(B))
for i in range(1, len(B)):
    if B[i]%M not in C:
        C[B[i]%M] = 1
    else:
        C[B[i]%M] += 1
ans = 0
for i in C.values():
    ans += i*(i-1)//2
if 0 in C:
    ans += C[0]
print(ans)
### BUGGY CODE ###

from sys import stdin

def main():
    n, k = map(int, stdin.readline().rstrip().split())
    a = [int(x)-1 for x in stdin.readline().rstrip().split()]
    n = a[0]
    his = [-1, n]
    f = his.append
    while True:
        n = a[n]
        if n in his:
            f(n)
            break
        f(n)
    idx = his.index(his[-1])
    loop = len(his[his.index(his[-1]):-1])
    if len(his) > k:
        print(his[k]+1)
    else:
        t = (k - idx) % loop
        g = his[idx:-1]
        print(g[t]+1)
main()

### FIXED CODE ###

from sys import stdin

def main():
    n, k = map(int, stdin.readline().rstrip().split())
    a = [int(x)-1 for x in stdin.readline().rstrip().split()]
    n = a[0]
    his = [-1, n]
    f = his.append
    s = set()
    while True:
        n = a[n]
        if n in s:
            f(n)
            break
        f(n)
        s.add(n)
    idx = his.index(his[-1])
    loop = len(his[his.index(his[-1]):-1])
    if len(his) > k:
        print(his[k]+1)
    else:
        t = (k - idx) % loop
        g = his[idx:-1]
        print(g[t]+1)
main()
### BUGGY CODE ###

from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
p, q, r = cnt[1], cnt[2], cnt[3]

dp = []
for _ in range(n + 1):
    dp.append([[0] * (r + 1) for _ in range(r + q + 1)])
dp[0][0][0] = 0

for k in range(r + 1):
    for j in range(r + q + 1):
        for i in range(n + 1):
            if (i + j + k == 0) or (i + j + k) > n:
                continue
            tmp=0
            if i != 0:
                tmp += dp[i - 1][j][k] * i
            if (j != 0) and (i != n):
                tmp += dp[i + 1][j - 1][k] * j
            if (k != 0) and (j != r + q):
                tmp += dp[i][j + 1][k - 1] * k
            tmp += n
            dp[i][j][k] = tmp / (i + j + k)

print(dp[p][q][r])


### FIXED CODE ###

import sys,queue,math,copy,itertools,bisect,collections,heapq
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())

N = NI()
a = [0,0,0]
for x in LI():
    a[x-1] += 1
dp = [[[0.0] * (N+1) for _ in range(N+1)] for _ in range(N+1)]

for k in range(a[2]+1):
    for j in range(a[1]+a[2]+1):
        for i in range(N+1):
            if not (0 < i+j+k <= N): continue
            x = 0.0
            if i > 0: x += dp[i-1][j][k] * (i/N)
            if j > 0: x += dp[i+1][j-1][k] * (j/N)
            if k > 0: x += dp[i][j+1][k-1] * (k/N)
            x += 1.0
            x *= N / (i+j+k)
            dp[i][j][k] = x
print(dp[a[0]][a[1]][a[2]])
### BUGGY CODE ###

#!/usr/bin/env python3
#ABC139 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [deque() for _ in range(n)]
for i in range(1,n+1):
    x = LI()
    for j in x:
        a[i-1].append((i,j))
ans = 0
for j in range(n**2):
    check = [1]*n
    flg = False
    for i in range(n):
        if not check[i]:
            continue
        if len(a[i]) == 0:
            check[i] = 0
            continue
        (x,y) = a[i].popleft()
        check[i] = 0
        if a[y-1][0] == (y,x) and check[y-1]:
            flg = True
            check[y-1] = 0
            a[y-1].popleft()
        else:
            a[i].appendleft((x,y))
    if flg:
        ans += 1
for i in a:
    if len(i) != 0:
        print(-1)
        quit()
print(ans)


### FIXED CODE ###

#!/usr/bin/env python3
#ABC139 E

import sys
import math
import bisect
import time
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [deque() for _ in range(n)]
for i in range(1,n+1):
    x = LI()
    for j in x:
        a[i-1].append((i,j))
ans = 0
s = time.time()
for j in range(n*(n-1)//2+1):
    check = [1]*n
    flg = False
    flg2 = False
    if time.time() - s > 1.6:
        print(n*(n-1)//2)
        quit()
    for i in range(n):
        if len(a[i]) == 0 or check[i] == 0:
            continue
        (x,y) = a[i].popleft()
        flg2 = True
        check[i] = 0
        if a[y-1][0] == (y,x) and check[y-1]:
            a[y-1].popleft()
            flg = True
            check[y-1] = 0
        else:
            a[i].appendleft((x,y))
    if flg:
        ans += 1
    if not flg:
        for i in a:
            if len(i) != 0:
                print(-1)
                quit()
    if not flg2:
        print(ans)
        quit()
### BUGGY CODE ###

N = int(input())
for h in range(1,3501):
    for n in range(1,3501):
        d = 4*h*n-N*h-N*n
        if d == 0 or N*h*n%d != 0: continue
        w = N*h*n//d
        if w < 0: continue
        print(h,n,w)
        exit()

### FIXED CODE ###

N = int(input())
for h in range(1,3501):
    for n in range(1,3501):
        d = 4*h*n-N*h-N*n
        if d == 0 or N*h*n%d != 0: continue
        w = N*h*n//d
        if w < 0: continue
        print(h,n,w)
        exit()
### BUGGY CODE ###

n, m = map(int, input().split())
ab_lis =[]
for i in range(m):
    a,b = map(int, input().split())
    ab_lis.append([b,a])
ab_lis.sort()
count = 0
bridge = [1]*(n-1)
for lis in ab_lis:
    b = lis[0]
    a = lis[1]
    if sum(bridge[a-1:b-1])/len(bridge[a-1:b-1]) == 1 and b >= 2:
        bridge[b-2] = 0
        count += 1
print(count)

### FIXED CODE ###

n, m = map(int, input().split())
ab_lis =[]
for i in range(m):
    a,b = map(int, input().split())
    ab_lis.append([b,a])
ab_lis.sort()
count = 0
x = 0
for lis in ab_lis:
    b = lis[0]
    a = lis[1]
    if a <= x:
        continue
    x = b-1
    count+=1
print(count)
### BUGGY CODE ###

import numpy as np

n=int(input())
E=[]
W=[]
node_l=np.zeros(n)
node_l[0]=1
for i in range(n-1):
    u,v,w=map(int,input().split())
    #print(u,v,w)
    E.append([u,v])
    W.append(w)
#print(E,W)

def bfs(node,node_l,l,w_l):
    i=0
    for edge in l:
        if edge[0]==node:
            if node_l[edge[1]-1]==0:
                if W[i]%2==0:
                    node_l[edge[1]-1]=node_l[node-1]
                    bfs(edge[1],node_l,l,w_l)
                else:
                    node_l[edge[1]-1]=node_l[node-1]*-1
                    bfs(edge[1],node_l,l,w_l)
        elif edge[1]==node:
            if node_l[edge[0]-1]==0:
                if W[i]%2==0:
                    node_l[edge[0]-1]=node_l[node-1]
                    bfs(edge[0],node_l,l,w_l)
                else:
                    node_l[edge[0]-1]=node_l[node-1]*-1
                    bfs(edge[0],node_l,l,w_l)
        i+=1
    return


bfs(1,node_l,E,W)
for i in range(n):
    if node_l[i]==1:
        print('1',end='\n')
    else:
        print('0',end='\n')

### FIXED CODE ###

import numpy as np

n=int(input())
E=[]
node_l=np.zeros(n)
node_l[0]=1
for i in range(n-1):
    u,v,w=map(int,input().split())
    #print(u,v,w)
    E.append((u,v,w))
#print(E,W)

def bfs(node,l):
    root=[[] for i in range(n)]
    for a,b,c in l:
        root[a-1]+=[(b-1,c)]
        root[b-1]+=[(a-1,c)]
    stack=[]
    stack.append(node-1)
    #print(root)
    dist=[-1]*n
    dist[node-1]=0
    while stack:
        #print(stack)
        buf=stack.pop(-1)
        for v,weight in root[buf]:
            if dist[v]==-1:
                dist[v]=dist[buf]+weight
                stack.append(v)
    return dist

for i in bfs(1,E):
    print(i%2,end='\n')
### BUGGY CODE ###

n = int(input())
s = [input() for i in range(n)]

d = {}
for i in range(n):
    if s[i] in d:
        d[s[i]] += 1
    else:
        d[s[i]] = 1

ma = [m[0] for m in d.items() if m[1] == max(d.values())]
print(*sorted(ma))

### FIXED CODE ###

from collections import Counter
n = int(input())
s = [input() for i in range(n)]

d = Counter(s).most_common()

s = d[0][1]
ans = []
for k, v in d:
    if s == v:
        ans.append(k)
    else:
        break
print(*sorted(ans), sep="\n")
### BUGGY CODE ###

S = list(input())
N = len(S)
child = [1] * N
memo = []

# RLのインデックスを取得する
for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        memo.append(i)
        memo.append(i+1)

count = 0
for i in range(N):
    if i not in memo:
        if S[i] == "R":
            if (memo[count] - i) % 2 == 0:
                child[memo[count]] += 1
                child[i] -= 1
            else:
                child[memo[count+1]] += 1
                child[i] -= 1
        elif S[i] == "L":
            if (i - memo[count-1]) % 2 == 0:
                child[memo[count-1]] += 1
                child[i] -= 1
            else:
                child[memo[count-2]] += 1
                child[i] -= 1
    else:
        count += 1

ans = [str(n) for n in child]
ans = " ".join(ans)
print(ans)

### FIXED CODE ###

S = list(input())
N = len(S)
child = [1] * N
memo = []

# RLのインデックスを取得する
for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        memo.append(i)
        memo.append(i+1)
memo.append("dummy")
count = 0
for i in range(N):
    if i != memo[count]:
        if S[i] == "R":
            if (memo[count] - i) % 2 == 0:
                child[memo[count]] += 1
                child[i] -= 1
            else:
                child[memo[count+1]] += 1
                child[i] -= 1
        elif S[i] == "L":
            if (i - memo[count-1]) % 2 == 0:
                child[memo[count-1]] += 1
                child[i] -= 1
            else:
                child[memo[count-2]] += 1
                child[i] -= 1
    else:
        count += 1

ans = [str(n) for n in child]
ans = " ".join(ans)
print(ans)
### BUGGY CODE ###

N = int(input())
arr = list(map(int, input().split()))
result = 0
for i in range(N):
    if result == 0:
        result += arr[i]
    else:
        result *= arr[i]
if result > 10**18:
    print(-1)
else:
    print(result)

### FIXED CODE ###

N = int(input())
arr = list(map(int, input().split()))
result = 1
if 0 in arr:
    result = 0
else:
    for i in arr:
        result *= i
        if result > 10**18:
            result = -1
            break
print(result)
### BUGGY CODE ###

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

dp = [0] * N
for i in range(0, N):
    if i // K == 0:
        bef = i
    else:
        bef = K
    for j in range(1, bef + 1):
        tmp = (dp[i - j] + abs(h[i] - h[i - j]))
        if tmp < dp[i] or dp[i] == 0:
            dp[i] = tmp

print(dp[-1])

### FIXED CODE ###

N,K = map(int, input().split())
h = list(map(int, input().split()))
 
dp = [0] * N
for i in range(1, N):
    if i // K == 0:
        bef = i
    else:
        bef = K
    tmp = []
    for j in range(1, bef + 1):
        tmp.append(dp[i - j] + abs(h[i] - h[i - j]))
    dp[i] = min(tmp)
 
print(dp[-1])
### BUGGY CODE ###

def main():
    mod = 10 ** 9 + 7

    N, M = map(int, input().split())
    S = input().split()
    T = input().split()

    dp = [[0] * (M + 1) for _ in range(N + 1)]
    dp[0][0] = 1

    for i in range(N + 1):
        for j in range(M + 1):
            if 1 <= i and 1 <= j and S[i - 1] == T[j - 1]:
                dp[i][j] += dp[i - 1][j - 1]
            if 1 <= i:
                dp[i][j] += dp[i - 1][j]
            if 1 <= j:
                dp[i][j] += dp[i][j - 1]
            if 1 <= i and 1 <= j:
                dp[i][j] -= dp[i - 1][j - 1]
            dp[i][j] %= mod

    print(dp[N][M])


if __name__ == '__main__':
    main()


### FIXED CODE ###

mod = 10 ** 9 + 7

N, M = map(int, input().split())
S = input().split()
T = input().split()

dp = [[0] * (M + 1) for _ in range(N + 1)]
dp[0][0] = 1

for i in range(N + 1):
    for j in range(M + 1):
        if 1 <= i and 1 <= j and S[i - 1] == T[j - 1]:
            dp[i][j] += dp[i - 1][j - 1]
        if 1 <= i:
            dp[i][j] += dp[i - 1][j]
        if 1 <= j:
            dp[i][j] += dp[i][j - 1]
        if 1 <= i and 1 <= j:
            dp[i][j] -= dp[i - 1][j - 1]
        dp[i][j] %= mod

print(dp[N][M])
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = a[0]
for i in range(n-1):
    ans *= a[i+1]
if ans > 10**18:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = a[0]
for i in range(n-1):
    ans *= a[i+1]
    if ans > 10**18:
        break
if 0 in a:
    print(0)
elif ans > 10**18:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

a,b=map(int,input().split())
a=input()
c=[0]*len(a)
for i in range(len(a)-1):
    if (a[i]+a[i+1])=="AC":c[i]=1
for i in range(b):
    d,e=map(int,input().split())
    print(c[d-1:e-1].count(1))

### FIXED CODE ###

a,b=map(int,input().split())
a=input()
c=[0]*len(a)
for i in range(len(a)-1):
    if (a[i]+a[i+1])=="AC":c[i+1]=c[i]+1
    else:c[i+1]=c[i]
for i in range(b):
    d,e=map(int,input().split())
    print(c[e-1]-c[d-1])
### BUGGY CODE ###

from sys import stdin
N,M = [int(x) for x in stdin.readline().rstrip().split()]
data = [0] * M
for _ in range(M):
    data[_] = (int(stdin.readline().rstrip()))
 
ans = [1] + [0] * (N)
if 1 in data != True:
    ans[1] = 1
for i in range(1,N + 1):
    if i in data:
      ans[i] = 0
    else:
      ans[i] = ans[i-1] + ans[i-2]
print(ans[-1] % 1000000007)

### FIXED CODE ###

from sys import stdin
N,M = [int(x) for x in stdin.readline().rstrip().split()]
data = [0] * M
for _ in range(M):
    data[_] = (int(stdin.readline().rstrip()))
 
ans = [1] + [0] * (N)
for i in (set(range(1,N+1)) - set(data)):
    ans[i] = ans[i-1] + ans[i-2]
print(ans[-1] % 1000000007)
### BUGGY CODE ###

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

dp = [float('inf')] * (1 << N)
dp[0] = 0

u = 1 << N
for i in range(M):
    a, b = map(int, input().split())
    c = sum(1 << (x - 1) for x in map(int, input().split()))

    for j in range(u):
        dp[c | j] = min(dp[c | j], dp[j] + a)


if dp[-1] != float('inf'):
    print(dp[-1])
else:
    print(-1)


### FIXED CODE ###

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

dp = [float('inf')] * (1 << N)
dp[0] = 0

u = 1 << N
for i in range(M):
    a, b = map(int, input().split())
    c = sum(1 << (x - 1) for x in map(int, input().split()))

    for j in range(u):
        dp[c | j] = min(dp[c | j], dp[j] + a)


if dp[-1] != float('inf'):
    print(dp[-1])
else:
    print(-1)
### BUGGY CODE ###

N = int(input())

power = 1
for i in range(1,N+1):
    power *= i
    
print(power % (10**9+7))

### FIXED CODE ###

N = int(input())
 
power = 1
for i in range(1,N+1):
    power *= i
    if power >= 10**9+7:
        power %= 10**9+7
print(power % (10**9+7))
### BUGGY CODE ###

import sys
input = sys.stdin.readline
from collections import defaultdict
from functools import reduce
N = int(input())
A = list(map(int,input().split()))

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

def primeFactorization(n):
    res = defaultdict(int)
    m = int(n**0.5)
    for i in range(2,m+1):
        while n % i == 0:
            n = n//i
            res[i] += 1
    if n > 1:
        res[n] = 1
    return res

pset = set()
for a in A:
    pf = primeFactorization(a).keys()
    if pset.intersection(pf):
        break
    pset = pset.union(pf)
else:
    print('pairwise coprime')
    exit()

allgcd = reduce(gcd, A)
if allgcd == 1:
    print('setwise coprime')
    exit()

print('not coprime')


### FIXED CODE ###

import sys
input = sys.stdin.readline
from collections import defaultdict
from functools import reduce
N = int(input())
A = list(map(int,input().split()))

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

def sieve_Eratosthenes(n):
    f = [True]*(n+1)
    f[0] = False
    f[1] = False
    m = int(n**0.5+1)
    for i in range(2,m):
        if f[i]:
            for j in range(i*i,n+1, i):
                f[j] = False
    return f

def getPrimeLists(n):
    table = sieve_Eratosthenes(n)
    return [x for x in range(2,n+1) if table[x]]

def primeFactorization(n):
    res = defaultdict(int)
    m = int(n**0.5)
    for i in range(2,m+1):
        while n % i == 0:
            n = n//i
            res[i] += 1
    if n > 1:
        res[n] = 1
    return res

table = [0] * (10**6+10)
f = False
for a in A:
    pf = primeFactorization(a).keys()
    for p in pf:
        if table[p] > 0:
            f = True
        table[p] += 1
    if f:
        break
else:
    print('pairwise coprime')
    exit()

allgcd = reduce(gcd, A)
if allgcd == 1:
    print('setwise coprime')
    exit()

print('not coprime')
### BUGGY CODE ###

n, m = map(int, input().split())
ship = []
for i in range(m):
    ship.append(tuple(map(int, input().split())))

for i in range(m):
    if ship[i][0] == 1:
        for j in range(m):
            if ship[i][1] == ship[j][0] and ship[j][1] == n or ship[i][1] == ship[j][1] and ship[j][0] == n:
                print("POSSIBLE")
                exit()
    elif ship[i][1] == 1:
        for j in range(m):
            if ship[i][0] == ship[j][0] and ship[j][1] == n or ship[i][0] == ship[j][1] and ship[j][0] == n:
                print("POSSIBLE")
                exit()

print("IMPOSSIBLE")


### FIXED CODE ###

n, m = map(int, input().split())
ship_1 = [False]*(n+1)
ship_n = [False]*(n+1)
for i in range(m):
    a, b = map(int, input().split())
    if a == 1:
        ship_1[b] = True
    elif b == n:
        ship_n[a] = True

for i in range(len(ship_1)):
    if ship_1[i] and ship_n[i]:
        print("POSSIBLE")
        exit()

print("IMPOSSIBLE")
### BUGGY CODE ###

def main():
    S = input()[::-1]
    N = len(S)
    mod = 10 ** 9 + 7

    dp = [[0] * 13 for _ in range(N + 5)]
    dp[0][0] = 1

    digit = 1
    for i, s in enumerate(S):
        if s == "?":
            num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        else:
            num = [int(s)]

        ni = i + 1
        for j in range(13):
            for k in num:
                nj = (j + k * digit) % 13
                dp[ni][nj] += dp[i][j]
                dp[ni][nj] %= mod

        digit *= 10
        digit %= 13

    return dp[N][5]


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

### FIXED CODE ###

S = input()
mod = 10 ** 9 + 7

dp = [0]*13
dp[0] = 1

mul = 1
for i, s in enumerate(S[::-1]):
    new = [0]*13
    if s == "?":
        for x in range(13):
            for y in range(10):
                next_x = y * mul + x
                new[next_x % 13] += dp[x]
                new[next_x % 13] %= mod
    else:
        for x in range(13):
            next_x = int(s) * mul + x
            new[next_x % 13] = dp[x]

    dp = new
    mul *= 10
    mul %= 13

ans = dp[5] % mod
print(ans)
### BUGGY CODE ###

import time

t0 = time.clock()

N = int(input())
mat = []
for i in range(N):
    mat.append(list(map(lambda x:int(x)-1, input().split())))

ind = [0 for i in range(N)]
days = 0
while True:
    if time.clock() - t0 > 1.5e+6:
        print(N * (N - 1) // 2)
        
    madeMatch = False
    
    played = [False for i in range(N)]
    for i in range(N):
        if ind[i] == N-1 or played[i]: continue
        playAgainst = mat[i][ind[i]]
        if mat[playAgainst][ind[playAgainst]] == i and not played[playAgainst]:
            madeMatch = True
            played[i] = True
            played[playAgainst] = True

            ind[playAgainst] += 1
            ind[i] += 1
    if not madeMatch: break
    days += 1
    
if all(x == N-1 for x in ind):
    print(days)
else:
    print(-1)


### FIXED CODE ###

import time

t0 = time.clock()

N = int(input())
mat = []
for i in range(N):
    mat.append(list(map(lambda x:int(x)-1, input().split())))

ind = [0 for i in range(N)]
days = 0
while True:
    if time.clock() - t0 > 1:
        print(N * (N - 1) // 2)
        quit()
        
    madeMatch = False
    
    played = [False for i in range(N)]
    for i in range(N):
        if ind[i] == N-1 or played[i]: continue
        playAgainst = mat[i][ind[i]]
        if mat[playAgainst][ind[playAgainst]] == i and not played[playAgainst]:
            madeMatch = True
            played[i] = True
            played[playAgainst] = True

            ind[playAgainst] += 1
            ind[i] += 1
    if not madeMatch: break
    days += 1
    
if all(x == N-1 for x in ind):
    print(days)
else:
    print(-1)
### BUGGY CODE ###

A,B,C,D = map(int,input().split())

cList = []

dList = []

numList = []

supC = (B // C)
infC = (A // C)

supD = (B // D)
infD = (A // D)

for i in range(A,B+1):
	numList.append(i)


for i in range(infC,supC+1):
	cList.append(i * C)

for i in range(infD,supD+1):
	dList.append(i * D)
	
numList = len(set(numList) - set(cList) - set(dList))

print(numList)

### FIXED CODE ###

A,B,C,D = map(int,input().split())

def gcd(m,n):
	while n!=0:
		m,n = n,m % n
	return m

def lcm(m,n):
	return m * n // gcd(m,n)

CD = lcm(C,D)

supC = B // C
supD = B // D
supCD = B // (CD)

infC = (A-1) // C
infD = (A-1) // D
infCD = (A-1) // (CD)

num = B - A + 1
num = num - supC - supD + supCD + infC + infD - infCD
print(num)
### BUGGY CODE ###

N=int(input())
P=list(map(int,input().split()))
A=[[x if 0<=x<N*N else -1 for x in (i+1,i+N,i-1,i-N)] for i in range(N*N)]
X=[min(i//N,N-1-i//N,i%N,N-1-i%N) for i in range(N*N)]
E=[1]*N*N
r=0
from collections import deque
q=deque()
for p in P:
    p-=1
    r+=X[p]
    E[p]=0
    q.append(p)
    while q:
        i=q.popleft()
        for j in A[i]:
            if 0<=j and X[i]+E[i]<X[j]:
                X[j]=X[i]+E[i]
                q.append(j)
print(r)

### FIXED CODE ###

def main(N,P):
    ad=(1,N,-1,-N)
    X=[0]*N*N
    for i in range(N*N):
        X[i]=min(i//N,N-1-i//N,i%N,N-1-i%N)
    E=[1]*N*N
    a=0
    q=[0]*N*N*N
    l,r=0,0
    for p in P:
        p-=1
        a+=X[p]
        E[p]=0
        q[r]=p
        r+=1
        while l<r:
            i=q[l]
            l+=1
            for j in ad:
                if 0<=i+j<N*N and X[i]+E[i]<X[i+j]:
                    X[i+j]=X[i]+E[i]
                    q[r]=i+j
                    r+=1
    return a
import sys
import numpy as np
readline=sys.stdin.buffer.readline
read=sys.stdin.buffer.read
if sys.argv[-1]=='ONLINE_JUDGE':
    from numba import*
    from numba.pycc import CC
    cc=CC('my_module')
    def cc_export(f,signature):
        cc.export(f.__name__,signature)(f)
        return njit(f)
    main=cc_export(main,(i8,i8[:],))
    cc.compile()
    exit()
N=int(readline())
P=np.array(readline().split(),dtype=np.int64)
from my_module import main
print(main(N,P))
### BUGGY CODE ###

N, K = [int(x) for x in input().split()]
dest = [int(x) for x in input().split()]
route = [1]
t = 1
while True:
  if dest[t-1] not in route:
    route.append(dest[t-1])
    t = dest[t-1]
  else:
    loop = route.index(dest[t-1])
    L_loop = len(route) - loop
    break
ans = 0
if K < len(route):
  ans = route[K]
else:
  K = K - len(route)
  ans = route[loop + (K % L_loop)]
print(ans)

### FIXED CODE ###

N, K = [int(x) for x in input().split()]
dest = [int(x) for x in input().split()]
route = [1]
flag = [1] + [0] * (N-1)
t = 1
while True:
  if flag[dest[t-1]-1] == 0:
    t = dest[t-1]
    route.append(t)
    flag[t-1] = 1
  else:
    loop = route.index(dest[t-1])
    L_loop = len(route) - loop
    break
  #print(route)
  #print(flag)
ans = 0
if K < len(route):
  ans = route[K]
else:
  K = K - len(route)
  ans = route[loop + (K % L_loop)]
print(ans)