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

Solution (Python):
N = int(input())
a = [int(input()) - 1 for _ in range(N)]
seen = [0] * N
seen[0] = 1
i = 0
ans = 1
while a[i] != 1:
    i = a[i]
    if not seen[i]:
        ans += 1
        seen[i] = 1
    else:
        break
else:
    print(ans)
    exit()
print(-1)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
sw = 1
ans = 0
for i in range(N):
    sw = A[sw-1]
    ans += 1
    if sw == 2:
        print(ans)
        exit()
print(-1)

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

now = 0
count = 0
while True:
    if count >= N:
        print(-1)
        break

    if now == 1:
        print(count)
        break

    now = A[now] - 1
    count += 1

Solution (Python):
N = int(input())
button = [int(input()) for _ in range(N)]
ans = 0
number = button[0]
while ans <= N + 5:
    if number == 2:
        print(ans + 1)
        exit()
    number = button[number - 1]
    ans += 1
print(-1)

Solution (Python):
N = int(input())
button = [int(input()) for _ in range(N)]
ans = 0
number = button[0]
while ans <= N + 5:
    if number == 2:
        print(ans + 1)
        exit()
    number = button[number - 1]
    ans += 1
print(-1)

Solution (Python):
N = int(input())
button = [int(input()) for _ in range(N)]
ans = 0
number = button[0]
while ans <= N + 5:
    if number == 2:
        print(ans + 1)
        exit()
    number = button[number - 1]
    ans += 1
print(-1)

Solution (Python):
def main():
    n = int(input())
    num_list = [int(input()) for _ in range(n)]

    light_no = 1
    for i in range(n):
        if light_no == 2:
            print(i)
            exit()
        light_no = num_list[light_no-1]
    
    print(-1)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
check = [0 for i in range(n)]
now = 1
ans = 0
while check[now-1] == 0:
    if now == 2:
        print(ans)
        break
    check[now-1] = 1
    now = a[now-1]
    ans += 1
else:
    print(-1)

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

current_bottom = a[0]
ans = 1
if current_bottom == 2:
    print(ans)
    exit()

for _ in range(N):
    current_bottom = a[current_bottom-1]
    ans += 1
    if current_bottom == 2:
        print(ans)
        exit()
print(-1)

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

pushed = [0] * N
pushed[0] = 1

current_bottom = a[0]
ans = 1
if current_bottom == 2:
    print(ans)
    exit()

for _ in range(N):
    current_bottom = a[current_bottom-1]
    if pushed[current_bottom-1] == 1:
        print(-1)
        exit()
    else:
        pushed[current_bottom-1] = 1
    ans += 1
    if current_bottom == 2:
        print(ans)
        exit()

Solution (Python):
import sys

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


switch = 1

c = 0
while True:
    switch = a[switch-1]
    c += 1
    if switch == 2:
        break
    
    if c > 10**6:
        print(-1)
        sys.exit()
        break

print(c)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]

cnt = 0
pos = 0

while True:
    if pos == 1:
        print(cnt)
        break
    if cnt >= N:
        print(-1)
        break

    pos = A[pos] - 1
    cnt += 1

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]

ans = 0

for i in range(N):
    if i == 0:
        ans += 1
        next_pos = A[i]
        if next_pos == 2:
            break
        else:
            continue
        
    ans += 1
    next_pos = A[next_pos-1]

    if next_pos == 2:
        break
    else:
        continue

if ans < N:
    print(ans)
