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):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, a: "List[int]"):
    k = 0
    for i in range(N + 1):
        if k == 1:
            print(i)
            return
        k = a[k] - 1
    print(-1)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(N, a)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
light_position =1
light_position = a[light_position-1]
count = 1
while True:
    if light_position == 2:
        break
    elif light_position == 1 or count > n:
        count = -1
        break        
    light_position = a[light_position-1]
    count += 1
print(count)

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

Solution (Python):
def resolve():
    n = int(input())
    l = [int(input()) for i in range(n)]

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

    return print(-1)

if __name__ == '__main__':
    resolve()

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
a = [int(input()) for _ in range(N)]
ans = -1
step = 0
idx = 0
while True:
    if step == N:
        break
    next = a[idx]
    idx = next-1
    step += 1
    if next == 2:
        ans = step
        break
print(ans)

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

cur = 1
cnt = 0
flag = False
while data[cur] > 0:
    if cur == 2:
        flag = True
        break
    a = data[cur]
    data[cur] = 0
    cnt += 1
    cur = a

print(cnt if flag else -1)

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

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
used = {1}
pos = 1
count = 0
for _ in range(n):
  pos = A[pos - 1]
  count += 1
  if pos in used:
    print(-1)
    break
  used.add(pos)
  if pos == 2:
    print(count)
    break

Solution (Python):
N=int(input())
list=[]
for b in range(N):
    b=int(input())
    list.append(b)
number=1
c=list[0]
numbers=[]
numbers.append(c)
if c==2:
    print(number)
else:
    while True:
        number+=1
        d=list[numbers[number-2]-1]
        numbers.append(d)
        if d==2:
            print(number)
            break
        elif number==N+1:
            print(-1)
            break
        else:
            continue

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

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

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

else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
i = a[0]
cnt = 1
while cnt<=n and i!=2:
    i = a[i-1]
    cnt += 1
if cnt==n+1:print(-1)
else:print(cnt)

Solution (Python):
import sys
n=int(input())
an=[0]*(n+1)
for i in range(n):
  an[i+1]=int(input())

t=1
cnt=0
for _ in range(n*2):
  t=an[t]
  cnt+=1
  if t==2:
    print(cnt)
    sys.exit()
print(-1)

Solution (Python):


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)

Solution (Python):
N = int(input())
a = [0]*N
for i in range(N):
  a[i]=int(input())
cur = 1-1
ret = 0
v = set()
while cur != 2-1:
  
  if cur in v:
    print(-1)
    exit()
  v.add(cur)
  cur = a[cur]-1
  ret += 1
print(ret)

Solution (Python):
n=int(input())
l=[]
cnt=0

for i in range(0,n):
            a=int(input())
            l.append(a)
j=0
for i in range(0,n+1):
            if l[j]!=2:
                        cnt+=1
                        j=int(l[j])-1
                        
            else:
                         print(cnt+1)
                         exit()
print(-1)
exit()

Solution (Python):
N = int(input())
a = []
for i in range(N):
    ai = int(input())
    a.append(ai)
cnt = 1
for i in range(N):
    cnt = a[cnt-1]
    if cnt == 1:
        print(-1)
        exit()
    elif cnt == 2:
        print(i+1)
        exit()
print(-1)

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

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

nb = 1
for i in range(N):
  nb = A[nb-1]
  if nb == 2:
    print(i+1)
    exit()
print(-1)

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

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



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

roop = 0
now = 0
while roop < n+1:
    if now == 1:
        print(roop)
        break
    else:
        now = a[now]-1
        roop += 1
else:
    print(-1)

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    a=[0]*N
    for i in range(N):
        a[i]=I()
        
    used=[0]*N
    used[0]=1
    now=0
    cnt=0
    f=0
    for i in range(N):
        cnt+=1
        now=a[now]-1
        if now==1:
            f=1
            break
        if used[now]==1:
            break
        used[now]=1
        
    if f==1:
        print(cnt)
    else:
        print(-1)

main()

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

