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=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0
kai=0
while i!=1:
  kai+=1
  out+=1
  i=a[i]
  if kai>n:
    out=-1
    break

print(out)

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

b = [True]*N
b[0] = False
i = a[0]
cnt = 1
while b[i]:
    if i == 1:
        print(cnt)
        break
    b[i] = False
    i = a[i]
    cnt += 1
else:
    print(-1)

Solution (Python):
def main():
    N = int(input())
    a = [int(input()) for _ in range(N)]
    i = 1
    ans = 0

    for K in range(N):
        i = a[i-1]
        ans += 1
        if i == 2:
            return ans
    ans = -1
    return ans

print(main())

Solution (Python):
def resolve():
    n = int(input())
    next_button = list()

    for i in range(n):
        a = int(input())
        next_button.append(a)
    
    now_button = 1
    ans = 0
    for i in range(n):
        now_button = next_button[now_button-1]
        
        if now_button == 2:
            ans += 1
            break
        ans += 1

    if ans == n:
        print(-1)
    else:
        print(ans)
resolve()

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

Solution (Python):
N=int(input())
a=[int(input()) for i in range(N)]
ans=0
cur=0
while True:
  ans+=1
  if a[cur]==2:
    print(ans)
    exit()
  else:
    cur=a[cur]-1
    if ans>=N:
      print(-1)
      exit()

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

Solution (Python):


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

	a = A[a-1]

	if cnt > N:
		print(-1)
		break

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

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

counter = 0
i = 0
while buttons[i] != -1:
    if i == 1:
        print(counter)
        break
    else:
        counter += 1
        _i = i
        i = buttons[i]
        buttons[_i] = -1
else:
    print(-1)

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

    y=a[x]
    a[x]=-1
    x=y
    if x==-1:
        print(-1)
        exit()

Solution (Python):
n=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
x=1 
c=0
while True:
    x=l[x-1]
    c+=1
    if x==2:
        print(c)
        break
    elif c>n:
        print(-1)
        break

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

if now == 2:
    print(1)
    exit()

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

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

if now == 2:
    print(1)
    exit()

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

Solution (Python):

def calc(N, button) : 
  count = 0
  position = 1
  next = button[1]
  flag = [False] * (10 ** 5 + 1)
  
  for i in range(1, N + 1) : 
    count += 1
    next = button[position]
    if next == 2 : 
      return count
    position = next
    if flag[position] == True : 
      return -1
    flag[position] = True
    
  return -1


N = int(input())

button = [0] * (10 ** 5 + 1)

for i in range(1, N + 1) : 
  button[i] = int(input())
  
print(calc(N, button))

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

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

a=[""]*n

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

light = a[0]
counter = 1
result = -1
if light == 2:
  print(1)
  exit()
for i in range(n):
  light = a[light-1]
  counter += 1
  if light == 2:
    result = counter
    break
print(result)

Solution (Python):
N = int(input())
buttons = [0] * (N + 1)
for i in range(N):
    buttons[i + 1] = int(input())
current = 1
for i in range(N):
    current = buttons[current]
    if current == 2:
        print(i + 1)
        exit()
print(-1)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
x=1
cnt=0
y=[1]
while True:
    x=a[x-1]
    cnt+=1
    y.append(x)
    if x==2:
        print(cnt)
        break
    elif x==1:
        print('-1')
        break
    elif cnt==100000:
        print(-1)
        break
    else:
        continue

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

pushed = [False] * N
pushed[0] = True
now = 0
cnt = 0

while True:
    next = A[now] - 1

    if pushed[next]:
        cnt = -1
        break

    if next == 1:
        cnt += 1
        break

    if next != 1:
        now = next
        pushed[next] = True
        cnt += 1


print(cnt)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
T = [False for i in range(n)]
tmp = 1
T[0] = True
count = 1
while True:
    tmp = a[tmp-1]
    if T[tmp -1]:
        print(-1)
        exit()
    T[tmp-1] = True
    if tmp == 2:
        print(count)
        break
    count+=1

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

Solution (Python):
N = int(input())
x = [int(input()) for i in range(N)]
cnt = 1
num = x[0]
if num != 2:
    cnt = 2
    for i in range(N):
        num = x[num-1]

        if(num == 2):
            break
        cnt += 1
if cnt == N + 2:
    cnt = -1
print(cnt)

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

count = 1
next = a[0]
flg = 0

