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 = [0 for _ in range(N)]
for i in range(N):
    a[i] = int(input())
c = 0
x = 1
while(x != 2):
    nex = a[x - 1]
    if not nex:
        c = -1
        break
    else:
        a[x - 1] = False
        c += 1
        x = nex
print(c)

Solution (Python):
N = int(input())
a = [0 for _ in range(N)]
for i in range(N):
  a[i] = int(input())
 
c = 0
x = 1
while(x != 2):
  nex = a[x-1]
  if not nex:
    c = -1
    break
  else:
    c += 1
    a[x-1] = 0
  x = nex
 
print(c)

Solution (Python):
N = int(input())
a = [0 for _ in range(N)]
for i in range(N):
  a[i] = int(input())
 
c = 0
x = 1
while(x != 2):
  nex = a[x-1]
  if not nex:
    c = -1
    break
  else:
    c += 1
    a[x-1] = False
  x = nex
 
print(c)

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)]
b=[]
next=0
count=0
for i in range(n):
    next=a[next]-1
    count+=1
    if next==1:
        print(count)
        exit()
print(-1)

Solution (Python):
n = int(input())
A = [int(input())-1 for i in range(n)]
B = [False] * n
ans = 0
i = 0
while True:
    if B[i] is True:
        print(-1)
        break
    else:
        B[i] = True
    
    ans += 1
    if A[i] != 1:
        i = A[i]
    else:
        print(ans)
        break

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

i = 1

cnt = 0
while cnt <= len(A):
    if i == 2:
        print(cnt)
        break
    pop = A[i - 1]

    i = pop
    cnt += 1
else:
    print(-1)

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

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

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

x = 0

result = -1

for i in range(N):
    x = A[x] - 1
    if x == 1:
        result = i + 1
        break

print(result)

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

Solution (Python):
N = int(input())
buttons = [int(input()) for _ in range(N)]
count = 1
tmp = buttons[0]
flag = True
while count < N+2:
    if tmp == 2:
        print(count)
        flag = False
        break
    else:
        tmp = buttons[tmp - 1]
        count += 1
if flag:
    print(-1)

Solution (Python):
def abc065_b():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    now = 1
    count = 0
    for i in range(n):
        if now == 2:
            print(count)
            return None
        now = a[now - 1]
        count += 1
    print(-1)


abc065_b()

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

le = [int(input()) for i in range(N)]
status = [0 for i in range(N)]
answer=0
index=0



while(1):
    
    if index==1:
        break
    
        
    answer+=1
    status[index]+=1
    if status[index]>=2:
        answer=-1
        break
    
    index = le[index]-1


print(answer)

Solution (Python):
import collections

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

d = collections.deque()
used = [False] * N
used[0] = 1 
d.append([0, 0])

while len(d) != 0:
    temp, count = d.popleft()
    next_a = a[temp] - 1

    if temp == 1:
        break

    if not used[next_a]:
        d.append([next_a, count + 1])
        used[next_a] = True
else:
    count = -1

print(count)

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

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

Solution (Python):



def main():
    """Function."""
    n = int(input())
    check = {}

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

    check2 = set()
    temp = 1
    check2.add(temp)
    for i in range(n):
        temp = check[temp]
        if temp in check2:
            print(-1)
            break

        check2.add(temp)
        if temp == 2:
            print(i+1)
            break


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    aa = [int(input()) - 1 for _ in range(n)]
    visiteds = [False] * n
    i = 0
    res = 0
    while True:
        if visiteds[i]:
            print(-1)
            break
        if i == 1:
            print(res)
            break
        visiteds[i] = True
        res += 1
        i = aa[i]


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [0 for _ in range(n)]
for i in range(n):
    a[i] = int(input()) - 1
visited = [False for _ in range(n)]
visited[0] = True
next = a[0]
count = 1
while True:
    if next == 1:
        print(count)
        exit()
    elif not visited[a[next]]:
        visited[a[next]] = True
        count += 1
        next = a[next]
    else:
        print(-1)
        exit()

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

a_dict = {}

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

answer = 0
now_point = 1

for i in range(N):
	now_point = a_dict[now_point]
	answer += 1
	if now_point == 2:
		print(answer)
		break

if now_point != 2:
	print(-1)