ans = -1
nxt = 1
cnt = 0
visited = [0] * 100000
while True:
    if nxt == 2:
        ans = cnt
        break
    nxt = nums[nxt - 1]
    if visited[nxt - 1] == 1:
        break
    visited[nxt - 1] = 1
    cnt += 1

print(ans)

Solution (Python):
N=int(input())
a=[]
for i in range(N):
    a.append(int(input()))
seen=[False]*N
cnt=0
now=0
while(True):
    if(seen[now]):
        print(-1)
        exit()
    elif(now==1):
        print(cnt)
        exit()
    else:
        seen[now]=True
        cnt+=1
        now=a[now]-1

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

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

ans = 0
var = 0

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

Solution (Python):
N = int(input())
D = {i+1:int(input()) for i in range(N)}
d = D[1]
ans = 1


for i in range(N):
  if d==2:
    print(ans)
    exit()
    
  d = D[d]
  ans +=1
  
  
print(-1)

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

hist = set()
next_i = 0
ans = 0
for i in range(N):
    ans += 1
    hist.add(next_i)
    next_i = a[next_i]
    if next_i == 1:
        break
    elif next_i in hist:
        ans = -1
        break

print(ans)

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

hist = set()
next_i = 0
ans = 0
for i in range(N):
    ans += 1
    hist.add(next_i)
    next_i = a[next_i]
    if next_i == 1:
        break
    elif next_i in hist:
        ans = -1
        break

print(ans)

Solution (Python):
n = int(input())
dic = {i+1:int(input()) for i in range(n)}
light = dic[1]
ans = 1

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

Solution (Python):
N=int(input())
D={i+1:int(input()) for i in range(N)}
d=D[1]
ans=1
for i in range(N):
    if d==2:
        print(ans)
        exit()
    d=D[d]
    ans+=1
print(-1)

Solution (Python):
import sys
n=int(input())
a=[int(i) for i in sys.stdin] 
b=1
count=0
for _ in range(n):
    b=a[b-1]
    count+=1
    if b==2:
        print(count)
        exit()
print(-1)

Solution (Python):


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

i = 1
for _ in range(n):
    
    b.append(i)
    i = a[i-1]
    
    
        
if 2 in b:
    print(b.index(2))
else:
    print(-1)

Solution (Python):

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

ans = 0
i = 1
while i != 2:
    i = a[i-1]
    
    ans += 1
    if ans >= n:
        print(-1)
        sys.exit()
print(ans)

Solution (Python):
n = int(input())
l = [int(input()) for i in range(n)]
x = 0
check = [True] * n
ans = 0
flg = False
while (check[x]):
    if x == 1:
        flg = True
        break
    else:
        ans += 1
        check[x] = False
        x = l[x] - 1
if flg:
    print(ans)
else:
    print(-1)

Solution (Python):

n=int(input())
a=[None]+[int(input()) for _ in range(n)]
x=1
ans=0
while x!=2 and ans<n:
    x=a[x]
    ans+=1
print(ans if ans<n else -1)

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

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

checker = {}
nxt = 0
count = 0
while(True):
    checker.setdefault(nxt, False)
    if nxt == 2 - 1:
        break
    elif not checker[nxt]:
        checker[nxt] = True
        count += 1
        nxt = al[nxt]
    else:
        count = -1
        break
    
print(count)

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

x = 1
ans = 1
while A[x-1] != 2:
    x = A[x-1]
    ans += 1
    if ans == 10**5:
        ans = -1
        break
    
print(ans)

Solution (Python):


button_count = int(input().rstrip())

buttons = [int(input().rstrip()) - 1 for i in range(button_count)]

current = 0
flag = [False for i in range(button_count)]
ans = 0
for i in range(button_count):
    if flag[current]:
        ans = -1
        break
    if current == 1:
        break
    flag[current] = True
    current = buttons[current]
    ans += 1

print(ans)

Solution (Python):


N = int(input())
A = [0] * (N+1)
Flg = True
for i in range(N):
    a = int(input())
    if a == 2:
        Flg = False
    A[i+1] = a

if Flg:
    print(-1)

