Problem:


Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3



Sample input:
3
3
1
2


Sample output:
2

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=[]
f=[]
has2=False
for i in range(n):
  ai=int(input())-1
  if ai==1:
    has2=True
  a.append(ai)
  f.append(False)

if has2==False:
  print("-1")
  sys.exit()

now=0
cnt=0
while True:
  
  if f[now]==True:
    print(-1)
    sys.exit()
  if now==1:
    print(cnt)
    sys.exit()
  f[now]=True
  now=a[now]
  cnt+=1

Submission status: Accepted

Solution code:
N=int(input())
A=[0]
for i in range(N):
    A.append(int(input()))
if A.count(2)==0:
    print(-1)
else:
    m=1
    for i in range(N):
        m=A[m]
        if m==2:
            print(i+1)
            break
        else:
            continue
    if m!=2:
        print(-1)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input()) for _ in range(N)]
flag=0
b=1
for i in range(N):
    b=a[b-1]
    if b==2:
        print(i+1)
        flag=1
        break
if flag==0:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
lst = [int(input()) for _ in range(N)]
lst = [0] + lst
lst2 = [0] * (N + 1)
i = 1
ret = 1
while True:
    if lst[i] == 2:
        break
    if lst2[i] == 1:
        ret = -1
        break
    ret += 1
    lst2[i] = 1 
    i = lst[i]
    
print(ret)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
num=0
cnt=0
while cnt<=n:
    tmp=a[num]
    cnt+=1
    if tmp==2:
        break
    else:
        num=tmp-1
if cnt>=n:
    print(-1)
else:
    print(cnt)

Submission status: Accepted

Solution code:
import sys
n = int(input())
arr = [int(input()) for i in range(n)]
btn = arr[0]
count = 1
for i in range(n):
  if btn == 2:
    print(count)
    sys.exit()
  else:
    btn = arr[btn-1]
    count += 1
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N) :
    a.append(int(input()))

index = 1
cnt = 0

for i in range(N):
    index = a[index - 1]
    cnt += 1
    if index == 2 :
        break
    if index == 1 :
        cnt = -1
        break
    if i == N - 1 :
        cnt = -1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = []
ans = -1
for i in range(n):
  a.append(int(input()))
btn = 1
cnt = 0
for i in range(n):
  btn = a[btn-1]
  cnt += 1
  if btn == 2:
    ans = cnt
    break
  if btn == 1:
    break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]

next = a[0]
count = 1

for i in range(1,n):
    if next == 2:
        print(count)
        break
    next = a[next-1]
    count += 1
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(1, N+1)]
i = a[0]
result = 1

for _ in range(N):
    if i == 2:
        break
    i = a[i-1]
    result += 1

print(-1 if result > N else result)

Submission status: Accepted

Solution code:
N = int(input())
a = {i: int(input()) for i in range(1, N+1)}
i = a[1]
result = 1

for _ in range(N):
    if i == 2:
        break
    i = a[i]
    result += 1

print(-1 if result > N else result)

Submission status: Accepted

Solution code:
n = int(input()) 
t = [int(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)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in [0]*N]
flg = [True]*N
i = 0
cnt = 0
while flg[i]:
    flg[i] = False
    if i == 1:
        break
    i = A[i] -1
    cnt += 1
if flg[1]:
    print(-1)
else:
    print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))

ans = -1
num = 0
ind = 0
while num < N+1:
  num += 1
  if A[ind] == 2:
    ans = num
    break
  ind = A[ind] - 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
a = [int(input())-1 for x in range(N)]

d = defaultdict(int)
c = 0
b = 0
while True:
    if b == 1:
        print(c)
        exit()
    if d[b] == 1:
        print(-1)
        exit()
    else:
        d[b] = 1
    b = a[b]
    c += 1

Submission status: Accepted

