CodeEff/ECCO huggingface dataset:
# Original solution
n,m=list(map(int,input().split()))
ans=0
for i in range(n):
    m-=2
    n-=1
    if m<0 or n<0:
        break
    ans+=1
if n<0 or m<0:
    print(ans)
    exit()
while m>0:
    if n>0:
        m-=2
        n-=1
    else:
        m-=4
    if m<0:
        break
    ans+=1
print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
if n>m//2:
    print((m//2))
else:
    ans=n
    tmp=m-(n*2)
    ans+=tmp//4
    print(ans)
# Original solution
N, M = list(map(int,input().split()))

while M - 2 * N > 0:
  M -= 2
  N += 1

print((M // 2))

# Optimized solution
N, M = list(map(int,input().split()))

x = M - 2 * N
if x > 0:
  N += x // 4

print((min(N, M // 2)))
# Original solution
s,c = list(map(int,input().split()))
scc = 0

for i in range(s):
    if c > 0:
        c -= 2
        s -= 1
        scc += 1
    if s == 0:
        break
        
scc += c // 4

print(scc)

# Optimized solution
s,c = list(map(int,input().split()))
scc = 0

tmp = c // 2
scc += min(s,tmp)
c -= scc*2
        
scc += c // 4

print(scc)
# Original solution
s, c = list(map(int, input().split()))
if s*2 > c:
    print((c//2))
else:
    print((s + (c-s*2)//4))


# Optimized solution
n,m=list(map(int,input().split()));m//=2;a=min(n,m);print((a+(m-a)//2))
# Original solution
n, m = list(map(int,input().split()))
ans = 0
while n >= 1 and m >= 2:
    n -= 1
    m -= 2
    ans += 1
if n < 1:
    ans += (m // 4)
print(ans)

# Optimized solution
n, m = list(map(int,input().split()))
print(((min(n * 2, m) + m) // 4))
# Original solution
# -*- coding:utf-8 -*-
N, M = list(map(int, input().split()))
if M > 0:
    if N*2 <= M:
        print(((N+M//2)//2))
    else:
        ans = 0
        while True:
            N -= 1
            M -= 2
            if M >= 0 and N >= 0:
               ans += 1
            else:
                break
        print(ans)
else:
    print((0))

# Optimized solution
# -*- coding:utf-8 -*-
N, M = list(map(int, input().split()))
if M > 0:
    if N*2 <= M:
        print(((N+M//2)//2))
    else:
        print((M//2))
else:
    print((0))
# Original solution
N, M = list(map(int, input().split()))

if N >= M // 2:
    print((M // 2))
else :
    print((N + (M - N * 2) // 4))
    

# Optimized solution
N, M = list(map(int, input().split()))

if M < 2 * N :
    print((M // 2))
else :
    print((N + (M - 2*N) // 4))
# Original solution
a = list(map(int, input().split()))
b, c = a[0], a[1]
ans = 0
while c > 0:
    if c > 1 and b > 0:
        ans += 1
        b -= 1
        c -= 2
    elif c > 3:
        c -= 4
        ans += 1
    else:
        break

print(ans)

# Optimized solution
a = list(map(int, input().split()))
b, c = a[0], a[1]
ans = min(b, c // 2)
if b > c // 2:
    c = c % 2
else:
    c -= b * 2
b -= min(b, c // 2)
if c > 3:
    ans += c // 4
print(ans)
# Original solution
S, C = list(map(int, input().split()))

if S * 2 >= C:
    print((C//2))
    exit()

i = 0
while (C - 2*i)/(S + i) >= 2:
    i += 1

print((S + i - 1))


# Optimized solution
S, C = list(map(int, input().split()))

if S * 2 >= C:
    print((C//2))
    exit()

i = (C - 2 * S) // 4

print((S + i))
# Original solution
s, c = list(map(int, input().split()))

cnt = 0
while s >= 1 and c >= 2:
    s -= 1
    c -= 2
    cnt += 1
    
if c > 3:
    cnt += c // 4
    
print(cnt)

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

x = c // 2
cnt = min(s, x)
c -= cnt * 2
    
if c > 3:
    cnt += c // 4
    
print(cnt)
# Original solution
N = int(eval(input()))
a = []
for _ in range(N):
    a.append(int(eval(input())))

cnt = 0
s = a[0]

if s == 2:
    print((cnt + 1))
else:
    for i in range(N):
        s = a[s-1]
        cnt += 1
        if s ==  2:
            print((cnt + 1))
            break
        if i == N-1:
            print('-1')

# Optimized solution
N = int(eval(input()))
a = []
for _ in range(N):
    a.append(int(eval(input())))
    
s = a[0]
cnt = 1

for i in range(N):
    if s == 2:
        print(cnt)
        exit()
    cnt += 1
    s = a[s-1]
    
print((-1))
# Original solution
N = int(eval(input()))
a = []
for _ in range(N):
    a.append(int(eval(input())))
    
s = a[0]
cnt = 1

for i in range(N):
    if s == 2:
        print(cnt)
        exit()
    cnt += 1
    s = a[s-1]
    
print((-1))

# Optimized solution
import sys

N = int(eval(input()))
a = [int(i) for i in sys.stdin]
    
s = a[0]
cnt = 1

for i in range(N):
    if s == 2:
        print(cnt)
        exit()
    cnt += 1
    s = a[s-1]
    
print((-1))
# Original solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]
b=a[0];c=1;d=[b]
while b!=2 and b!=1 and len(set(d))==len(d):
  b=a[b-1]
  c+=1
  d.append(b)
if b==2:print(c)
else:print((-1))

# Optimized solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]
b=a[0];c=1
while b!=2 and c<=n:
  b=a[b-1]
  c+=1
if b==2:print(c)
else:print((-1))
# Original solution
n = int(eval(input()))
a = []
for i in range(n):
  a.append(int(eval(input())))

visited = set()
count = 0
next = 1
while True:
  count += 1
  visited.add(next)
  next = a[next-1]
  if next == 2:
    print(count)
    exit(0)
  if next in visited:
    print((-1))
    exit(0)

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

b = a[0]
cnt = 1

while b != 2 and cnt <= n:
    b = a[b-1]
    cnt += 1
if b == 2:
    print(cnt)
else:
    print((-1))
# Original solution
def main():
    buttons = int(eval(input()))
    next_lighten_up = [int(eval(input())) for _ in range(buttons)]
    searched = [1]
    answer = 1
    while True:
        if next_lighten_up[searched[-1] - 1] == 2:
            break
        elif next_lighten_up[searched[-1] - 1] in searched:
            answer = -1
            break
        else:
            answer += 1
            searched.append(next_lighten_up[searched[-1] - 1])
    print(answer)


if __name__ == '__main__':
    main()



# Optimized solution
def main():
    buttons = int(eval(input()))
    next_lighten_up = [int(eval(input())) for _ in range(buttons)]
    searched = [False] * (buttons + 1)
    answer = 1
    index = 1
    while True:
        if next_lighten_up[index - 1] == 2:
            break
        elif searched[next_lighten_up[index - 1]]:
            answer = -1
            break
        else:
            searched[next_lighten_up[index - 1]] = True
            answer += 1
            index = next_lighten_up[index - 1]
    print(answer)


if __name__ == '__main__':
    main()
# Original solution
def main():
    buttons = int(eval(input()))
    next_lighten_up = [int(eval(input())) for _ in range(buttons)]
    searched = [False] * (buttons + 1)
    answer = 1
    index = 1
    while True:
        if next_lighten_up[index - 1] == 2:
            break
        elif searched[next_lighten_up[index - 1]]:
            answer = -1
            break
        else:
            searched[next_lighten_up[index - 1]] = True
            answer += 1
            index = next_lighten_up[index - 1]
    print(answer)


if __name__ == '__main__':
    main()



# Optimized solution
def main():
    n = int(eval(input()))
    a = [0] + [int(eval(input())) for _ in range(n)]
    checked = [False for _ in range(n + 1)]
    answer = 0
    ind = 1
    while True:
        answer += 1
        checked[ind] = True
        ind = a[ind]
        if checked[ind]:
            answer = -1
            break
        elif ind == 2:
            break
    print((answer if answer > -1 else -1))


if __name__ == '__main__':
    main()
# Original solution
def main():
    N = int(eval(input()))
    a = [0 for i in range(N)]
    for i in range(N):
        a[i] = int(eval(input()))
    d = [1]
    ans = 0
    for i in range(N):
        if a[d[i]-1] == 2:
            ans += 1
            break
        elif a[d[i]-1] in d or i == N - 1:
            ans = -1
            break
        else:
            d.append(a[d[i]-1])
            ans += 1
    print(ans)
main()



# Optimized solution
def main():
    N = int(eval(input()))
    a = [0 for i in range(N)]
    for i in range(N):
        a[i] = int(eval(input()))
    now = 1
    for i in range(N):
        if a[now - 1] == 2:
            break
        elif i == N - 1:
            i = -2
        else:
            now = a[now - 1]
        #print('{}: {}'.format(i, now))
    print((i + 1))
main()
# Original solution
N = int(eval(input()))
next = []
for i in range(N):
    next.append(int(eval(input())))
count = 0
history = [1]
while True:
    light = next[history[-1] - 1]
    if light in history:
        print((-1))
        exit()
    count += 1
    if light == 2:
        print(count)
        exit()
    else:
        history.append(light)

# Optimized solution
N = int(eval(input()))
next = []
for i in range(N):
    next.append(int(eval(input())))
count = 0
light = 1
for i in range(N):
    light = next[light - 1]
    count += 1
    if light == 2:
        print(count)
        exit()
print((-1))
# Original solution
import sys
nums = int(eval(input()))
bottom = list(range(nums))
check = {}
count = 0
a = 0
for i in range(nums):
    bottom[i] = int(eval(input()))
    check[i] = 0

while True:

    if bottom[a] == 2:
        count += 1
        break
    if check[a] == 1:
        print("-1")
        sys.exit()
    check[a] = 1
    a = bottom[a] - 1
    count += 1
print(count)

# Optimized solution
a = [int(eval(input())) for _ in range(int(eval(input())))]
c,s = 1,a[0]
for i in range(len(a)):
    if s ==2:
        print(c)
        break
    c,s = c+1, a[s-1]
else:print((-1))
# Original solution
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N = int(eval(input()))
A = [0] + [int(eval(input())) for _ in range(N)]

cur = 1
ans = 0
pushed = [False] * (N + 1)
pushed[1] = True
ok = True
while True:
    ans += 1
    cur = A[cur]
    if cur == 2:
        break
    if pushed[cur]:
        ok = False
        break

if ok:
    print(ans)
else:
    print((-1))

        



# Optimized solution
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N = int(eval(input()))
A = [0] + [int(eval(input())) for _ in range(N)]

cur = 1
ans = 0
pushed = [False] * (N + 1)
pushed[1] = True
ok = True
while True:
    pushed[cur] = True
    ans += 1
    cur = A[cur]
    if cur == 2:
        break
    if pushed[cur]:
        ok = False
        break

if ok:
    print(ans)
else:
    print((-1))
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]

cnt, i = 0, 0
for j in range(10 ** 6):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        #break
        quit()
    else:
        i = a[i] - 1
print((-1))

# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]

cnt, i = 0, 0
for j in range(len(a)):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        #break
        quit()
    else:
        i = a[i] - 1
print((-1))
# Original solution
def main():
    n = int(eval(input()))
    As = [int(eval(input())) for _ in range(n)]

    move = []
    i = 0
    ans = 1
    while As[i] != 2:
        if As[i] not in move:
            move.append(As[i])
            ans += 1
            i = As[i]-1
        else:
            ans = -1
            break
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    n = int(eval(input()))
    As = [int(eval(input())) for _ in range(n)]

    x = [0]*n
    i = 0
    ans = 0
    while x[i] == 0:
        ans += 1
        x[i] = 1
        i = As[i] - 1
        if i == 1:
            print(ans)
            exit()
    print((-1))


if __name__ == "__main__":
    main()
# Original solution
N = int(eval(input()))
A = [int(eval(input()))-1 for i in range(N)]

light = 0
first = 0
ans = 0

while True:
    if light == A[light]:
        print((-1))
        exit()
    light = A[light]
    ans += 1
    if light == 1:
        print(ans)
        exit()
    if light == first:
        print((-1))
        exit()



# Optimized solution
N = int(eval(input()))
A = [int(eval(input()))-1 for _ in range(N)]
p = 0
for i in range(N):
    if p == 1:
        print(i)
        break
    p = A[p]
else:
    print((-1))
# Original solution
N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))
history = []
now = 0
cnt = 0
while(True):
    if now in history:
        cnt = -1
        break
    else:
        cnt += 1
        history.append(now)
        now = a[now]-1
        if now == 1:
            break
print(cnt)

# Optimized solution
N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))
history = set()
now = 0
cnt = 0
while(True):
    if now in history:
        cnt = -1
        break
    else:
        cnt += 1
        history.add(now)
        now = a[now]-1
        if now == 1:
            break
print(cnt)
# Original solution
n = int(eval(input()))
a = [0]*n
for i in range(n):
    a[i] = int(eval(input()))

fp = [a[0]]
now = a[0]
cnt = 0

while now != 2:
    cnt += 1
    now = a[now-1]
    if now in fp:
        print((-1))
        exit()
    fp.append(now)

if now == 2:
    cnt += 1

print(cnt)

# Optimized solution
n = int(eval(input()))
a = [0]*n
for i in range(n):
    a[i] = int(eval(input()))

now = a[0]
cnt = 0

while cnt <= n:
    cnt += 1
    if now == 2:
        print(cnt)
        exit()
    now = a[now-1]

print((-1))
# Original solution
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(eval(input()))
    edge = [[] * n for i in range(n)] # rangeに頂点の数。繋がっている頂点を投入するリスト
    for i in range(n): # rangeに辺の数
        x, y = i,int(eval(input()))
        y = y - 1
        edge[x].append(y)
    sagasu=0
    cnt=0
    while True:
        a=edge[sagasu]
        if a==[1]:
            print((cnt+1))
            break
        else:
            if cnt>n:
                print((-1))
                break
            else:
                cnt+=1
                sagasu=a[0]
resolve()

# Optimized solution
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(eval(input()))
    l=[int(eval(input())) for i in range(n)]
    light=0
    cnt=0
    while True:
        if l[light]==2:
            cnt+=1
            print(cnt)
            break
        else:
            if cnt>n:
                print((-1))
                break
            else:
                light=l[light]-1
                cnt+=1
            
resolve()
# Original solution
n = int(eval(input())) 
t = [int(eval(input())) for i in range(n)] 

res = [1]
i = 1

while i <= n:
  res.append(t[res[-1]-1])
  if res[-1] == 2:
    print((len(res)-1))
    break
  elif len(res) != len(set(res)):
    print((-1))
    break
  i += 1


# Optimized solution
n = int(eval(input())) 
t = [int(eval(input())) for i in range(n)] 

x = 1
res = -1
for i in range(n):
  x = t[x-1]
  if x == 2:
    res = i + 1
    break
  
print(res)
# Original solution
# coding: utf-8
import sys

N = int(eval(input()))
lst = [int(eval(input())) for _ in range(N)]

btn = 1
cnt = 0
for a in lst:
    btn = lst[btn-1]
    cnt += 1
    if btn == 2:
        print(cnt)
        sys.exit()
print((-1))

# Optimized solution
# coding: utf-8
import sys

N = int(eval(input()))
lst = [int(i) for i in sys.stdin]

btn = 1
cnt = 0
for a in lst:
    btn = lst[btn-1]
    cnt += 1
    if btn == 2:
        print(cnt)
        sys.exit()
print((-1))
# Original solution
N = int(eval(input()))
a_list = [int(eval(input())) for _ in range(N)]

button_history = [1]
while True:
    next_button = a_list[button_history[-1] - 1]
    if next_button == 2:
        print((len(button_history)))
        break
    elif next_button in button_history:
        print((-1))
        break
    else:
        button_history.append(next_button)


# Optimized solution
N = int(eval(input()))
a_list = [int(eval(input())) for _ in range(N)]

button_history = [1]
current_button = 1
num_manipulation = -1

for i in range(1, N + 1):
    current_button = a_list[current_button - 1]
    if current_button == 2:
        num_manipulation = i
        break

print(num_manipulation)
# Original solution
n = int(eval(input()))
a = [0]
tmp = [int(eval(input())) for _ in range(n)]
a = a + tmp

r = -1
cnt = 1
next = a[1]
while cnt < n:
  if next == 2:
    r = cnt
    break
  else:
    next = a[next]
    cnt += 1
print(r)

# Optimized solution
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *a = list(map(int, read().split()))
    r = 0
    x = 1
    while x != 2:
        r += 1
        x = a[x - 1]
        if x == 1 or r >= n:
            r = -1
            break
    print(r)

if __name__ == '__main__':
    main()
# Original solution
# ABC065B - Trained?
n = int(eval(input()))
lst = list(map(int, [input().rstrip() for _ in range(n)]))
button = lst[0]
count = 0
for _ in range(n):
    count += 1
    if button == 2:
        break
    button = lst[button - 1]
else:
    count = -1
print(count)

# Optimized solution
# ABC065B - Trained?
import sys
input = sys.stdin.readline

n = int(eval(input()))
lst = list(map(int, [input().rstrip() for _ in range(n)]))
button = lst[0]
count = 0
for _ in range(n):
    count += 1
    if button == 2:
        break
    button = lst[button - 1]
else:
    count = -1
print(count)
# Original solution
N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))

def get_ans(a):
    mods = list()
    
    if 2 not in a:
        print((-1))
        return True
    
    count = 0
    mod = 1
    while(True):
        mod = a[mod-1]
        count += 1
        if (mod == 2):
            print(count)
            return True
        elif mod in mods:
            print((-1))
            return True
        else:
            mods.append(mod)

ret = get_ans(a)

# Optimized solution
N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input()))-1)
index = 0
#indexが1になったら（２ボタンを押したら）おしまい
flag = False 
count = 0
for i in range(N):
    count+=1
    index=a[index]
    if(index == 1):
        flag = True
        break
if(flag):
    print(count)
else:
    print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

record = []
index = 0
count = 0
tf = True
while True:
    index=a[index]-1
    record.append(index)
    if record.count(index) == 2:
        print((-1))
        tf = False
        break
    else:
        count += 1
        if index == 1:
            break
if tf:
    print(count)


# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

index = 0
count = 1
tf = False

while True:
  if a[index]==2:
    tf = True
    break
  else:
    index=a[index]-1
    if count == n:
      break
    else:
      count += 1
if tf:
    print(count)
else:
  print((-1))
# Original solution
N = int(eval(input()))
data = [int(eval(input())) for i in range(N)]
i = 0
a_i = None
res = 1
pre_i = 0
while data[i] != 2:
    a_i = data[i]
    if pre_i == a_i:
        res = -1
        break

    pre_i = i + 1
    i = data[i] -1
    res += 1
print(res)



# Optimized solution
N = int(eval(input()))
data = [int(eval(input())) for i in range(N)]
i = 0
a_i = None
res = 1
pre_i = 0
time = -1
while data[i] != 2:
    a_i = data[i]
    if pre_i == a_i or time == N:
        res = -1
        break

    pre_i = i + 1
    i = data[i] -1
    time += 1
    res += 1
print(res)
# Original solution
def solve():
    n = int(eval(input()))
    l = []

    for i in range(n):
        l.append(int(eval(input())))

    a = 1
    hist = []
    while(True):
        if(a-1 in hist):
            return -1
        if(a-1 == 1):
            return len(hist)
        hist.append(a-1)
        a = l[a-1]

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


# Optimized solution
def solve():
    n = int(eval(input()))
    l = []

    for i in range(n):
        l.append(int(eval(input())))

    a = 1
    hist = set([])
    while(True):
        if(a-1 in hist):
            return -1
        if(a-1 == 1):
            return len(hist)
        hist.add(a-1)
        a = l[a-1]

if(__name__ == "__main__"):
    print((solve()))
# Original solution
def resolve():
    N = int(eval(input()))
    A = [int(eval(input())) - 1 for _ in range(N)]

    pressed = []
    i = 0
    t = 0
    while i != 1:
        i = A[i]
        if i in pressed:
            t = -1
            break
        pressed.append(i)
        t += 1

    print(t)
    
resolve()

# Optimized solution
def resolve():
    N = int(eval(input()))
    A = [int(eval(input())) - 1 for _ in range(N)]

    pressed = 0
    i = 0
    t = 0
    while i != 1:
        i = A[i]
        if pressed > N:
            t = -1
            break
        pressed += 1
        t += 1

    print(t)
    
resolve()
# Original solution
n = int(eval(input()))
li = [int(eval(input())) for _ in range(n)]

idx_li = []

count = 1
bu = li[0]
idx_li.append(bu)
if bu == 2:
  print(count)
  exit()
  
while True:
  bu = li[bu - 1]
  count += 1
  
  if bu == 2:
    print(count)
    exit()
  
  if bu in idx_li:
    print((-1))
    exit()
  else:
    idx_li.append(bu)
    
  

# Optimized solution
n = int(eval(input()))
li = [int(eval(input())) for _ in range(n)]

light = 1
count = 0

for i in range(n):
  light = li[light - 1]
  count+=1
  if light == 2:
    print(count)
    exit()
print((-1))
# Original solution
def resolve():
    N = int(eval(input()))
    A = [int(eval(input())) for i in range(N)]
    idx = 0
    target = 1
    cnt = 1
    passed = []
    while True:
        if idx >= N:
            print((-1))
            return
        if A[idx]-1 == target:
            print(cnt)
            return
        if A[idx]-1 in passed:
            print((-1))
            return
        passed.append(idx)
        idx = A[idx]-1
        cnt += 1



if '__main__' == __name__:
    resolve()

# Optimized solution
def resolve():
    N = int(eval(input()))
    A = [int(eval(input())) for i in range(N)]
    idx = 0
    target = 1
    cnt = 1
    passed = [False]*N
    while True:
        if idx >= N:
            print((-1))
            return
        if A[idx]-1 == target:
            print(cnt)
            return
        if passed[A[idx]-1]:
            print((-1))
            return
        passed[idx] = True
        idx = A[idx]-1
        cnt += 1



if '__main__' == __name__:
    resolve()
# Original solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]
c,s=1,a[0]
while s!=2 and c<n:c,s=c+1,a[s-1]
print((c if c<n else -1))

# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
b = a[0]
count = 0
for i in range(N-1):
	count += 1
	if b == 2:
		print(count)
		break
	b = a[b-1]
else:
	print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
check = [0 for i in range(n)]
check[0] = 1
i = 0
cnt = 0
while True:
	cnt += 1
	x = a[i]
	if x-1 == 1:
		print (cnt)
		exit()
	if check[x-1] == 1:
		print((-1))
		exit()
	else:
		check[x-1] = 1
	i = x-1

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
chk = [False for i in range(n)]

c = 0
d = 0
while True:
	if chk[c]:
		print((-1))
		exit()
	else:
		chk[c] = True
		if c == 1:
			print (d)
			exit()
	c = a[c]-1
	d += 1
# Original solution
g = [[] for _ in range(int(eval(input())))]
for gv in g:
    gv.append(int(eval(input())) - 1)

q = [[0]]
while q:
    path = q.pop(0)
    n = path[-1]
    if n == 1:
        print((len(path) - 1))
        exit()
    else:
        for x in g[n]:
            if x not in path:
                new_path = path[:]
                new_path.append(x)
                q.append(new_path)
print((-1))

# Optimized solution
n=int(eval(input()))
a = [int(eval(input())) for x in [0]*n]
k=0
for i in range(n):
    if k==1:
        print(i)
        break
    k=a[k]-1
else:
    print((-1))