else:
    ans = -1
    i = 1
    cnt = 0
    while i != 2:
        i = A[i]
        cnt += 1
        if cnt > N:
            print(-1)
            Flg = True
            break
    if Flg == False:
        print(cnt)

Solution (Python):

N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
cnt=0
flg=False
light=0

for i in range(N):
    light=A[light]-1
    cnt+=1
    if light==1:
        flg=True
        print(cnt)
        break

if not(flg):
    print(-1)

Solution (Python):
n = int(input())
al = list(int(input()) for _ in range(n))

a = 1
cnt = 0
while True:
    cnt += 1
    a = al[a-1]
    if a == 2:
        print(cnt)
        break
    if cnt == n:
        print(-1)
        break

Solution (Python):
n = int(input())
count = 0
a = list()
for i in range(n):
    a.append(int(input()))
button = 0
for i in range(n):
    count += 1
    button = a[button]-1
    if button == 1:
        print(count)
        exit()
print(-1)

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

if l[0] == 2:
    print(1)
    exit()
else:
    x = l[0]-1
    z = 1
    for i in range(10**5):
        z += 1
        if l[x] == 2:
            break
        else:
            x = l[x]-1

if z == 100001:
    print('-1')
else:
    print(z)

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

if l[0] == 2:
    print(1)
    exit()

x = l[0]-1
z = 1
for i in range(10**5):
    z += 1
    if l[x] == 2:
        break
    else:
        x = l[x]-1

if z == 100001:
    print('-1')
else:
    print(z)

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

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
now = 0
res = 0
for i in range(N + 1):
    if now == 1:
        break
    now = a[now] - 1
    res += 1
print(-1 if res == N + 1 else res)

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

Solution (Python):
n = int(input())
l = []
o = set()
t = 0
c = 0
ans = -1
for i in range(n):
    l.append(int(input())-1)

for i in range(n):
    if l[t] == 1:
        ans = c+1
        break
    else:
        if t in o:
            break
        else:
            c += 1
            o.add(t)
            t = l[t]

print(ans)

Solution (Python):
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)

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

count = 1
now = a[0]

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

Solution (Python):
n = int(input())
a = [0] + [int(input()) for _ in range(n)]
pushed = [0]*(n+1)

na = a[1]
cnt = 1
pushed[na] = 1
while True:
    if na == 2:
        break
    na = a[na]
    cnt += 1
    if pushed[na] == 1:
        cnt = -1
        break
    pushed[na] = 1
print(cnt)

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

check = [0 for _ in range(n)]
check[0] = 1
i = a[0]
c = 1
while i != 1 and check[i]==0:
    check[i] = 1
    i = a[i]
    c += 1
    
if i==1:
    print(c)
else:
    print(-1)

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

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



cursor = 0
roots = [0] * n

for i in range(1,n):
    roots[cursor] = 1
    cursor = a[cursor]-1
    if (cursor == 1):
        print(i)
        break
    elif (roots[cursor] == 1):
        print(-1)
        break

Solution (Python):
n = int(input())
a = []
f = [False]*n
for _ in range(n):
    a.append(int(input()))
cnt = 0
i = 0
while(True):
    if f[i]:
        cnt = -1
        break
    if i == 1:
        break
    f[i]=True
    cnt += 1
    i = a[i]-1
print(cnt)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    a_list = list([int(input())-1 for _ in range(N)])
    pushed_a = set([0])
    i = 0
    ans = 0
    while True:
        ans += 1
        next_a = a_list[i]
        if next_a == 1:
            break
        elif next_a in pushed_a:
            ans = -1
            break
        else:
            pushed_a.add(a_list[i])
            i = next_a
    print(ans)

if __name__ == '__main__':
    main()

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

    btn = 1
    hist = set()
    while True:
        if btn == 2:
            print(len(hist))
            break

        if btn in hist:
            print(-1)
            break

        hist.add(btn)
        btn = a[btn - 1]


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = 1
ans = 0
two = False

