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

n,k=map(int,input().split())
a=sorted([list(map(int,input().split())) for i in range(n)],reverse=True,key=lambda x: x[1])
type=set([a[i][0] for i in range(k)])
dsum=sum([x[1] for x in a[:k]])
tnum={i:[x[0] for x in a[:k]].count(i) for i in type}
ans=dsum+len(type)**2
sushi=a[-(n-k+1)::-1]
for i in a[k:]:
    if i[0] not in type:
        type.add(i[0])
        for x in sushi:
            if tnum[x[0]]>1:
                tnum[x[0]]-=1
                sushi.remove(x)
                dsum=dsum+i[1]-x[1]
                ans=max(ans,dsum+len(type)**2)
                break
print(ans)




### FIXED CODE ###

n, k = map(int, input().split())
td = sorted([list(map(int, input().split())) for i in range(n)], reverse=True, key=lambda x: x[1])

type = set()
L = []
Sum = 0
for x in td[:k]:
    Sum += x[1]
    if x[0] not in type:
        type.add(x[0])
    else:
        L.append(x[1])

L = L[::-1]
type_cnt = len(type)
ans = Sum + type_cnt ** 2
for x in td[k:]:
    if len(L)==0:break
    if x[0] not in type:
        type.add(x[0])
        type_cnt += 1
        Sum = Sum - L.pop(0) + x[1]
        ans = max(ans, Sum + type_cnt ** 2)
print(ans)
### BUGGY CODE ###

INT_MAX = 10**8
h, n = map(int, input().split())
e = tuple(tuple(map(int, input().split())) for _ in range(n))

dp = [0] + [INT_MAX]* h
for i in range(1, h+1):
  v = INT_MAX
  for a, b in e:
    c = dp[max(0, i - a)] + b
    v = min(v, c)
  dp[i] = v
print(dp[h])

### FIXED CODE ###

INT_MAX = 10**8
h, n = map(int, input().split())
e = tuple(tuple(map(int, input().split())) for _ in range(n))

dp = [INT_MAX] * (h+1)
dp[0] = 0
for i in range(1, h+1):
  for a, b in e:
    c = dp[max(0, i - a)] + b
    dp[i] = min(dp[i], c)

print(dp[h])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)
    b.append(B)'''

def yakusuu(n):
    #約数リスト生成,バラバラに出るので.sort()を使う必要あるかも
    #PyPy推奨
    yakusuu_list = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            yakusuu_list.append(i)
            if i != n // i:
                yakusuu_list.append(n//i)
    return yakusuu_list

def ok(wa,waaa,item,bl):
    m=yakusuu(item)
    #print(m)
    if waaa[item-1]==True:
        bl.append(item-1)
        return

    waaa[item-1]=True

    for jtem in m:
        if jtem!=item and waaa[jtem-1]==True:
            bl.append(item-1)
            return

    waaa[item-1]=True
    #print(item,waaa[:25])
    return

def main():
    n=int(input())
    a=list(map(int, input().split()))
    #print(sorted(a))
    a.sort()
    wa=[a[0]]
    waaa=[False]*(a[-1])
    bl=[]

    for item in a:
        ok(wa,waaa,item,bl)

    #import collections
    print(waaa.count(True)-len(set(bl)))
    return
    #print(max(0,len(wa)-len(set(bl))))

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

#n = int(input())
#l = list(map(int, input().split()))

'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''
def cal(n,li):
    lis=[True]*(n+1)
    for item in li:
        if lis[item]:
            for i in range(item*2,n+1,item):
                lis[i]=False
    return lis
from collections import Counter

n=int(input())
a=list(map(int,input().split()))
a.sort()
l=cal(a[-1],a)
ll=Counter(a)
#print(l,ll)
ans=0
for i in range(n):
    if ll[a[i]]==1:
        if l[a[i]]:
            ans+=1

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

import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, M = mi()
Q = li2(M)
Q = sorted(Q, key=lambda x:x[1])

table = dp2(0, N, N)

#for i in range(M):
    #s, t = mi()
    #for i in range(t):
    #table[s-1][t-1] = 1

#for i in reversed(range(N-1)):
    #for j in range(i, N):
        #table[i][j] = max(table[i+1][j], table[i][j])

#print(table)
'''
i = j = 0
cnt = 0
while True:
    while j < N and table[i][j] == 0:
        j += 1
    if j >= N:
        break
    cnt += 1
    i = j