else:
    print(-1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
cnt = 0
tmp = 1
for i in A:
    tmp = A[tmp-1]
    cnt += 1
    if tmp == 2:
        print(cnt)
        exit()
print("-1")

Solution (Python):
import sys

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

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

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

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

print(-1)

Solution (Python):
n=int(input())
a=[0]*n
for i in range(n):
  a[i]=int(input())-1

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

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

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

print(cnt)

Solution (Python):
n=int(input())
count={}
a=[]
for i in range(n):
    tmp=int(input())
    a.append(tmp)
button=a[0]
cnt=1

while(True):
    if button==2:
        print(cnt)
        exit()
    else:
        if button in count:
            print(-1)
            exit()
        else:
            count[button]=1
            button=a[button-1]
            cnt+=1

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append(int(input())-1)

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

Solution (Python):
n = int(input())
a = list()
for _ in range(n):
    a.append(int(input())-1)
g = 0
cnt = 1
while cnt <= n:
    g = a[g]
    if g == 1:
        print(cnt)
        exit()
    cnt += 1
print(-1)

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

a = A[0]
ans = True if a == 2 else False
cnt = 0
for i in range(N):
    cnt += 1
    if a == 2:
        ans |= True
        break
    a = A[a - 1]

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

Solution (Python):
N = int(input())
A = []
for _ in range(N):
    ai = int(input())
    A.append(ai)

shined_button = A[0]
btn_attempt = 1
while True:
    if shined_button == 2:
        print(btn_attempt)
        break
    shined_button = A[shined_button-1]
    btn_attempt +=1

    if btn_attempt > N+1:
        print(-1)
        break

Solution (Python):

import math




n = int(input())


a = []
check = [0]*n
for i in range(n):
    a.append(int(input()))

find = False
i = 0
check[0]=1
count = 1
while not find:
    if a[i]!=2:
        if check[a[i]-1]==0:
            check[a[i]-1]=1
            count += 1
        else:
            print(-1)
            exit()

        i = a[i]-1   
    else:
        find=True


print(count)

Solution (Python):
n=int(input())
A=[]
for i in range(n):
    a=int(input())
    A.append(a-1)
t = 0
r = 0
while True:
    t = A[t]
    r += 1
    if t == 1:
        print(r)
        break
    if r == n:
        print(-1)
        break

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

b_set=set()

ans=1
now=0
while True:
    nxt=a[now]-1
    if nxt==1:
        print(ans)
        exit()
    else:
        if nxt in b_set:
            print(-1)
            exit()
        b_set.add(nxt)
        ans+=1
        now=nxt

Solution (Python):
def solve():
    N = int(input())
    a = [int(input()) for i in range(1, N + 1)]
    
    l = []
    s = a[0]
    l = [0] * N
    c = 1
    l[0]=True
    while(True):
        l[s - 1] = True
        if s == 2:
            print(c)
            return True
        s = a[s - 1]
        if l[s - 1] == True:
            print(-1)
            return False
        c += 1
solve()

Solution (Python):
n = int(input())
l = list(int(input()) for _ in range(n))
num = 1
ans = -1

for i in range(n):
    if num == 2: 
        ans = i
        break
    num = l[num-1]
print(ans)

Solution (Python):
n = int(input())
a_list = []
for i in range(n):
	a_list.append(int(input()))
r = -1
num = 1
count = 0
while True:
	num = a_list[num-1]
	count += 1
	if num == 2:
		r = count
		break
	if count == n:
		break
print(r)

Solution (Python):


N = int(input())
a_list = []

for i in range(N):
    a = int(input())
    a_list.append(a)


current = 0
count = 0
for _ in range(N):
    current = a_list[current] - 1
    count += 1

    if current == 1:
        print(count)
        exit()

print(-1)

Solution (Python):
import sys
import os

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    B = iln(N)
    b = 1
    for n in range(N+1):
        b = B[b-1]
        if b == 2:
            print(n+1)
            break
    else:
        print(-1)


if __name__ == '__main__':
    main()

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

ans, nex = 1, a[0]
while ans < N and nex != 2:
	 nex = a[nex - 1]
	 ans += 1
	 
print(ans if ans != N else -1)

Solution (Python):
import sys
import os

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    B = iln(N)
    t = 1
    cnt = 1
    while True:
        if cnt > N:
            print(-1)
            exit()
        if B[t-1] == 2:
            print(cnt)
            exit()
        cnt += 1
        t = B[t - 1]


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
direction = []
click = [0]*n
click[0] = 1

for i in range(n):
  direction.append(int(input()))
  
index = 0
cnt = 0
while cnt<=100000:
  cnt += 1
  index = direction[index] - 1
  click[index] += 1
  
  if index == 1:
    break
  if click[index] ==2:
    cnt = -1
    break
   
print(cnt)

Solution (Python):
import sys

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

flag = False
button = 0
cnt = 0
for i in range(1, n+1):
    if a[button] == 2:
        cnt = i
        flag = True
        break
    else:
        button = a[button] - 1

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

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

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

used = [0]*N
ans = 0
i = 0
for _ in range(N):
  used[i] = 1
  i = A[i]-1
  ans += 1
  if used[i] == 1:
    ans = -1
    break
  if i == 1:
    break
print(ans)

Solution (Python):
import sys

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

l = 1
p = a[0] - 1

if a[0] == 2:
    print(l)
    sys.exit()

while l < int(2e5):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print(-1)

Solution (Python):
import sys

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

l = 1
p = a[0] - 1

if a[0] == 2:
    print(l)
    sys.exit()

while l < int(1e6):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
now = 0
cnt = 0
for i in range(n):
    
    now = a[now] - 1
    cnt += 1
    if now == 1:
        
        print(cnt)
        
        exit()
print("-1")

Solution (Python):
n = int(input())
a = [int(input())-1 for i in range(n)]
on = 0
for i in range(n):
    on = a[on]
    if on == 1:
        print(i+1)
        exit()
print(-1)

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

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
b = [0] * n
ans = 0
c = 0
b[c] = 1
while True:
    c = a[c] - 1
    ans += 1
    if b[c] == 1:
        ans = -1
        break
    if c == 1:
        break
    b[c] = 1
print(ans)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
if 2 not in a:
    print(-1)
    quit()
else:
    s = 1
    count = 0
    while count <= n:
        s = a[s-1]
        count += 1
        if s == 2:
            print(count)
            quit()
print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
if 2 not in a:
    print(-1)
    quit()
else:
    t = a.index(2)
    s = 1
    count = 0
    while count <= 10**6:
        s = a[s-1]
        count += 1
        if s == 2:
            print(count)
            quit()
print(-1)

Solution (Python):
n=int(input())
a = [int(input()) for _ in range(n)]
nex=[]
k=1
for i in range(len(a)):
    nex.append(a[k-1])
    k=nex[-1]
    if nex[-1]==2:
        print(len(nex))
        break
    elif len(nex)>=n:print(-1)
    else:pass

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]
index = a[0] - 1
cnt = 1
flg = False
for i in range(len(a)):
    if index==1:
        print(cnt)
        flg = True
        break
    index = a[index] - 1
    cnt += 1