while True:
  if next == 2:
    break
  if count == n:
    flg = 1
    break
  
  count += 1
  next = a[next-1]

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

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

button = list()

for x in range(n):
    button.append(int(input())-1)

now = 0
count = 0

while True:
    if now == 1:
        print(count)
        break

    if count >= n:
        print(-1)
        break

    count += 1
    now = button[now]

Solution (Python):
n = int(input())
a = {}

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

b = a[1]
k = 1
if b == 2:
  ans = k
else:
  f = 0
  for i in range(n):
    b = a[b]
    k += 1
    if b == 2:
      ans = k
      f = 1
      break
    else:
      next
  if f == 0:
    ans = -1
print(ans)

Solution (Python):
import sys
n = int(input())
ls = [int(input()) for _ in range(n)]
if 2 not in ls:
    print(-1)
    sys.exit()
i = 1
k = 0
for j in range(n):
    i = ls[i-1]
    k += 1
    if i == 2:
        print(k)
        sys.exit()
print(-1)

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

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

A = [0] + A
count = 0
light = 1
if 2 not in A:
  print("-1")
  exit()
else:
  while light != 2 and count <= n:
    count += 1
    light = A[light] 

if count <= n:
  print(count)
else:
  print("-1")

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
b = [0]
target = 0
count = 0
while(True):
    target = a[target] - 1
    count += 1
    if count > n:
        print("-1")
        break
    if target == 1:
        print(count)
        break

Solution (Python):
def main():
    a = [int(input())-1 for _ in range(int(input()))]
    now = 0
    sumi = set()
    count = 0
    while now != 1:
        if now in sumi:
            print(-1)
            return
        sumi.add(now)
        now = a[now]
        count += 1
    print(count)
        
if __name__ == "__main__":
    main()

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

Solution (Python):

"""
Created on Thu Jun  4 22:35:46 2020

@author: naoki
"""

import sys

N = int(input()) 
a = [int(input()) for i in range(N)] 
a = [0] + a 
count = 0 
light = 1 
if 2 not in a: 
    print(-1)
    sys.exit()
else: 
    while light != 2 and count <= N : 
        count += 1 
        light = a[light]

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

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
N = I()
a = [0]+[I() for _ in range(N)]
ans,now = 0,1
for _ in range(N):
    temp = a[now]
    ans += 1
    if temp==2:
        print(ans)
        break
    now = temp
else:
    print(-1)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
N = I()
a = [0]+[I() for _ in range(N)]
ans,now = 0,1
for _ in range(N):
    temp = a[now]
    ans += 1
    if temp==2:
        print(ans)
        break
    else:
        now = temp
else:
    print(-1)

Solution (Python):
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
ans = 0
light = 0
for i in range(10**6):
    light = a[light]-1
    ans += 1
    if light == 1:
        print(ans)
        exit()
print(-1)

Solution (Python):

N = int(input())
a = []
for _ in range(N):
    temp = int(input())
    a.append(temp)

index = 0
current = 0
count = 0
for i in range(N):
    if current == 2:
        print(count)
        break
    current = a[index]
    index = current - 1
    count += 1
else:
    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)
        exit()

print(-1)

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

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

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

Solution (Python):
import numpy as np

n = int(input())

array = np.zeros(n, dtype = np.int)

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

next_number = array[0]
counter = 1

for i in range(n):
    if next_number == 2:
        print(counter)
        break
    else:
        next_number = array[next_number-1]
        counter += 1
        if i == n-1:
            print(-1)

Solution (Python):

import math


def inp():
    return(int(input()))



def inlist():
    return(list(map(int, input().split())))



def instr():
    s = input()
    return(list(s[:len(s)]))



def invr():
    return(map(int, input().split()))


n = inp()
a = [0]

for i in range(n):
    a.append(inp())

i = 1
c = 1

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

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
B=set()
B.add(1)
b=1
count=0
while True:
    b=A[b-1]
    count+=1
    if b==2:
        print(count)
        break
    elif b in B:
        print(-1)
        break
    else:
        B.add(b)

Solution (Python):
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*int(N+1)

a=1
count=0

if lamps.count(2)==0:
    print("-1")
    exit()

while PressedButtons[a]==False :
    PressedButtons[a]=True
    count+=1
    a=lamps[a]
    if a==2:
        print(count)
        exit()
print('-1')

Solution (Python):
from collections import deque