for i in range(len(a)):
    b = a[b-1]
    ans += 1
    if b == 2:
        two = True
        break

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

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def main():
	a = int(input())
	al=[]
	for i in range(a):
		b = int(input())
		al.append(b)
		
	opened_list=set()
	st=0
	opened_list.add(st)
	count=0
	while st != 1:
		st = al[st]-1
		count+=1
		if st in opened_list:
			return -1
		opened_list.add(st)
			
	return count
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
def main():
    buttons = int(input())
    next_lighten_up = [int(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()

Solution (Python):
n = int(input())
a = []

for _ in range(n):
    a.append(int(input()) - 1)

cnt = 0
current = 0
s = set()
while True:
    current = a[current]
    if current in s:
        print(-1)
        exit(0)
    s.add(current)
    cnt += 1
    if current == 1:
        print(cnt)
        exit(0)

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

next = list_a[0]
ans = 1

for i in range(1,N+1) :
    if next == 2 :
        print(ans)
        exit()
    next = list_a[next-1]
    ans += 1

print(-1)

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
now = 1
ans = -1
for k in range(N):
    now = a[now-1]
    if now ==2:
        ans = k +1
        break
    
print(ans)

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

Solution (Python):
def I(): return int(input())
def MI(): return map(int, input().split())
def LMI(): return list(map(int, input().split()))

N = I()
A = [I() for _ in range(N)]

seen = set()
i = 0
light = 1
while light != 2:
    seen.add(light)
    light = A[light - 1]
    if light in seen:
        print(-1)
        exit(0)
    i += 1
print(i)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
x = {0}
now = 0
cnt = 0
while True:
    now = a[now] - 1
    cnt += 1
    if now == 1:
        print(cnt)
        break
    if now in x:
        print(-1)
        break
    x.add(now)

Solution (Python):
N = int(input())
next = []
for i in range(N):
    next.append(int(input()))
count = 0
light = 1
for i in range(N):
    light = next[light - 1]
    count += 1
    if light == 2:
        print(count)
        exit()
print(-1)

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

Solution (Python):



N = int(input().strip())
A_list = [int(input().strip()) for _ in range(N)]


A_list.insert(0,0)

pressed_button={}
end_flag=False
index_button=1
count=0
while end_flag == False:
    count += 1
    pressed_button[str(index_button)] = True
    
    
    index_button = A_list[index_button]
    if ( str(index_button) in pressed_button ) or (index_button == 2):
        end_flag = True

if index_button == 2:
    print(count)
else:
    print("-1")

Solution (Python):
N = int(input())
a_li = [int(input()) for i in range(N)]
ans = 0
now = a_li[0]

while ans <= N :
    ans += 1
    if now == 2 :
        print(ans)
        exit()
    now = a_li[now - 1]
print(-1)

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

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

tmp = a[0]
c = 0

while c != (n + 1):
    
    if tmp == 2:
        print(c+1)
        exit()
    else:
        tmp = a[tmp-1]
        c += 1

print(-1)

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

i = 0
button = 0
while i <= n:
  i += 1
  button = li[button]-1
  
  if button == 1:
    break
  
  


if button == 1:
  print(i)
else:
  print(-1)

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

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
b=[1]
buf=1
count=0
if 2 not in a:
    print(-1)
else:
    while count<n:
        a_buf=a[buf-1]
        
        if a_buf==2:
            
            print(len(b))
            break
        
            
            
            
        b.append(a_buf)
        buf=a_buf
        count+=1
    if count==n:
        print(-1)

Solution (Python):
n=int(input())
a=[0]
for _ in range(n):
    a.append(int(input()))
visited=set([1])
answer=0
x=1
while True:
    x=a[x]
    if x in visited:
        answer=-1
        break
    visited.add(x)
    answer+=1
    if x==2:
        break
print(answer)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
lis = ["0"]*n
bot = a[0]
lis[0] = "1"
cnt = 1

while True:
    if bot == 2:
        print(cnt)
        break
    elif bot != 2 and lis[bot-1] == "1":
        print(-1)
        break
    else:
        cnt += 1
        lis[bot-1] = "1"
        bot = a[bot-1]

Solution (Python):
n=int(input())
l=[int(input()) for i in range(n)]
count=0

for i in range(n):
  if l[count]==2:
    print(i+1)
    exit()
  else:
    count=l[count]-1
print(-1)

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

Solution (Python):
n = int(input())
l = [int(input()) for i in range(n)]
ans = {}
i = 1
while True:
  i = l[i-1]
  if i in ans:
    print(-1)
    exit()
  else:
    ans[i] = 1
    
  if i == 2:
    print(len(ans))
    exit()

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
B = []
cnt = 0
i = 0
while i != 1:
    i = A[i]-1
    B.append(i)
    cnt += 1
    if cnt > 10**5 + 1:
        print(-1)
        exit()
print(cnt)

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

i = 0
c = 0
while a[i] != 2:
    if a[i] == 0:
        c = -2
        break
    tmp = a[i]
    a[i] = 0
    i = tmp - 1
    c += 1
print(c + 1)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
l=[]
c=1
i=0
f=False
for _ in range(100001):
    i+=1
    c=a[c-1]
    if c==2:
        f=True
        break
if f:print(i)
else:print(-1)

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

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

i = 1
result = 1
while True:
    if t[i] == -1:
        print(-1)
        exit()
    if t[i] == 2:
        print(result)
        exit()
    result += 1
    j = t[i]
    t[i] = -1
    i = j

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
s=set()
tmp=1
cnt=0
while tmp not in s:
    s.add(tmp)
    tmp =a[tmp-1]
    cnt+=1
    if tmp==2:
        break
else:
    cnt=-1
print(cnt)

Solution (Python):
N = int(input())
a = [0] * N
for i in range(N):
    k = int(input())
    li = [i+1,k]
    a[i] = li
a.insert(0,[0,0])
import sys
sys.setrecursionlimit(100000000)
seen = [False] * (N+1)
def dfs(i):
    seen[i] = True
    if seen[a[i][1]] == False:
        dfs(a[i][1])
dfs(1)
def search(a):
    import collections
    from collections import deque
    que = deque()
    count = 0
    que.append(1)
    while len(que) != 0:
        i = que.popleft()
        if i != 2:
            count+=1
            que.append(a[i][1])
        if i == 2:
            return count
if seen[2]:
    k = search(a)
    print(k)
else:
    print(-1)

Solution (Python):
N = int(input())
a = [0] * N
for i in range(N):
    k = int(input())
    li = [i+1,k]
    a[i] = li
a.insert(0,[0,0])
import sys
sys.setrecursionlimit(100000000)
seen = [False] * (N+1)
def dfs(i):
    seen[i] = True
    if seen[a[i][1]] == False:
        dfs(a[i][1])
dfs(1)
def search(a):
    import collections
    from collections import deque
    que = deque()
    count = 0
    que.append(1)
    while len(que) != 0:
        i = que.popleft()
        if i != 2:
            count+=1
            que.append(a[i][1])
        if i == 2:
            return count
if seen[2]:
    k = search(a)
    print(k)
else:
    print(-1)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
l = [int(input()) for _ in range(n)]
s = set()
tmp = 1
cnt = 0
while tmp not in s:
  s.add(tmp)
  tmp = l[tmp-1]
  cnt += 1
  if tmp == 2:
    break
else:
  cnt = -1
print(cnt)

Solution (Python):
import sys

n=int(input())
a=[int(input()) for i in range(n)]
count=0
num=1

while(num!=2):
	num=a[num-1]
	count+=1
	if count>=n:
		count=-1
		break

print(count)

Solution (Python):
n = int(input())
a = [0]
tmp = [int(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)

Solution (Python):
N = int(input())
a = [int(input())-1 for _ in range(N)]
p = 0
for i in range(N):
    if p == 1:
        print(i)
        exit()
    p = a[p]
print(-1)

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

Solution (Python):
N=int(input())
a=[]
visitflag=0
count=0
for i in range(N):
    suc=int(input())
    a.append([suc,visitflag])

i=0


while True:
    if a[i][1]==0:
        a[i][1]=1
        success=(a[i][0])-1
        i=success
        count=count+1
        if i==1:
            print(count)
            break
    else:
        print("-1")
        break

Solution (Python):
n = int(input())
buttons = [0 for _ in range(n)]
for i in range(n):
    buttons[i] = int(input())

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
buttons = list(map(int, sys.stdin))

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

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

al = [0 for _ in range(n)]
for i in range(n):
    al[i] = int(input())

ans = 1
i = 0
while True:
    if al[i] == 2:
        print(ans)
        break
    elif al[i] == -1:
        print(-1)
        break
    tmp = i
    i = al[i]-1
    al[tmp] = -1
    ans += 1

Solution (Python):
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
i = a[0]
counter = 1
while counter <= n+1:
  if i == 2:
    print(counter)
    exit()
  else:
    i = a[i-1]
    counter += 1
print(-1)

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

i = 0
d = 0
c = 0
for _ in range(n):
    c += 1
    d = A[i]
    if d==1:
        break
    else:
        i = d
print(c if c!=n else -1)

Solution (Python):
N = int(input())
a = []
for i in range(N):
    b = int(input())
    a.append(b)
ans = 0
tmp = 1
flag = False
while(1):
    tmp = a[tmp-1]
    ans += 1
    if tmp==2:
        flag = True
        break
    if ans > N:
        break
if flag:
    print(ans)
else:
    print(-1)

Solution (Python):




def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    result = 1
    count = -1
    for i in range(N):
        result = A[result - 1]
        if result == 2 and count < 0:
            count = (i + 1)
    print(count)


if __name__ == "__main__":
    main()

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

temp = 1
count = 0
while temp != 2 and count <= 100001:
	temp = a[temp - 1]
	count += 1

if count > 100000:
	print(-1)
else:
	print(count)

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

Solution (Python):
n = int(input())
a_i = []
for x in range(n):
    a_i.append(int(input()))
a_i2 = []
num = 1
for i in range(n):
    a_i2.append(num)
    num = a_i[num-1]
if a_i2.count(2) == 0:
    print(-1)
else:
    print(a_i2.index(2))

Solution (Python):
n = int(input())
a_i = []
for x in range(n):
    a_i.append(int(input()))
a_i_Order = []
btn = 1
for x in range(n):
    a_i_Order.append(a_i[btn - 1])
    btn = a_i[btn - 1]
if a_i_Order.count(2) == 0: print(-1)
else: print(a_i_Order.index(2) + 1)

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

ans=-1
count=0
tmp=1

for i in range(1,n+1):
    tmp=li[tmp-1]
    if tmp == 2:
        ans=i
        break

print(ans)

Solution (Python):
n = int(input())
lis = []
for i in range(n):
    lis.append(int(input()))
val = set()
i = 1
cnt =0
while True:
    
    cnt += 1
    if lis[i-1] == 2:
        break
    if  lis[i-1] in val:
        cnt = -1
        break
    val.add(lis[i-1])
    i = lis[i-1]

print(cnt)

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

count = 0
next_index = 1

for _ in range(N):
    if next_index == 2:
        break

    next_index = B[next_index-1]
    count += 1

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

Solution (Python):
N=int(input())
BU=[int(input())-1 for i in range(N)]
ans=-1
onid=0
for i in range(1,N+1):
  onid=BU[onid]
  if onid==1:
    ans=i
    break
print(ans)

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

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

Solution (Python):
N =int(input())
a =[]
for _ in range(0,N):
    a.append(int(input()))
b =[0]*N
b[0]=1
f=0
for i in range(1,N):
    b[i]=a[b[i-1]-1]
    if b[i]==2:
        f=i
        break
if f>0:
    print(i)
else:
    print(-1)

Solution (Python):
n=int(input())
l={i+1:int(input()) for i in range(n)}
c=1
i=1
while(i<=n):
  c=l.get(c)
  if c==2:
    print(i)
    exit()
  i+=1
print(-1)