Solution (Python):
n = int(input())
edges = {i: int(input())-1 for i in range(n)}


def push_buttom(v, goal):
    for i in range(n):
        v = edges[v]
        if v == goal:
            return i+1
    return -1


print(push_buttom(0, 1))

Solution (Python):
N = int(input())
B = [int(input()) for i in range(N)]
A = [0]
A.extend(B)
past = [1]
next_l = 1



for i in range(N):
  next_l = A[next_l]
  
  
  
  if next_l == 2:
    print(len(past))
    exit(0)
  past.append(next_l)
print(-1)

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

push_index = 1
push_count = -1
count = 0
for i in range(N) :
    push_index = buttons[push_index-1]
    count += 1
    if (push_index == 2) :
        push_count = count
        break

print(push_count)

Solution (Python):

import sys

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

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

Solution (Python):
N = input()
a = [input() for i in range(N)]
idx = 0
c = 0
for i in range(N):
    if idx == 1:
        break
    idx = a[idx] - 1
    c += 1
print c if c < N else -1

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

Solution (Python):
N=int(input())
a=[]
button=1
i=0
cnt=0
for j in range(N):
    a.append(int(input()))
while i < N and button != 2:
    button=a[button-1]
    i+=1
    cnt+=1
if i == N:
    print("-1")
else:
    print(str(cnt))

Solution (Python):
import sys
N = int(input())
btn = [int(input()) for _ in range(N)]
ans = []
x = 0

for i in range(N):
  ans.append(btn[x])
  if ans[i] == 2:
    print(len(ans))
    sys.exit()
  else:
    x = ans[i] - 1
print(-1)

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

temp = 0
for j in range(N):
  if nums[temp]==2:
    print(j+1)
    exit()
  else:
    temp = nums[temp]-1

print(-1)

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

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

count=0
x=1

for i in range(N):
    x=a[x-1]
    count+=1
    if x==2:
        print(count)
        quit()
print("-1")

Solution (Python):

import sys

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

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

Solution (Python):

import sys

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

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

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

	k=a[k]-1
	if(k==1):
		ans=i+1
		break
print(ans)

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

Solution (Python):
N = int(input())
A = [0]*(N+1)
for i in range(1, N+1):
    A[i] = int(input())
B = [0, 1]+[0]*(N-1)
ans = 0
i = 1
while B[i]:
    ans += 1
    if i == A[i] or B[A[i]] == 1:
        break
    elif A[i] == 2:
        print(ans)
        exit()
    else:
        i = A[i]
        B[i] = 1
print(-1)

Solution (Python):

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

a = [int(m) for m in d]

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

Solution (Python):


import sys


def model():
    e = list(map(int, sys.stdin))
    cur = e[1]
    for i in range(e[0]):
        if cur == 2:
            print(i + 1)
            break
        cur = e[cur]
    else:
        print(-1)


def input_lines():
    """hell?"""
    lines = sys.stdin.readlines()
    for i in range(1, int(lines[0])+1):
        yield i, int(lines[i].strip())


def solve():
    d = {i: button for i, button in input_lines()}
    count = 1
    index = 1

    while True:
        try:
            prev = index
            index = d[index]

            if index is None:
                print(-1)
                exit()
            elif index == 2:
                print(count)
                exit()
            d[prev] = None
            count += 1
        except KeyError:
            print(-1)
            exit()


if __name__ == '__main__':
    solve()

Solution (Python):


import sys


def model():
    e = list(map(int, sys.stdin))
    cur = e[1]
    for i in range(e[0]):
        if cur == 2:
            print(i + 1)
            break
        cur = e[cur]
    else:
        print(-1)


def input_lines():
    """hell?"""
    lines = sys.stdin.readlines()
    for i in range(1, int(lines[0])+1):
        yield i, int(lines[i].strip())


def solve():
    d = {i: button for i, button in input_lines()}
    count = 1
    index = 1

    while True:
        try:
            prev = index
            index = d[index]

            if index == 2:
                print(count)
                exit()
            elif index is None:
                print(-1)
                exit()
            d[prev] = None
            count += 1
        except KeyError:
            print(-1)
            exit()


if __name__ == '__main__':
    solve()

Solution (Python):


import sys