Solution code:
n = int(input())
a = [int(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)

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=[int(input()) for _ in range(n)]
c=set()
s=a[0]
cnt=1
while(1):
    if s==2:
        print(cnt)
        sys.exit()
    if s in c:
        print(-1)
        sys.exit()
    c.add(s)
    s=a[s-1]
    cnt+=1

Submission status: Accepted

Solution code:
n = int(input())
List = [int(input()) for _ in range(n)]
c, s = 1, List[0]
while s!=2 and c<n:
    c+=1
    s = List[s-1]
print(-1 if c>=n else c)

Submission status: Accepted

Solution code:
n = int(input())
List = [int(input()) for _ in range(n)]
s = List[0]
count = 1
while s!=2 and count<n:
    count+=1
    s = List[s-1]
print(-1 if count>=n else count)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
l = [0]*n
num = 0

for i in a:
    l[num] += 1
    if l[1] == 1:
        print(sum(l)-1)
        break
    else:
        num = a[num]-1
        if l[num] == 2:
            print(-1)
            exit()

Submission status: Accepted

Solution code:
#ABC065B
N = int(input())
ai = [int(input()) for _ in range(N)]

lightning = 1
count = 1
ok = False
for _ in range(N):
    lightning = ai[lightning-1]
    if lightning == 2:
        print(count)
        ok = True
        break
    else:
        count += 1

if not ok:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
cnt = 0
button = 1
for _ in range(N):
    button = a[button - 1]
    cnt += 1
    if button == 2:
        print(cnt)
        exit()
print(-1)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
f=lambda x,y:x[y]
i=1
c=0
for _ in range(n):
  i=f(a,i-1)
  c+=1
  if i==2:
    print(c)
    exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
from collections import defaultdict
d = defaultdict(int)
for i in range(1, N+1):
    d[i] = int(input())
used = defaultdict(bool)
i = 1
ans = 0
while True:
    i = d[i]
    ans += 1
    if used[i]:
        ans = -1
        break
    if i == 2:
        break
    used[i] = True
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
used = [False for _ in range(N)]

crntbtn = 1
cnt=0
while(True):
  used[crntbtn-1] = True
  crntbtn = A[crntbtn-1]
  cnt += 1
  if crntbtn == 2:
    print(cnt)
    break
  else:
    if used[crntbtn-1] == True:
      print(-1)
      break

Submission status: Accepted

Solution code:
#65
n = int(input())
a = []
for j in range(n):
    a.append(int(input()))

count = 0
i = 1
for j in range(n+1):
    if a[i-1]==2:
        count += 1
        print(count)
        break
    else:
        i = a[i-1]
        count += 1
    if j ==n:
        print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
cnt = 0
i = 0
while(cnt <= N):
    if(a[i] == 2):
        cnt += 1
        break
    else:
        i = a[i]-1
        cnt += 1
if(cnt > N):
    cnt = -1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = [0] * (N + 1)
for i in range(N):
	a[i + 1] = int(input())
cnt = 0
j = 1
while cnt <= N and j != 2:
	j = a[j]
	cnt += 1
if j == 2:
	print(cnt)
else:
	print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]

num = 0
now = 0
been = set()
been.add(0)

while now != 1:
    now = a[now] - 1
    if now in been:
        num = -1
        break
    else:
        been.add(now)
        num += 1

print(num)

Submission status: Accepted

Solution code:


def solve(an,n):
  ret = 0
  nxt = 1
  while ret <= n+1:
    nxt = an[nxt-1]
    ret += 1
    if nxt == 2:
      return ret
  return -1


if __name__ == "__main__":
  n = int(input())
  An = [0 for i in range(n)]
  for i in range(n):
    An[i] = int(input())
  print(solve(An,n))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
b = 1
flag = True
for i in range(n):
    b = a[b-1]
    if b == 2:
        print(i+1)
        flag = False
        break
    elif b == 1:
        print(-1)
        flag = False
        break
if flag:
    print(-1)

Submission status: Accepted

Solution code:
# ABC065B - Trained?
import sys
input = sys.stdin.readline

n = int(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)

Submission status: Accepted

Solution code:
# ABC065B - Trained?
n = int(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)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
trace = [False for _ in range(n)]
pos = 0
push_count = 0