N=int(input())
graph=[[] for _ in range(N)]
for i in range(N):
    a=int(input())-1
    graph[i].append(a)

d=[float("inf")]*N
d[0]=0
queue=deque([0])
while queue:
    x=queue.pop()
    for y in graph[x]:
        if d[y]!=float("inf"):
            break
        d[y]=d[x]+1
        queue.append(y)

if d[1]==float("inf"):
    print(-1)
else:
    print(d[1])

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

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

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

count = 1
now = 0

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


if a[now] == 2:
  print(count)
else:
  print(-1)

Solution (Python):
import sys
n=int(input())
List=[int(input()) for i in range(n)]
ans=0
nxt=1
while(1):
    if ans>=n:
        print(-1)
        sys.exit()
    elif nxt==2:
        break
    nxt=List[nxt-1]
    ans+=1
print(ans)

Solution (Python):
N = int(input())
a = []
exp = {1}
for i in range(N):
  a.append(int(input()))
r = a[0]
flag = 0
ans = 0
while True:
  if(r in exp):
    flag = -1
    break
  if(r == 2):
    flag = 1
    break
  exp.add(r)
  r = a[r- 1]
  ans+= 1
  
if(flag == -1):
  print("-1")
else:
  print(ans+ 1)

Solution (Python):
class UnionFindTree:
    def __init__(self, n):
        self.__parents = [-1] * n

    def find(self, x):
        p = self.__parents[x]
        if p < 0:
            return x
        p = self.find(p)
        self.__parents[x] = p
        return p

    def union(self, x, y):
        p = self.__parents
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if p[x] > p[y]:
            x, y = y, x
        p[x] += p[y]
        p[y] = x
        return True

    def same(self, x, y):
        return self.find(x) == self.find(y)


N = int(input())
uft = UnionFindTree(N)
cur = 0
buttons = list(map(lambda x: int(x) - 1, (input() for _ in range(N))))
count = 0
while True:
    n = buttons[cur]
    count += 1
    if not uft.union(cur, n):
        count = -1
        break
    if n == 1:
        break
    cur = n
print(count)

Solution (Python):
N = int(input())
lamps = [0] * N
next_ = 0
count = 0
buttons = list(map(lambda x: int(x) - 1, (input() for _ in range(N))))
while True:
    count += 1
    lamps[next_] = 1
    next_ = buttons[next_]
    if next_ == 1:
        break
    if lamps[next_]:
        count = -1
        break

print(count)

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    a = [0] * N
    for i in range(N):
        a[i] = int(input()) - 1

    visited = [False] * N
    visited[0] = True
    i = 0
    ans = 0
    while True:
        i = a[i]
        if visited[i]:
            ans = -1
            break
        visited[i] = True
        ans += 1
        if i == 1:
            break

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):






N = int(input())




l=[]
for i in range(N):
    B = int(input())
    l.append(B)

now=0
next=1
c=0
while c<10**6:
    now=next
    if now==2:
        print(c)
        exit()
    next=l[now-1]
    c+=1

print(-1)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    dict={}
    for i in range(n):
        dict[i] = int(input())

    dict2={}
    count = 0
    now =1
    for i in range(n):
        if now in dict2:
            print(-1)
            break
        else:
            if dict[now-1] ==2:
                count+=1
                print(count)
                break
            else:
                dict2[now]=0
                count+=1
                now = dict[now-1]

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

    ans = 0
    nxt = 1
    while True:
        if ans > N:
            print(-1)
            exit()
        elif nxt == 2:
            break
        nxt = a[nxt-1]
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def li():
    return list(map(int, input().split()))
def mi():
    return map(int, input().split())
def ii():
    return int(input())
def lim(N):
    return [int(input()) for i in range(N)]

N = ii()
a = lim(N)

current = 1
result = 1

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

Solution (Python):
import sys

N = int(sys.stdin.readline())

button = sys.stdin.readlines()
button = [int(j.replace('\n','')) for j in button]


b = button[0]
counter = 0
for i in range(len(button)):
    if b == 2:
        print(counter+1)
        exit()
    else:
        b = button[b-1]
        counter = counter+1

print(-1)

Solution (Python):
_, *A = map(lambda x: int(x) - 1, open(0).read().split())
A = list(A)
pos = 0
cnt = 1
for i in range(10 ** 5 + 1):
    pos = A[pos]
    if pos == 1:
        print(cnt)
        break
    cnt += 1