def input_lines():
    """hell?"""
    lines = sys.stdin.readlines()
    for i in range(1, int(lines[0])+1):
        yield i, int(lines[i].strip())


def solve():
    d = {i: button for i, button in input_lines()}
    count = 1
    index = 1

    while True:
        try:
            prev = index
            index = d[index]

            if index == 2:
                print(count)
                exit()
            else:
                del d[prev]
                count += 1
        except KeyError:
            print(-1)
            exit()


if __name__ == '__main__':
    solve()

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


ind = num_list[0]
if ind == 1:
    print(1)
else:
    for i in range(n-1):
        ind = num_list[ind]
        count += 1
        if ind == 1:
            print(count)
            break
if count > n-1:
   print(-1)

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

button = 1
for pushCnt in range(n):
    button = buttons[button]
    pushCnt += 1
    if button == 2:
        print(pushCnt)
        break
else:
    print(-1)

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

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

pushed = []

button = 0
count = 0
flag = False

while count < n + 1:
	if button == (2 - 1):
		flag = True
		break
	
	pushed.append(button)
	button = a[button] -1
	count += 1
	
if flag:
	print(count)
else:
	print(-1)

Solution (Python):
n = int(input())
num_list = [int(input()) for _ in range(n)]
ans_cnt = 1
end_cnt = 0
btn_idx = 0

while end_cnt < len(num_list):
    if num_list[btn_idx] == 2:
        print(ans_cnt)
        exit()
    else:
        btn_idx = num_list[btn_idx]-1
        ans_cnt += 1
    end_cnt += 1
print(-1)

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

a = [input() for i in range(n)]
b = 0
cnt = 0

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

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
i = 1
S = set()
while(i != 2):
    if i not in S:
        S.add(i)
    else:
        print(-1)
        exit()
    i = A[i-1]
print(S.__len__())

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

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

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

f = [0 for i in range(n)]

b = a[0]
f[0] = 1
while(True):
    if(b == 2):
        print(f.count(1))
        sys.exit()
    ni = b-1
    b = a[ni]
    if(f[b-1] == 1):
        print(-1)
        sys.exit()
    else:
        f[b-1] = 1

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

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

print(-1)

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

nexti = 0
for i in range(N):
    nexti = A[nexti] - 1
    if nexti == 1:
        break

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

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

i = 0
k = 0
s = set()
flag = True
while i != 1:
    if i in s:
        flag = False
        break
    s.add(i)
    i = A[i]
    k += 1

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

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
a.insert(0,-1)
ans = 0

tmp = 1
for i in range(1,n+1):
    current = a[tmp]
    ans += 1
    if current == 2:
        print(ans)
        exit()
    else:
        tmp = current

print(a[0])

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

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

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

t = [0]

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


index = 1
found = False
for i in range(N):
    if t[index] == 2:
        found = True
        break

    index = t[index]


print(i + 1 if found else -1)

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

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

Solution (Python):
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
route = [a[0]]
count = 1
flag = 0
for i in range(1, N+2):
    count += 1
    route.append(a[route[i-1]-1])
    if route[i] == 2:
        flag = 1
        break

if route[0] == 2:
    print(1)
elif flag == 0:
    print(-1)
else:
    print(len(route))

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

button = [1]
for _ in range(N-1):
    button.append(a[button[-1] - 1])
    if button[-1] == 2:
        print(len(button) - 1)
        exit()
    if button[-1] == button[-2]:
        break
    
print(-1)

Solution (Python):
N = int(input())
array = []
for i in range(N):
  a = int(input())
  array.append(a)
n = 1
c = 0
while n != 2:
  n = array[n-1]
  c += 1
  if c > (10 ** 5):
    break
if c > (10 ** 5):
  print("-1")
else:
  print(c)

Solution (Python):
n=int(input())
a=[]
for i in range(n):
	a.append(int(input()))
b=['']*n
b[0]='hoge'
c=0
x=0
while True:
	c+=1
	x=a[x]-1
	if b[x]=='hoge':
		c=-1
		break
	else:
		b[x]='hoge'
		if x==1:
			break
print(c)

Solution (Python):
n = int(input())
a = [int(input()) - 1 for _ in range(n)]
i = 0
s = set()
c = 0