while True:
    # push
    push_count +=1

    if a[pos]==2:
        print(push_count)
        break
    if not trace[pos]:
        trace[pos] = True
        pos = a[pos] -1  
    else:
        print(-1)
        break

Submission status: Accepted

Solution code:
import sys
from collections import Counter

n = int(input())
a_list = [int(input()) for _ in range(n)]
past_set = set()
next_hop = 0
cnt = 0

while True:
    if next_hop in past_set:
        print(-1)
        sys.exit()
    else:
        past_set.add(next_hop)
    if next_hop == 1:
        print(cnt)
        sys.exit()
    else:
        next_hop = a_list[next_hop]-1
        cnt += 1

Submission status: Accepted

Solution code:
N = int(input())
alist = [int(input()) for _ in range(N)]

now = alist[0]
for i in range(N):
    if now==2:
        print(i+1)
        break
    now = alist[now-1]
else:
    print(-1)

Submission status: Accepted

Solution code:
from collections import defaultdict

def read_input():
    n = int(input())
    alist = [-1]

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


    return n, alist

def submit():
    n, alist = read_input()

    through = defaultdict(int)
    curr_button = 1
    steps = 0
    while through[curr_button] == 0:
        through[curr_button] = 1
        curr_button = alist[curr_button]
        steps += 1

        if curr_button == 2:
            break

    if curr_button == 2:
        print(steps)
    else:
        print(-1)



if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
def solve():
    N = int(raw_input())
    arr = []
    for i in range(N):
        arr.append(int(raw_input()))
    turning_on = 1
    count = 0
    while turning_on != 2:
        count += 1
        turning_on = arr[turning_on - 1]
        if count > N + 1:
            count = -1
            break
    print str(count)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(int(input()) for i in range(N))
i = 1
cnt = 0
while True:
    i = A[i-1]
    cnt += 1
    if i == 2:
        print(cnt)
        break
    if cnt == N:
        print(-1)
        break

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)

N = int(input())
a = [int(input()) for _ in range(N)]
c = [0] * N

def f(i, n):
  if a[n] == 2: return i
  if c[n] == 1: return -1
  c[n] = 1
  return f(i + 1, a[n] - 1)

print(f(1, 0))

Submission status: Accepted

Solution code:
N = int(input())
A = {}
for i in range(N):
    A[i+1] = int(input())
    
n = 1
c = 0
for i in range(N):
    n = A[n]
    c += 1
    if n == 2:
        print(c)
        quit()
print(-1)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7


N = int(input())
a = [0 for i in range(N)]
for i in range(N):
    a[i] = int(input())

cnt = 0
ans = -1
position = 1
for i in range(N):
    position = a[position-1]
    cnt += 1
    if position == 2:
        ans = cnt
        break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = [True for i in range(n+1)]
a = [0]
for i in range(n):
    a.append(int(input()))
i = 1
ans = 0
while l[i]:
    l[i] = False
    i = a[i]
    ans += 1
    if i==2:
        print(ans)
        import sys
        sys.exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())

As = []
for i in range(0,N):
    As.append(int(input()))

start = As[0]
steps = 1

if start == 2:
    print(steps)
    exit()

while True:
    next = As[start - 1]
    steps += 1
    if next == 2:
        break
    else:
        if next == start or steps > N:
            print(-1)
            exit()

    start = next

print(steps)

Submission status: Accepted

Solution code:
def main():
    input = sys.stdin.readline
    try:
        N = int(input())
        a = [int(input()) for i in range(N)]
        i = 0
        cnt = 0
        while True:
            cnt += 1
            if a[i] == 2:
                print(cnt)
                break
            elif a[i] == -1:
                print(-1)
                break
            a[i], i = -1, a[i] - 1

    finally:
        input = None

import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]

button = 1
for i in range(N):
    if button == 2:
        print(i)
        break
    button = a[button-1]
    
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
p = 1
c = 0
while True:
  c += 1
  next = a[p-1]
  if next == 2:
    print(c)
    break
  if next == 0:
    print(-1)
    break
  a[p-1] = 0
  p = next

Submission status: Accepted

Solution code:
N = int(input())
nums = [int(input()) for _ in range(N)]