if flg:
    pass
else:
    print(-1)

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
ans = 0
light = 1
list = [0] * n

while True:
    if light == 2:
        break
    elif list[light - 1] == 1:
        ans = -1
        break
    else:
        ans = ans + 1
        list[light - 1] = 1
        light = A[light - 1]

print(ans)

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

ans=0
t=1
while t!=2 and ans<100001:
	ans=ans+1
	t=a[t-1]
if ans>100000:
	print(-1)
else:
	print(ans)

Solution (Python):
n = int(input())
table = {}
for i in range(n):
    a = int(input())
    table[i] = a-1

is_visited = [ False for _ in range(n) ]
is_visited[0] = True

cnt = 0
now = 0
while True:
    is_visited[now] = True
    if now == 1:
        print(cnt)
        break
    nxt = table[now]
    if is_visited[nxt]:
        print(-1)
        break
    now = nxt
    cnt += 1

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

cnt, i = 0, 0
for j in range(len(a)):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        
        quit()
    else:
        i = a[i] - 1
print(-1)

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

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

Solution (Python):
N = int(input())
a = list()
for i in range(N):
    x = int(input())
    a.append(x)

c = 0
l = 1

for i in a:
    l = a[l-1]
    c += 1
    if l == 2:
        break
if l != 2:
    c = -1
print(c)

Solution (Python):
n=int(input())
root = [[] for i in range(n)]
for i in range(n):
    a=int(input())
    root[i].append(a-1)


stack=[0]
count = 1
while len(stack):
    v = stack.pop()
    for i in root[v]:
        if i == 1:
            print(count)
            exit()
        count += 1
        stack.append(i)
        if count == 1000000:
            print(-1)
            exit()

print(-1)

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

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