print(cnt)
'''

l = [N]*N
for i in range(M):
    ind = max(0, Q[i][0]-1)
    l[ind] = min(Q[i][1]-1, l[ind])

for i in reversed(range(N-1)):
    l[i] = min(l[i], l[i+1])

cnt = 0
i = 0
#print(l)
while True:
    i = l[i]
    cnt += 1
    if l[i] == N:
        break

print(cnt)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, M = mi()
Q = li2(M)
#Q = sorted(Q, key=lambda x:x[1])

l = [N]*N
for i in range(M):
    ind = max(0, Q[i][0]-1)
    l[ind] = min(Q[i][1]-1, l[ind])

for i in reversed(range(N-1)):
    l[i] = min(l[i], l[i+1])

cnt = 0
i = 0
#print(l)
while True:
    i = l[i]
    cnt += 1
    if l[i] == N:
        break

print(cnt)
### BUGGY CODE ###

from itertools import product

H, W = map(int, input().split())
A = []
black = []
for i in range(H):
    tmp = input()
    A.append(tmp)
    black += [(i, j) for j in range(W) if tmp[j] == '#']
ans = 0
for i, j in product(range(H), range(W)):
    if A[i][j] == '.':
        dist_bl = min([abs(b[0] - i) + abs(b[1] - j) for b in black])
        ans = max(ans, dist_bl)
print(ans)

### FIXED CODE ###

import numpy as np
from itertools import product
from collections import deque

H, W = map(int, input().split())
val = []
INF = 10**9
for i in range(H):
    tmp = input()
    val.append([0 if tmp[j] == '#' else INF for j in range(W)])
val = np.array(val)

for i in range(H-1):
    val[i+1, :] = np.minimum(val[i, :]+1, val[i+1, :])
for i in range(H-1, 0, -1):
    val[i-1, :] = np.minimum(val[i, :]+1, val[i-1, :])
for j in range(W-1):
    val[:, j+1] = np.minimum(val[:, j]+1, val[:, j+1])
for j in range(W-1, 0, -1):
    val[:, j-1] = np.minimum(val[:, j]+1, val[:, j-1])

print(val.max())
### BUGGY CODE ###

N,K,Q = map(int,input().split())
li = [int(input()) for i in range(Q)]
ans = [K]*N
for i in range(Q):
    for j in range(N):
        if li[i]-1 != j:
            ans[j] -= 1
for i in range(N):
    if ans[i] <= 0:
        ans[i] = 'No'
    else:
        ans[i] = 'Yes'
    print(ans[i])


### FIXED CODE ###

import collections

N,K,Q = map(int,input().split())
li = [int(input()) for i in range(Q)]
ans = [0]*N
lis = collections.Counter(li)
for w in lis:
    ans[w-1] = K-(Q-lis[w])
for i in range(N):
    if i+1 not in lis:
        ans[i] = K-Q
for i in range(N):
    if ans[i] <= 0:
        ans[i] = 'No'
    else:
        ans[i] = 'Yes'
    print(ans[i])
### BUGGY CODE ###

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

friends = [[] for _ in range(n+1)]
blocks = [[] for _ in range(n+1)]

for _ in range(m):
    a, b = map(int, input().split())
    friends[a].append(b)
    friends[b].append(a)

for _ in range(k):
    c, d = map(int, input().split())
    blocks[c].append(d)
    blocks[d].append(c)

groups = []
nseen = {i for i in range(1, n+1)}
while len(nseen)>0:
    stack = [nseen.pop()]
    tmp = set()
    while len(stack)>0:
        node = stack.pop()
        tmp.add(node)
        for c_node in friends[node]:
            if c_node not in nseen:
                continue
            stack.append(c_node)
            nseen.remove(c_node)
    groups.append(tmp)

for i in range(1, n+1):
    for j in groups:
        if i in j:
            print(len(j - set(friends[i]) - set(blocks[i]))-1, end=' ')
            break

### FIXED CODE ###

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

friends = [[] for _ in range(n+1)]
blocks = [[] for _ in range(n+1)]

for _ in range(m):
    a, b = map(int, input().split())
    friends[a].append(b)
    friends[b].append(a)

for _ in range(k):
    c, d = map(int, input().split())
    blocks[c].append(d)
    blocks[d].append(c)

groups = [-1]*(n+1)
n_groups = []
g = 0
for i in range(1, n+1):
    if groups[i]>=0:
        continue
    stack = [i]
    cnt = 0
    groups[i] = g
    while len(stack)>0:
        node = stack.pop()
        for c_node in friends[node]:
            if groups[c_node]>=0:
                continue
            stack.append(c_node)
            groups[c_node] = g
            cnt += 1
    n_groups.append(cnt)
    g += 1

ans = []
for i in range(1, n+1):
    tmp = n_groups[groups[i]]
    for j in friends[i]:
        if groups[i] == groups[j]:
            tmp -= 1
    for j in blocks[i]:
        if groups[i] == groups[j]:
            tmp -= 1
    ans.append(tmp)

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

r,g,b,n = map(int,input().split())
rgb = [r,g,b]
rgb.sort()
x = rgb[2]
y = rgb[1]
z = rgb[0]
ans = 0

for i in range(2200):
    for j in range(2200):
        if (n-x*i-y*j)%z ==0:
            if (n-x*i-y*j)//z >=0:
                ans += 1
          
print(ans)

### FIXED CODE ###

r,g,b,n = map(int,input().split())
rgb = [r,g,b]
rgb.sort()
x = rgb[2]
y = rgb[1]
z = rgb[0]
ans = 0

for i in range(n//x+30):
    for j in range(n//y+30):
        if (n-x*i-y*j)%z ==0:
            if (n-x*i-y*j)//z >=0:
                ans += 1
          
print(ans)
### BUGGY CODE ###

if __name__ == '__main__':
    n,m = map(int, input().split())
    a_list = list(map(int, input().split()))
    a_list = sorted(a_list, reverse=True)

    search_range = 1
    while m > 0:
        max_index = a_list[0:search_range].index(max(a_list[0:search_range]))
        a_list[max_index] = a_list[max_index] // 2
        m -= 1
        search_range += 1

    answer = sum(a_list)
    print(answer)


### FIXED CODE ###

import heapq


if __name__ == '__main__':
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    q = []
    for x in a:
        heapq.heappush(q, -x)

    for i in range(m):
        x = heapq.heappop(q)
        x /= 2
        heapq.heappush(q,x)

    answer = 0
    for x in q:
        answer += int(-x//1)
    print(answer)
### BUGGY CODE ###

from math import sqrt
from sys import stdin
input = stdin.readline

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

A.sort()
ans = 0

div_set = set([])


def div(x):
    for i in range(1, int(sqrt(x)) + 1):
        if x % i == 0:
            if i in div_set or (x // i) in div_set:
                div_set.add(x)
                return False
    div_set.add(x)
    return True


for i in range(n - 1):
    if A[i] in div_set:
        continue
    elif A[i] == A[i + 1]:
        div_set.add(A[i])
        continue
    elif div(A[i]):
        ans += 1

if div(A[n - 1]):
    ans += 1


print(ans)


### FIXED CODE ###

from sys import stdin
input = stdin.readline

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

A.sort()

is_ok = [False] * (10 ** 6 + 1)
is_exist = [False] * (10 ** 6 + 1)

for a in A:
    if is_exist[a]:
        is_ok[a] = False
    else:
        is_exist[a] = True
        is_ok[a] = True
        for i in range(1, 10 ** 6 // a + 1):
            if a * i <= 10 ** 6:
                is_exist[a * i] = True

print(is_ok.count(True))
### BUGGY CODE ###

# coding: utf-8

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

count = 0

for begin in range(n):
    for end in range(begin+1, n+1):
        if sum(a[begin:end]) % m == 0:
            count += 1

print(count)


### FIXED CODE ###

# coding: utf-8


def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))

    d = {0: 1}

    a_sum = 0
    for i in range(n):
        a_sum += a[i]
        d[a_sum % m] = d.get(a_sum % m, 0) + 1

    answer = 0
    for count in d.values():
        answer += count * (count - 1) / 2

    print(int(answer))


if __name__ == '__main__':
    main()
### 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 
    power %= (10**9 + 7)

print(power)
### BUGGY CODE ###

N = int(input())
L = list(map(int, input().split()))

L.sort()
#print(L)

from bisect import bisect_left

ans = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1:]):
        j=i+1+j
        #print(a+b, abs(a-b), i, j)
        #for k, c in enumerate(L[j+1:]):
        #    if abs(a-b) < c and c < a+b:
        #        ans += 1
        #    if c >= a+b:
        #        break
        l = bisect_left(L[j+1:], abs(a-b))
        r = bisect_left(L[j+1:], a+b)
        #print(r-l)
        ans += r-l
print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
L = list(map(int, input().split()))

L.sort()
#print(L)

from bisect import bisect_right,bisect_left

ans = 0
for i, a in enumerate(L[:-2]):
    for j, b in enumerate(L[i+1:-1]):
        j=i+1+j
        #print(a+b, abs(a-b), i, j)
        #for k, c in enumerate(L[j+1:]):
        #    if abs(a-b) < c and c < a+b:
        #        ans += 1
        #    if c >= a+b:
        #        break
        l = bisect_left(L, abs(a-b),j+1)
        r = bisect_left(L, a+b,j+1)
        #print(r-l)
        ans += r-l
print(ans)
### BUGGY CODE ###

import itertools


N, M = map(int, input().split())
AB = [tuple(sorted(map(int, input().split(' ')))) for _ in range(M)]

ans = 0
for i in range(2, N):
    l = [1, i, N]
    ans += sum([1 for edge in zip(l, l[1:]) if tuple(sorted(edge)) in AB]) == 2
if ans > 0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')


### FIXED CODE ###

n, m = map(int, input().split())
sa = set()
sb = set()
for _ in range(m):
    a, b = map(int, input().split())
    if a == 1:
        sb.add(b)
    if b == n:
        sa.add(a)

print('POSSIBLE' if len(sa&sb) != 0 else 'IMPOSSIBLE')
### BUGGY CODE ###

N, a = map(int, input().split())
x = list(map(int, input().split()))
dp = [[[0]*(50*N+1) for _ in range(N+1)] for _ in range(N+1)]
for i in range(N+1):
    for j in range(N+1):
        for k in range(50*N+1):
            if i == j == k == 0:
                dp[i][j][k] = 1
            elif i and k < x[i-1]:
                dp[i][j][k] = dp[i-1][j][k]
            elif i and j and k >= x[i-1]:
                dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-1][k-x[i-1]]
ans = 0
for j in range(1, N+1):
    ans += dp[N][j][a*j]
print(ans)



### FIXED CODE ###

n, a = map(int, input().split())
x = list(map(int, input().split()))
dp = [[[0]*(50*n+1) for _ in range(n+1)] for _ in range(n+1)]
for i in range(n+1):
    for j in range(n+1):
        for k in range(50*n+1):
            if i == 0 and j == 0 and k == 0:
                dp[i][j][k] = 1
            elif i and k < x[i-1]:
                dp[i][j][k] = dp[i-1][j][k]
            elif i and j and k >= x[i-1]:
                dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-1][k-x[i-1]]
ans = 0
for j in range(1, n+1):
    ans += dp[n][j][a*j]
print(ans)
### BUGGY CODE ###

from sys import stdin
from collections import defaultdict 
# from bisect import *
# from heapq import *
# import math
# mod = 10**9+7

def primes(n):
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1, int(n**0.5)//3+1):
        if sieve[i]:
            k = 3*i+1|1
            sieve[k*k//3::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
            sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2, 3] + [3*i+1|1 for i in range(1, n//3-correction) if sieve[i]]

N = input()
A = list(map(int, input().split()))
dic = defaultdict(lambda : False)
ps = primes(10**6+1)
flg = True
for a in A:
    for p in ps:
        if a % p == 0:
            if dic[p]:
                flg = False
                break
            dic[p] = True
    if not flg:
        break
if flg:
    print("pairwise coprime")
    exit()
gcd = lambda x, y : x if y == 0 else gcd(y, x % y)
x = A[0]
for a in A[1:]:
    x = gcd(x, a)
if x == 1:
    print("setwise coprime")
else:
    print("not coprime")

### FIXED CODE ###

from sys import stdin
from collections import defaultdict 
# from bisect import *
# from heapq import *
# import math
# mod = 10**9+7

N = input()
A = list(map(int, input().split()))
M = 10**6+1
spf = [i for i in range(M)]
for i in range(2, M):
    if spf[i] == i:
        for j in range(i+i, M, i):
            spf[j] = i
cnt = defaultdict(int)
for a in A:
    ps = set()
    while a != 1:
        ps.add(spf[a])
        a = a // spf[a]
    for p in ps:
        cnt[p] += 1
if all([x < 2 for x in cnt.values()]):
    print("pairwise coprime")
    exit()
gcd = lambda x, y : x if y == 0 else gcd(y, x % y)
x = A[0]
for a in A[1:]:
    x = gcd(x, a)
if x == 1:
    print("setwise coprime")
else:
    print("not coprime")
### BUGGY CODE ###

import sys

lines = sys.stdin.readlines()

X = lines[0].split(' ')
N = int(X[0])
M = int(X[1])

H = []
Y = [[0 for i in range(M)] for j in range(N)]
Z =[0]*N

for i in range(N):
    H.append(lines[1].split(' ')[i])

for j in range(M):
    A = int(lines[j+2].split(' ')[0])
    B = int(lines[j+2].split(' ')[1])
    Y[A-1][Z[A-1]] = int(H[B-1])
    Z[A-1] +=1

    Y[B-1][Z[B-1]] = int(H[A-1])
    Z[B-1] +=1

good = 0
for i in range(N):
    if len(Y[i]) ==0:
        good +=1
    else:
        Y[i].sort()
        if int(H[i]) > int(Y[i][len(Y[i])-1]):
        	good +=1

print(good)


### FIXED CODE ###

import sys

lines = sys.stdin.readlines()

X = lines[0].split(' ')
N = int(X[0])
M = int(X[1])

H = []
Y = []

H=lines[1].split(' ')

for j in range(M):
    A = int(lines[j+2].split(' ')[0])
    B = int(lines[j+2].split(' ')[1])
    if int(H[A-1]) <= int(H[B-1]):      
      Y.append(A-1)
    if int(H[B-1]) <= int(H[A-1]):
      Y.append(B-1)
Z = len(set(Y))
print(N-Z)
### BUGGY CODE ###

from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque


def check(num, s, left, right):

    if right - left <= 1:
        print(left)
        sys.exit()

    half = (left + right) // 2

    # print(half, left, right)

    for i in range(num - half):
        now_str = s[i]

        for j in range(i + half, num):
            if half >= num - j + 1:
                break

            if now_str == s[j]:
                i_ind, j_ind = i, j

                for k in range(half - 1):
                    i_ind += 1
                    j_ind += 1
                    if i_ind >= j or j_ind >= num:
                        break
                    if s[i_ind] != s[j_ind]:
                        break
                else:
                    check(num, s, half, right)
    check(num, s, left, half)

def main():
    num = int(input())
    s = list(input())

    left, right = 0, num

    check(num, s, left, right)




if __name__ == '__main__':
    main()


### FIXED CODE ###

from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque


def check(num, s, left, right):

    if right - left <= 1:
        print(left)
        sys.exit()

    half = (left + right) // 2

    data_set = set()

    for i in range(num -2 * half  + 1):
        now_str = s[i:i + half]
        data_set.add(now_str)
        if s[i + half : i + 2 * half] in data_set:
            check(num, s, half, right)
    check(num, s, left, half)

def main():
    num = int(input())
    s = input()

    left, right = 0, num // 2 + 1

    check(num, s, left, right)




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

n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a.reverse()
lis = [2,5,5,4,5,6,3,7,6]
mat=[]
for num in a:
    mat.append([num,lis[num-1]])

def new_val(num1,num2): # 123 , 4 -> 4123
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    return int(str(num1)+str(num2))

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=mat[i][0]
    honsu=mat[i][1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

### FIXED CODE ###

n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])
### BUGGY CODE ###

n,m,R=map(int,input().split())
r=[int(i) for i in input().split()]
inputs=[]
d=[[10**100 for i in range(n+1)] for i in range(n+1)]
for i in range(m):
    a,b,c=map(int,input().split())
    d[a][b]=c
    d[b][a]=c

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n+1):
        for i in range(n+1):
            for j in range(n+1):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
d=warshall_floyd(d)
ans=10**100
import itertools
for x in itertools.permutations(r):
    p=0
    for i in range(1,R):
        p+=d[x[i-1]][x[i]]
    ans=min(ans,p)
print(ans)

### FIXED CODE ###

n,m,R=map(int,input().split())
r=[int(i) for i in input().split()]
inputs=[]
d=[[10**100 for i in range(n+1)] for i in range(n+1)]
for i in range(m):
    a,b,c=map(int,input().split())
    d[a][b]=c
    d[b][a]=c

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n+1):
        for i in range(n+1):
            for j in range(n+1):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d
d=warshall_floyd(d)
ans=10**100
import itertools
for x in itertools.permutations(r):
    p=0
    for i in range(1,R):
        p+=d[x[i-1]][x[i]]
    ans=min(ans,p)
print(ans)
### BUGGY CODE ###

n = int(input())

a = [int(input()) for _ in range(n)]

pushed = []

button = 0
count = 0
flag = False

while button not in pushed:
	if button == (2 - 1):
		flag = True
		break
	
	pushed.append(button)
	button = a[button] -1
	count += 1
	
if flag:
	print(count)
else:
	print(-1)

### FIXED CODE ###

n = int(input())

a = [int(input()) for _ in range(n)]

pushed = []

button = 0
count = 0
flag = False

while count < n + 1:
	if button == (2 - 1):
		flag = True
		break
	
	pushed.append(button)
	button = a[button] -1
	count += 1
	
if flag:
	print(count)
else:
	print(-1)