visited = [False]*N
ind = 1
count = 1

for _ in range(N):
    visited[ind - 1] = True
    v = nums[ind -1]
    if v == 2:
        break
    if visited[v-1]:
        count = -1
        break
    ind = v
    count += 1

print(count)

Submission status: Accepted

Solution code:

N = int(input())
nums = [int(input()) for x in range(N)]

ind = 1
count = 1
while(True):
    next = nums[ind-1]
    if next == 2:
        break
    ind = next
    count+=1
    if count>= 100000:
        count = -1
        break

print(count)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input())-1 for i in range(N)]
visited = [False]*N
now = 0
ans = 0
while now != 1:
    if visited[now]:
        ans = -1
        break
    visited[now] = True
    now = a[now]
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
cnt = 0
tmp = 0
for i in range(n):
    if a[tmp]==2:
        print(cnt+1)
        exit()
    else:
        tmp =a[tmp]-1
        cnt += 1
else:
    print(-1)
    exit()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]

on, ans, flg = 0, 0, 0

for i in range(N):
    now = a[on]-1
    ans += 1
    if now == 1:
        flg = 1
        break
    else:
        on = now

if flg == 1:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
l = [0] + [int(input()) for i in range(n)]
cnt = 0
a = 1
while a != 2:
    if a==0:
        print(-1)
        break
    tmp=l[a]
    l[a]=0
    a=tmp
    cnt += 1
else:
    print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
A=list(int(input()) for i in range(n))
a=0
count=0
for i in range(n):
  if a!=1:
    a=A[a]-1
    count+=1
print(count if a==1 else -1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]

pushed = [False] * N
ans = 0
current = 0
while True:
    ans += 1
    current = a[current] - 1
    if pushed[current]:
        ans = -1
        break
    if current == 1:
        break
    pushed[current] = True
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
  a.append(int(input()))
j = 0
count = 1
flag = False

for i in range(N):
  if a[j] == 2:
    flag = True
    break
  else:
    count += 1
    j = a[j]-1


if flag:
  print(int(count))
else:
  print("-1")

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline
N = int(input())
a = [int(input()) for _ in range(N)]
b = [-1] * N
botan = a[0]

cnt = 0

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

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
b = [-1] * N
botan = a[0]

cnt = 0

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

print(-1)

Submission status: Accepted

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input().rstrip())
    a = []

    for i in range(N):
        a.append(int(input().rstrip()) - 1)
    next_a = 0
    cnt = 0
    flag=False
    for i in range(N):
        if(next_a == 1):
            flag = True
            break
        else:
            cnt +=1
            next_a = a[next_a]
    if flag:
        print(cnt)
    else:
        print(-1)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
arr = []
passed = []
for i in range(n):
  a = int(input())
  arr.append(a)
  
next_position = 1
ans = 0

while(1):
  next_position = arr[next_position-1]
  #print(position)
  if ans == n:
    print(-1)
    break
  #passed.append(next_position)
  ans += 1
  if next_position == 2:
    print(ans)
    break

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]

count = 0
pnext = 1
while count < N:
	count += 1
	pnext = A[pnext - 1]
	if pnext == 2:
		print(count)
		break
else:
	print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input())-1)
button = 0 
push = 0
while a[button] != -1:
    push += 1
    tmp = a[button]
    a[button] = -1
    button = tmp 
    if button == 1:
        print(push)
        exit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
botan = a[0]
ans = -1
for i in range(n):
	if botan == 2:
		ans = i+1
		break
	botan = a[botan-1]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
list=[]
list2=[0]*n
count=0
next=1
for i in range(n):
  list.append(int(input()))
while True:
  list2[next-1]+=1
  count+=1
  next=list[next-1]
  if next==2:
    print(count)
    break
  if list2[next-1] >= 2:
    print(-1)
    break

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input()) for _ in range(N)]
count=0
nexti=1
for i in range(N):
    nexti=a[nexti-1]
    count+=1
    if(nexti==2):
        print(count)
        exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]

k = 1
ans = 0
while(True):
    k = a[k-1]
    ans += 1
    if k == 2 or ans == N:
        break