else:
    print(-1)

Solution (Python):
import sys

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

current = 1
count = 0
while True:
    current = a[current]
    count += 1
    if current == 2:
        print(count)
        sys.exit(0)

    if count > 20 ** 5:
        break
print(-1)

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

a=[int(input())-1 for i in range(n)]
c=[-1 for i in range(n)]
cu=0
c[cu]=0
for i in range(n):
  cu=a[cu]
  if c[cu]>0:
    break
  c[cu]=i+1
  
print(c[1])

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

Solution (Python):
N = int(input())
a = 1
A = list(int(input()) for i in range(N))

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

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

cnt = 1
x = A[0]
while x != 2 and cnt != n:
    x = A[x - 1]
    cnt += 1
    
if cnt == n:
    print(-1)
else:
    print(cnt)

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
B = []
for i, a in enumerate(A):
    B.append([i + 1, a])
    
idx = 1
for i in range(n):
    x = B[idx - 1][1]
    if x == 2:
        print(i + 1)
        break
    else:
        idx = x
else:
    print(-1)

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

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

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

print(-1)

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

i = 0
cnt = 1
while True:
    if cnt > len(a):
        cnt = -1
        break
    if a[i] == 1:
        cnt = -1
        break
    elif a[i] == 2:
        break
    else:
        i = a[i] - 1
        cnt += 1
print(cnt)

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

next_li = []

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


st = 0
c = 1
for i in range(N):
    if next_li[st] == 2:
        print(c)
        exit()
    else:
        c += 1
        st = next_li[st]-1
print(-1)

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

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

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

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

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


visited = set()
cnt = 0 
i = 1 
v = a[i] 
f = True
while True:
    if i in visited:
        f = False
        break
    if v == 2:
        cnt += 1
        break
    visited.add(i)
    i = v
    v = a[v]
    cnt += 1

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

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

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

d = [999999 for i in range(N+1)]
d[1]=0
seen = [0 for i in range(N+1)]

q = [1]
seen[1]=1

while q:
  v = q[0]
  del q[0]

  if seen[graph[v]]==0:
    q.append(graph[v])
    seen[graph[v]]=1
  if d[v]+1<d[graph[v]]:
    d[graph[v]]=d[v]+1
print(-1 if d[2]==999999 else d[2] )

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

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

print(-1)

Solution (Python):
N = int(input())
A = list(int(input()) for i in range(N))
now=0
ok=False
while(True):
    if A[now]==-1:
        break
    else:
        temp=now
        now=A[now]-1
        A[temp]=-1
    if now==1:
        ok=True
        break
print(A.count(-1) if ok else -1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
pos = 1
cnt = 0
passed = [0]*(n+1)
TF = True
while TF:
  pos -= 1
  temp = a[pos]
  if passed[temp] == 1:
    TF = False
    print(-1)
  elif temp == 2:
    print(cnt+1)
    TF = False
  else:
    passed[temp] = 1
    pos = temp
    cnt += 1

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

i = 0
l = 1
while 1:
  i+=1
  l=a[l-1]
  if l==2:
    ans=i
    break
  if i>N:
    ans=-1
    break
print(ans)

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

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

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

count = 0
idx = 0
for _ in range(N):
    if A[idx] == 2:
        print(count + 1)
        exit()
    idx = A[idx] - 1
    count += 1
print(-1)

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

i = 1
count = 0
find = False
for _ in range(N):
    count += 1
    i = A[i-1]
    if i == 2:
        find = True
        break

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

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

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

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

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

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

button = 0
cnt = 0
done = [False] * n

while True:
    if done[button] == False:
        done[button] = True
        cnt += 1
        if a[button] == 1:
            break
        button = a[button]
    else:
        cnt = -1
        break

print(cnt)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
i = 0
now = 0
while(i < n):
    if now == 1:
        print(i)
        exit()
    now = a[now]-1
    i += 1
else:
    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())
buttons = [0]
target_index = 0
for _ in range(n):
    buttons.append(int(input()))

count = 0
prev_index = 1
while count < n:
    if prev_index < 1 or len(buttons) <= prev_index:
        count = -1
        break

    next_index = buttons[prev_index]
    count += 1
    if next_index == 2:
        break
    else:
        prev_index = next_index

print(count if count < n else -1)

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