print(-1)

Solution (Python):
n = int(input())

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

count = 1
right = btn_list[1]

while True:
    if right == 2:
        print(count)
        break
    elif count == n:
        print(-1)
        break
    else:
        count += 1
        right = btn_list[right]

Solution (Python):
n = int(input())

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

count = 1
right = btn_list[1]

while True:
    if right == 2:
        print(count)
        break
    elif count == n:
        print(-1)
        break
    else:
        count += 1
        right = btn_list[right]

Solution (Python):
n = int(input())

buttons = [0] * 10**5

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

next = 1
i = 0
while i <= 10**5:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()

print(-1)

Solution (Python):
n = int(input())

buttons = []

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

next = 1
i = 0
while i <= 10**5:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()

print(-1)

Solution (Python):
n = int(input())

buttons = [0] * 10**5

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

next = 1
i = 0
while next != 2:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()
    if i >= 10**5:
        print(-1)
        break

Solution (Python):
N = int(input())
As = [int(input())-1 for i in range(N)]
seen = [-1] * N

from collections import deque
q = deque()
q.append(0)
seen[0] = 1
cnt = 1
while len(q) > 0:
    a = q.popleft()
    n = As[a]
    
    if n == 1:
       print(seen[a])
       exit()
    if seen[n] > -1: continue
    seen[n] = seen[a] + 1
    q.append(n)
print(-1)

Solution (Python):

N = int(input())
A = [int(input()) for i in range(N)]
A.insert(0,0)
flg = [False]*(N+1)
idx = 1
ans = -1
cnt = 0
while not flg[2]:

    if flg[A[idx]]:
        break
    else:
        flg[A[idx]]=True
        idx = A[idx]
        cnt +=1
if flg[2]:
    ans = cnt
print(ans)

Solution (Python):
N=int(input())
List = [int(input())-1 for i in range(N)]
count = 1
a = List[0]
while(a!=1 and count<=N):
    count+=1
    a=List[a]
if count==N+1:
    print(-1)
else:
    print(count)

Solution (Python):
import sys

N = int(input())
a = [0]
for _ in range(N):
    a.append(int(input()))
s = 1
for i in range(N):
    tmp = a[s]
    if tmp == 2:
        print(i+1)
        sys.exit()
    else:
        s = tmp
print(-1)

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

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

Solution (Python):
import queue
n=int(input())
a=[int(input()) for _ in range(n)]
q=queue.Queue()
q.put(1)
ans=0
for i in range(n):
    b=q.get()
    if b==2:
        print(ans)
        exit()
    q.put(a[b-1])
    ans+=1
print(-1)

Solution (Python):
n=int(input())
a=[0]
vis=[0]*(n+1)
for _ in range(n):
    x=int(input())
    a.append(x)
ans=0
cur=1
while vis[cur]==0:
    
    vis[cur]=1
    if cur==2:
        break
    ans+=1
    cur=a[cur]
if vis[2]==0:
    print('-1')
else:
    print(ans)

Solution (Python):
n = int(input())
listA = list(int(input()) for i in range(n))
t = 0
s =-1
for i in range(n):
  a =listA[t]
  if a==2:
    s=i+1
    break
  else:
    t = a-1
print(s)

Solution (Python):
N = int(input())

dic = {}
for n in range(1,N+1):
  dic[n] = int(input())

now = 1
count = 0
flag = False
while True:
  if now==2:
    flag = True
    break
  if now not in dic:
    break
  now = dic.pop(now)
  
  count += 1

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

Solution (Python):

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

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

i_next = 1

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

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

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

Solution (Python):
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

l = [False] * n
p = 0
f = False
cnt = 0
while not l[p]:
    cnt += 1
    l[p] = True
    p = a[p] - 1
    if (p == 1):
        f = True
        break

print(cnt if f else -1)

Solution (Python):
N=int(input())




import sys
a=[int(i) for i in sys.stdin]

count=0




loop=set()

tmp=1
while True:  
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp-1 in loop:
    print(-1)
    exit()
  
  loop.add(tmp-1)

Solution (Python):
N=int(input())


a = []