print(ans) if k == 2 else print(-1)

Submission status: Accepted

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

count = 0
i = 0
while count <= 10e+5:
    count += 1
    if a[i] == 2:
        print(count)
        break
    else:
        i = a[i]-1
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = [ni() for _ in range(n)]
count = 0
nexti = 0
for i in range(n):
    if nexti == 1:
        print(count)
        exit()
    else:
        nexti = a[nexti] - 1
        count += 1
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]

if 2 in a:
	l = 1
	count = 0
	while 1:
		if l==2:
			print(count)
			break
		elif a[l-1]==0:
			print(-1)
			break
		temp = l-1
		l = a[l-1]
		a[temp] = 0 
		count += 1
else:
	print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input())for _ in[0]*N]
r=0;i=0
for _ in [0]*N:
  i=A[i]-1;r+=1
  if i==1:print(r);exit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())

a = {i + 1:int(input()) for i in range(n)}

x = 1

for i in range(n):
  x = a[x]
  if x == 2:
    print(i + 1)
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input())-1 for _ in range(N)]
result=[False]*N
k=0
while 1:
	newlight=A[k]
	if k==1:
		ans=0
		break
	elif result[k]:
		ans=1
		break
	else:
		result[k]=True
		k=newlight
if ans:
	print(-1)
else:print(sum(result))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(500000)
N=int(input())
A=[int(input())-1 for _ in range(N)]
result=[False]*N
def next(k):
	newlight=A[k]
	if k==1:
		return(1)
	elif result[k]:
		return(0)
	else:
		result[k]=True
		return(next(newlight))
if not next(0):
	print(-1)
else:print(sum(result))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0));c,s=1,a[0]
while s!=2and c<n:c+=1;s=a[s-1]
print(c*(c<n)or-1)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = []
for i in range(n):
	a.append(int(raw_input()))

num = 1
count = 0

for i in range(n):
	num = a[num-1]
	count += 1
	if num == 2:
		break
	elif i == n - 1:
		count = -1

print count

Submission status: Accepted

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]
a=1
ans=0
while a!=2:
    a=l[a-1]
    ans+=1
    if ans==n+1:
        ans=-1
        break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(input()) for _ in range(n)]

ai = 0
for i in range(n):
  ai = s[ai] - 1
  if ai == 1:
    print(i + 1)
    exit()
  else:
    continue
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append([i+1, int(input())])

flag = False
cnt = 0
b, new = A[0]
for i in range(2*N):
    if b == 2:
        flag = True
        break

    cnt += 1
    b, new = A[new-1]

if flag:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = -1
ai = 0
for i in range(n):
    ai = a[ai] - 1
    if ai == 1:
        ans = i + 1
        break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
cnt = 0
k = 0
while cnt<n:
    if a[k]==2:
        print(cnt+1)
        exit()
    else:
        k = a[k]-1
        cnt+=1
print(-1)

Submission status: Accepted

Solution code:
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N = int(input())
    A = [0] + [int(input()) for _ in range(N)]

    cnt = 1
    pushed = set([1])
    q = deque()
    q.append(1)
    while len(q) > 0:
        a = q.popleft()
        b = A[a]
        if b == 2:
            return cnt
        elif b in pushed:
            return -1
        else:
            pushed.add(b)
            cnt += 1
            q.append(b)


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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]

next_light = 1
li = [0]
count = 0
for i in range(N):
    next_light = a[next_light-1]
    count += 1
    if 2 == next_light:
        break
if N == count:
    print(-1)
else:
    print(count)

Submission status: Accepted

Solution code:
n=int(input())
hoge=[]
for i in range(n):
  hoge.append(int(input()))
ans=0
punch=1
while 1:
  ans+=1
  punch=hoge[punch-1]
  if(punch-1==1):
    print(ans)
    break
  if(ans>=100001):
    print(-1)
    break

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]