while True:
  if i == 1:
    print(c)
    break
  elif i in s:
    print(-1)
    break
  else:
    s.add(i)
    i = a[i]
    c += 1

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
turn_off_button = 0
count = 0
while True:
  count += 1
  light = A[turn_off_button]
  if light == 2:
    print(count)
    break
  elif count >= N:
    print(-1)
    break
  turn_off_button = light - 1

Solution (Python):
n = int(input())
button = []
id = 1
times = 0

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

for i in range(n):
        times += 1
        id = button[id-1]
        if id == 2:
                print(times)
                break
else:
        print(-1)

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

tgt = 1
cnt = 0
flg = False
for j in range(n):
    tgt = a[tgt-1]
    cnt += 1
    if tgt == 2:
        flg = True
        break
print(cnt if flg == True else "-1")

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

tmp=1
count=0
while(1):
  tmp=_list[tmp]
  count+=1
  if(tmp==2):
    print(count)
    break
  if(tmp==1 or n<count):
    print(-1)
    break

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

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

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

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

ans = 1
while a[0] !=  2:
	n = a[0]
	a[0] = a[n-1]
	ans += 1
	if  ans > N:
		ans = -1
		break

print(ans)

Solution (Python):


n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.insert(0,-1)
res=0
index=1
for i in range(0,n):
  if a[index]==-1:
    res=-1
    break
  ti=a[index]
  a[index]=-1
  index=ti
  
  res=res+1

  if index==2:
    break
print(res)

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

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

s = 0
for n in range(N):
    s = d[s]
    if s == 1:
        print(n + 1)
        break
else:
    print(-1)

Solution (Python):
from sys import stdin


def main() -> int:
    n = next_int()
    a = [next_int() for _ in range(n)]

    j = 1

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

    print(-1)
    return 0


def next_int() -> int:
    return int(next_str())


def next_str() -> str:
    result = ""
    while True:
        tmp = stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin


def main() -> int:
    n = next_int()
    a = [next_int() for _ in range(n)]

    j = 1
    dic = dict()

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

    print(-1)
    return 0


def next_int() -> int:
    return int(next_str())


def next_str() -> str:
    result = ""
    while True:
        tmp = stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

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

count = 0
n = 1
while step[n-1]:
    count += 1
    if a[n-1] == 2:
        print(count)
        exit()
    step[n-1] = False
    n = a[n-1]
print(-1)

Solution (Python):
n = int(input())
a = [int(input())-1 for x in range(n)]
ans = 0
ok = False
i = 0
for x in range(n):
    if i == 1:
        ok = True
        break
    i = a[i]
    ans += 1

print([-1, ans][ok])

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

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

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

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

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

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

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

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

lu = a[0] - 1
count = 0

for i in range(N):
    count += 1
    if lu == 1:
        print(count)
        exit()
    lu = a[lu] - 1

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

Solution (Python):
import sys

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

already_nums = set()
push_count = 0
i = 0
while i != 1:
    if i not in already_nums:
        already_nums.add(i)
        i = a[i]-1
        push_count += 1
    else:
        print(-1)
        sys.exit()
print(push_count)

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

Solution (Python):
n = int(input())
a_list = [0]
for _ in range(n):
    a_list.append(int(input()))
counter = 0
current_number = 1
already_pushed_list = set()

while(True):
    counter += 1
    already_pushed_list.add(current_number)
    current_number = a_list[current_number]
    if current_number in already_pushed_list:
        print(-1)
        break
    if current_number == 2:
        print(counter)
        break

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

Solution (Python):
n = int(input())
a = list(int(input())-1 for i in range(n))
s = 0
c = 0

while s != 1 and c < n:
    c += 1
    s = a[s]
if c < n:
    print(c)
else:
    print(-1)

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

a = []

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

now = a[0]
count = 1

while now != 2 and count < n:
    count += 1
    now = a[now - 1]

if now == 2:
    print(count)
    exit(0)

else:
    print("-1")
    exit(0)

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

light=1
ans=0
while True:
    if light==2:
        break
    elif light==buttun[light]:
        ans=-1
        break
    else:
        tmp=light
        light=buttun[light]
        ans+=1
        buttun[tmp]=1
    if light==1:
        ans=-1
        break