for i in range(N):
    a.append(int(input()))



count=0




loop=set()

tmp=1
while True:  
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp-1 in loop:
    print(-1)
    exit()
  
  loop.add(tmp-1)

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
x=1
for k,i in enumerate(a):
	x=a[x-1]
	if x==2:
		print(k+1)
		break
else:
	print(-1)

Solution (Python):
N=int(input())
a=[0]*N
for i in range(N):
  a[i]=int(input())
count=0



loop=set()

tmp=1
while True:  
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp-1 in loop:
    print(-1)
    exit()
  loop.add(tmp-1)

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

now = 0
count = 0
checked = [-1] * N

while checked[now] == -1 and now != 1:
    checked[now] = 1
    now = a[now] - 1
    count += 1
if now == 1:
    print(count)
else:
    print(-1)

Solution (Python):
N = int(input())

buttons = {}
for i in range(N):
    buttons[i+1] = int(input())
    
pushed = []

b = 1
cnt = 0
flag = False
for i in range(N):
    cnt += 1
    next_button = buttons[b]
    if next_button == 2:
        flag = True
        break
    b = next_button
        
if flag:
    print(cnt)
else :
    print(-1)

Solution (Python):
N = int(input())
A = [int(input())-1 for _ in range(N)]

visited = {0}

G = 1
t = 0
for i in range(N):
    n = A[t]
    if n in visited:
        print(-1)
        break
    elif n == G:
        print(i+1)
        break
    else:
        visited.add(n)
        t = n
else:
    print(-1)

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
a = nin(n)

i = 0
ans = 0
flag = True
while i != 1:
    if ans > n:
        flag = False
        break
    i = a[i] - 1
    ans += 1

print(ans if flag else -1)

Solution (Python):
import sys
inputs = list(map(int, sys.stdin))
ai = inputs[1]
for i in range(inputs[0]):
    if (ai == 2):
        print(i + 1)
        break
    ai = inputs[ai]
else:
    print(-1)

Solution (Python):
import sys

N = list(map(int, sys.stdin))
bot = N[1]

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

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
isFound = False
ai = 1
for i in range(1, N + 1):
    ai = a[ai - 1]

    if (ai == 2):
        isFound = True
        print(i)
        break
if (isFound is False):
    print(-1)

Solution (Python):
n=int(input())
a=[]
for _ in range(n):
    a.append(int(input()))

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

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

k = 1
p = False
for i in range(n):
    if a[k-1] == 2:
        print(i+1)
        p = True
        break
    k = a[k-1]
if not p:
    print(-1)

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

now=1
cnt=0
for j in range(1,N+1):
  
  if a[now]==2:
    cnt +=1
    print(cnt)
    break
  elif a[now]==now:
    print(-1)
    break
  else:
    now=a[now]
    cnt += 1
else:
  print(-1)

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
ch=[0]*n
now=1
ans=0
ch[0]=1

while 1:
    now=a[now-1]
    ans+=1
    
    if now==2:
        print(ans)
        exit()
    else:
        if ch[now-1]==1:
            print("-1")
            exit()
        else:
            ch[now-1]=1

Solution (Python):
n=int(input())
count=0
A=[]
for i in range(n):
  A.append(input())
temp=1
ans = -1
for i in A:
  temp=A[int(temp)-1]
  count+=1
  if temp=='2':
    ans=count
    break
print(ans)

Solution (Python):
N = int(input())
a_dic = {}
for i in range(1, N+1):
    a = int(input())
    a_dic[i] = a
lamp = 1
cnt = 0
ans = -1
while(cnt<N):
    if lamp in a_dic:
        lamp = a_dic[lamp]
    else:
        break
    cnt += 1
    if lamp==2:
        ans = cnt
        break
print(ans)

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

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

Solution (Python):












mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n = int(input())
    a = []
    for i in range(n):
        a.append(int(input()))
    b = [0] * n
    b[0] = 1
    i = 0
    ans = 0
    while True:
        b[a[i]-1] = 1
        ans += 1
        i = a[i]-1
        if b[1]:
            break
        elif b[a[i]-1]:
            ans = -1
            break
    print(ans)
    return