cnt = 0
i = 1

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
i = 0
done = {a - 1: False for a in A}
num = 0
while True:
    i = A[i] - 1
    num += 1
    if i == 1:
        print(num)
        exit()
    if done[i]:
        print(-1)
        exit()
    done[i] = True

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
i = 0
done = {a - 1: False for a in A}
num = 0
while True:
    i = A[i] - 1
    num += 1
    if i == 1:
        print(num)
        exit()
    if done[i]:
        print(-1)
        exit()
    done[i] = True

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]


i = c = 0

for _ in range(N):
    i = a[i] - 1
    c += 1
    if i == 1:
        print(c)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
d=[int(input())-1for i in range(n)]
s=set()
s.add(0)
ans=0
b=0
while 1>0:
  if d[b] in s:
    ans=-1
    break
  b=d[b]
  s.add(b)
  ans+=1
  if b==1:
    break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = {}
for i in range(1, n+1):
    a[i] = int(input())
j = 1
c = 0
while (c < n):
    j = a[j]
    c += 1
    if j == 2:
        print(c)
        break
    if ((j != 2) & (c >= n)):
        print(-1)
        break

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
"""
x,a,b = LI()
if b<=a:
    print("delicious")
elif b-a <= x:
    print("safe")
else:
    print("dangerous")
"""
#B
n = I()
a = IR(n)
f = [0 for i in range(n)]
x = 0
f[x] = 1
for i in range(n):
    x = a[x]-1
    if x == 1:
        print(i+1)
        quit()
    if f[x]:
        print(-1)
        quit()
    f[x] = 1
#C

#D
"""
def root(x):
    if x == par[x]:
        return x
    par[x] = root(par[x])
    return par[x]

def same(x,y):
    return root(x) == root(y)

def unite(x,y):
    x = root(x)
    y = root(y)
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1
n = I()
v = LIR(n)
l = [None for i in range(2*n-2)]
for i in range(n):
    v[i].append(i)
par = [i for i in range(n)]
rank = [0 for i in range(n)]
v.sort(key = lambda x:x[0])
for i in range(n-1):
    l[i] = [v[i][2],v[i+1][2],v[i+1][0]-v[i][0]]
v.sort(key = lambda x:x[1])
for i in range(n-1):
    l[i+n-1] = [v[i][2],v[i+1][2],v[i+1][1]-v[i][1]]
l.sort(key = lambda x:x[2])
k = 0
ans = 0
for i in range(2*n-2):
    x,y,c = l[i]
    if not same(x,y):
        k += 1
        ans += c
        unite(x,y)
    if k == n-1:break
print(ans)
"""
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

Solution code:
N = int(input())
an = [int(input()) for _ in range(N)]

i = 0
c = 0
for _ in range(10**5):
    i = an[i]-1
    c += 1
    if i == 1:
        print(c)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))

idx = 1
count = 1

i = 0
while i < N:
    if a[idx - 1] == 2:
        print(count)
        break
    
    idx = a[idx - 1]
    count += 1
    i += 1
if i == N:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * (n+1)

for i in range(1, n+1):
    a[i] = int(input())

s = set()
temp = 1
ans = -1
count = 0
while temp not in s:
    s.add(temp)
    count += 1
    if a[temp] != 2:
        temp = a[temp]
    else:
        ans = count
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=[]

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

ind=0
cnt=1
while A[ind]!=2:
    ind=A[ind]-1
    cnt+=1
    if cnt>n:
        cnt=-1
        break

print(cnt)

Submission status: Accepted

Solution code:
def trained(N: int, A: list)->int:
    lightning = 1
    for i in range(N):
        if lightning == 2:
            return i
        lightning = A[lightning-1]
    return -1


if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    ans = trained(N, A)
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] + [int(input()) for _ in range(n)]

pos = 1
for i in range(n):
    pos = a[pos]
    if pos == 2:
        print(i+1)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a_list = [int(input()) for i in range(N)]
a = 0
c = 0
ans = -1
for l in range(N):
	c = c + 1
	a = a_list[a] - 1
	if a == 1:
		ans = c
		break
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]

memo = 1
cnt = 0
for i in A:
    memo = A[memo - 1]
    cnt += 1
    if memo == 2:
        print(cnt)
        exit()
print(-1)