route = set()
route.add(0)
before = 0
ans = 0
for i in range(N):
    now = a[before] - 1
    ans += 1
    if now == 1:
        break
    elif now not in route:
        route.add(now)
        before = now
    else:
        ans = -1
        break
print(ans)

Solution (Python):
N=int(input())
A=[]
for _ in range(N):
  A.append(int(input())-1)
visited=[0]*N
i,ans= 0,0
while i != 1:
  if visited[i] == 0:
    visited[i] = 1
    i = A[i] 
    ans += 1
  else:
    ans = -1
    break
print(ans)

Solution (Python):
n=int(input())
x = [int(input()) for i in range(n)]
ans=1
visited=[False]*n
now=0
visited[now]=True

while(1):
  
  now=x[now]-1
  if visited[now]:
    print(-1)
    break
   
  visited[now]=True
  if now==1:
    print(ans)

    break
  else:
    ans+=1

Solution (Python):
def main():
    n = int(input())
    A = [0] + [int(input()) for _ in [0] * n]
    visited = [0] * (n+1)
    now = 1
    visited[now] = 1
    cnt = 0
    while True:
        next = A[now]
        cnt += 1
        if visited[next]:
            print(-1)
            break

        if next == 2:
            print(cnt)
            break

        visited[next] = 1
        now = next


if __name__ == "__main__":
    main()

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

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

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

li = [False]*n

li[0]=1
now = 1
for i in range(n):
    now = ai[now-1] 
    if now ==2:
        print(i+1)
        exit()
    elif li[now-1] !=False:
        print(-1)
        exit()
else:
    print(-1)

Solution (Python):
def main():
    N, *A = map(int, open(0).read().split())
    count = 0
    cur = 1
    while count < N:
        count += 1
        cur = A[cur - 1]
        if cur == 2:
            print(count)
            return
    print(-1)

if __name__ == '__main__':
    main()

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

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

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

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

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

u = [False for _ in range(n)]
i = 0
ans = 0
while(i != 1):
  u[i] = True
  i = a[i]
  if u[i]:
    print(-1)
    exit()
  ans += 1
print(ans)

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

ans,i = 0,0
check = [0]*N

while i != 1:
  check[i] += 1
  if check[i] > 1:
    ans = -1
    break
  ans += 1
  i = a[i]

print(ans)

Solution (Python):


import sys






























def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = ii()
    L = [ii()-1 for _ in range(n)]
    cnt = 0
    current = 0
    while cnt < n:
        if current == 1:
            break
        current = L[current]
        cnt += 1
    
    if cnt == n:
        print(-1)
    else:
        print(cnt)



if __name__ == "__main__":
    main()

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

visited = [0] * n

index = 0
num_push = 0
goal = False
while True:
    if visited[index] == 1:
        break

    visited[index] = 1
    if index == 1:
        goal = True
        break

    index = as_[index]
    num_push += 1

if goal:
    print(num_push)
else:
    print(-1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
if 2 not in A:
    print(-1)
    exit()
now = 1
count = 0
past = []
while True:
    if now == 2:
        print(count)
        exit()
    if count >= N:
        print(-1)
        exit()
    count += 1
    now = A[now - 1]

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

idx = 0
count = 0
while True:
    if not is_visited[idx]:
        is_visited[idx] = True
    else:
        print(-1)
        break
    idx = A[idx] - 1
    count += 1
    if idx == 1:
        print(count)
        break

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

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    btn = [int(input())-1 for _ in range(N)]
    i = 0
    li = [False]*N
    
    
    for _ in range(N):
        li[i] = True
        k = btn[i]
        
        
        if li[k]:
            print('-1')
            break
        elif k == 1:
            print(sum(li))
            break
        
        i = k
    
if __name__ == '__main__':
    main()

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

while True:
    light = a[light - 1]
    count += 1

    if light == 2:
        print(count)
        break
    if count == N - 1:
        print(-1)
        break

Solution (Python):
n = int(input())
a = [int(input())-1 for _ in range(n)]
visit=[False]*n
cnt=0
nxt=0
for _ in range(n):
    visit[nxt]=True    

    
    if nxt==1:
        break

    nxt=a[nxt]

    
    if visit[nxt]:
        cnt=-1
        break

    cnt+=1
    
print(cnt)

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

cnt = 0
idx = 0

while True :
    x = lst[idx]
    cnt += 1
    if x == 2:
        print(cnt)
        exit()
    idx = x - 1
    if cnt > N:
        print(-1)
        exit()

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