if __name__=='__main__':
    main()

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
c=0
for i in range(1,n+1):
  c=a[c]-1
  if c==1:
    print(i)
    break
else:
  print(-1)

Solution (Python):
n=int(input())
l=[int(input()) for _ in range(n)]
t=0
for i in range(1,n+1):
    t=l[t]-1
    if t == 1:
        print(i)
        break
else:
    print(-1)

Solution (Python):
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))

a.insert(0,0)
ans = 0
i = 1

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

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
idx=1

for i in range(1,n+1):
    idx=a[idx-1]
    
    if idx==2:
        print(i)
        exit()
print(-1)

Solution (Python):












n = int(input())

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

ans = 0
btn = 1
if not 2 in lists:
    ans = -1
else:
    while ans <=n:
        if lists[btn-1]==2:
            ans +=1
            break
        else:
            btn = lists[btn-1]
            ans +=1
            continue
    else:
        ans = -1

print(ans)

Solution (Python):



import sys
sys.setrecursionlimit(10000000)



def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,=pin()
    l=[int(input())-1 for i in range(N)]
    i=0
    ans=0
    while(i!=1):
        i=l[i]
        ans+=1
        if ans>N*10:
            print(-1)
            return
    print(ans)
    return
"""         


"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """3
3
1
2"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """4
3
4
1
2"""
        output = """-1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """5
3
3
4
2
4"""
        output = """3"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Solution (Python):
N=int(input())
a={i+1:int(input()) for i in range(N)}

cnt=1
flag=0
b=a[1]

for i in range(N):
    if b==2:
        flag=1
        break
    else:
        b=a[b]
        cnt+=1
    

print(cnt if flag else -1)

Solution (Python):
n = int(input())
button = [int(input()) for i in range(n)]
button.insert(0,0)

ans = -1
tap_button = button[1]
for i in range(1,n):
    if tap_button == 2:
        ans = i
        break
    k = tap_button
    tap_button = button[k]
    
print(ans)

Solution (Python):

n=int(input())
a=[]
for i in range(n):
    x=int(input())
    a.append(x)

ans=-1
cnt=0
switch=1
while cnt<n:
    cnt+=1
    switch=a[switch-1]
    if switch==2:
        ans=cnt
        break
print(ans)

Solution (Python):
n = int(input())
A = [int(input())-1 for _ in range(n)]

visited = [0]*n
ans = 0
i = 0
while True:
  i = A[i]
  ans += 1  
  if visited[i]==1:
    ans = -1
    break    
  visited[i] = 1
  if i == 1:
    break
  
print(ans)

Solution (Python):
n = int(input())
a = list(int(input()) for i in range(n))

ans = a[0]
count = 0
for i in range(1, n):
    count += 1
    if ans == 2:
        print(count)
        exit()
    ans = a[ans-1]


print(-1)

Solution (Python):

import sys
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    N = INT()
    Button = [INT() for _ in range(N)]

    count = 0
    isVisited = [False]*N
    GOAL = 1

    cur = 0
    while True:
        next = Button[cur]-1
        count += 1

        if next == GOAL:
            print(count)
            return
        
        if isVisited[next]:
            print(-1)
            return
        isVisited[next] = True
        cur = next
        
    return

if __name__ == '__main__':
    main()

Solution (Python):








def resolve():
    N=int(input())

    A=[int(input()) for i in range(N)]
    num=0
    cnt=0

    for i in range(N):
        if A[num]==2:
            print(cnt+1)
            break
        num=A[num]-1
        cnt+=1

        if i==N-1:
            print(-1)

resolve()

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

num = 1
cnt = 1

for i in range(n):
    if a[num-1] == 2:
        print(cnt)
        exit(0)
    num = a[num-1]
    cnt += 1

print("-1")

Solution (Python):
N = int(input())
done = [0] * N
button = [int(input()) for _ in range(N)]
done[0] = 1
i = 0
ans = 1
for _ in range(N):
  i = button[i] - 1
  if i == 1:
    print(ans)
    exit()
  if done[i] == 1:
    print(-1)
    exit()
  else:
    done[i] = 1
    ans += 1