print(ans)

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
visited = [False for _ in range(n)]
button = 0
ans = 0
while True:
    if visited[button]:
        print(-1)
        exit()
    if button == 1:
        break
    visited[button] = True
    button = A[button] - 1
    ans += 1
print(ans)

Solution (Python):
n = int(input())
list_a = []
for i in range(0,n):
    list_a.append(int(input()))
number = 0
count = 0
while True:
    number = list_a[number] - 1        
    count += 1
    if number == 1:
        print(count)
        break
    if count > n:
        print(-1)
        break

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

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
d = {}
for i in range(n):
  d[i] = a[i]
button = 0
for i in range(n):
  button = d[button] - 1
  if button == 1:
    print(i+1)
    exit()
print(-1)

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

res = []
cnt = 0
i = 0
while True:
    cnt += 1
    i = a[i]-1
    if cnt > 10**5:
        print(-1)
        break
    if i == 1:
        print(cnt)
        break

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

i = 1
c = 0
r = [1]
while True:
    c += 1
    i = a[i]
    if i == 2:
        print(c)
        break
    if c > (10**5):
        print(-1)
        break

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

i = 1
c = 0
r = [1]
while True:
    c += 1
    i = a[i]
    if i == 2:
        print(c)
        break
    if c > (10**5):
        print(-1)
        break

Solution (Python):
n = int(input())
a = []
b = [0]*n
look = 1
end = 0
count = 0
for i in range(n):
    a.append(int(input()))
while end == 0:
    count += 1
    b[look-1] = 1
    look = a[look - 1]
    if b[look - 1] == 1:
        end = -1
    else:
        if look == 2:
            end = count
print(end)

Solution (Python):
n = int(input())
a = [0]
for _ in range(n):
  a.append(int(input()))
visited = [False]*(n+1)
visited[1] = True
cnt = 0
but = 1
while True:
  nextb = a[but]
  if visited[nextb]:
    cnt = -1
    break
  elif nextb == 2:
    cnt += 1
    break
  else:
    but = nextb
    cnt += 1
    visited[but] = True
print(cnt)

Solution (Python):
N=int(input())
A=[int(input())for i in range(N)]
now=A[0]
c=1
while True:
    if now ==2:
        print(c)
        break
    if c>=N:
        print(-1)
        break
    c+=1
    now=A[now-1]

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

now = src[0] - 1
for i in range(N):
    if now == 1:
        print(i+1)
        exit()
    now = src[now] - 1
print(-1)

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

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def dfs(graph:list, node:int, start:int) -> list:
    
    INF = float("inf")
    dist = [INF]*node
    
    
    dist[start] = 0
    stack = [(0,start)]
    
    while stack:
        cost, cur_node = stack.pop()

        
        for nex_cost, nex_node in graph[cur_node]:
            if dist[nex_node] != INF:
                continue
            else:
                dist[nex_node] = dist[cur_node] + nex_cost
                stack.append((dist[nex_node],nex_node))
                
    return dist

n = ni()
a = [ni() for _ in range(n)]

adj = [[] for _ in range(n)]

for i in range(n):
    adj[i].append((1,a[i]-1))

dist = dfs(adj, n, 0)

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

Solution (Python):
N = int(input())
A = [0]
for i in range(N):
    A.append(int(input()))
old = []
bo = 1
ind = 1
while ind <= N:
    if A[bo] == 2:
        print(ind)
        exit()
    else:
        ind += 1
        bo = A[bo]
print(-1)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N = int(input())
    A = []
    for i in range(N):
        a = int(input())
        A.append(a - 1)

    idx = 0
    used = [0] * N
    ans = 0
    while True:
        if idx == 1:
            break
        if used[idx]:
            print(-1)
            return
        used[idx] = 1
        idx = A[idx]
        ans += 1

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

now = 0
count = 0
visited = set()

while True:
  now = A[now]
  count += 1
  if now == 1:
    print(count)
    exit(0)
    
  if now in visited:
    print(-1)
    exit(0)
  else:
    visited.add(now)

Solution (Python):
N=int(input())
a=[int(input())-1 for i in range(N)]
b=[0 for i in range(N)]
k=0
b[0]=1
for i in range(N):
    k=a[k]
    b[k]+=1
    if b[k]>=2:
        print(-1)
        exit()
    if k==1:
        print(i+1)
        exit()