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())
d = {}
for i in range(1, n+1):
    a = int(input())
    d[i] = a

i = 1
visited = set()
steps = 0
while i!= 2 and i not in visited:
    visited.add(i)
    i = d[i]
    steps += 1

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

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

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

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

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

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

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

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

lis = [int(input()) for i in range(N)]

now = 1
Ans = -1
count = 0

for i in range(len(lis)):
	now = lis[now-1]
	count += 1
	if now == 2:
		Ans = count
		break

print(Ans)

Solution (Python):
N = int(input())
button_list = []
for _ in range(N):
    button_list.append(int(input()))
ans = -1
p = 1
s = 0
for i in range(N):
    p = button_list[p-1]
    s += 1
    if p == 2:
        ans = s
        break
print(ans)

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

    visited_flag = [False for _ in range(N)]
    cnt = 1
    i = 0
    while 1:
        if A[i] == 2:
            print(cnt)
            break
        else:
            i = A[i]  - 1
            if visited_flag[i] == True:
                print(-1)
                break
            visited_flag[i] = True
            cnt += 1


solve()

Solution (Python):
n = int(input())
btn = []
checked = set()

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

def find(n):
  global btn, checked
  j = 1
  for i in range(n):
    if btn[j - 1] == 2:
      
      return len(checked) + 1
    elif j - 1 in checked:
      return -1
    else:
      checked.add(j - 1)
      j = btn[j - 1]
  return -1

print(find(n))

Solution (Python):
a, v, c = [input() for _ in xrange(input())], {1}, 1
while True:
    c = a[c - 1]
    if c in v:
        print -1
        exit()
    if c == 2:
        print len(v)
        exit()
    v.add(c)

Solution (Python):
def solve():
    N = int(input())
    a = [0 for i in range(N)]
    for i in range(N):
        a[i] = int(input())
    on_light = 1
    cnt = 0
    for i in range(N):
        on_light = a[on_light-1]
        cnt += 1
        if on_light == 2:
            print(cnt)
            return
    print(-1)
    return
    
    
solve()

Solution (Python):
N = int(input())
a = [0]*(N+1)

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

dp = [False]*(N+1)

ans = 0
ind = 1
while True:
    if ind == 2:
        print(ans)
        break
    else:
        if dp[ind]:
            print(-1)
            break
        else:
            dp[ind] = True
            ans += 1
            ind = a[ind]

Solution (Python):


num = int(input())

array = []
for i in range(num):
    array.append(int(input()))

res = -1
if 2 in array:
    judge = array[0]
    roop = 1
    while judge != 2 and roop != 100001:
        roop += 1
        judge = array[judge-1]
        if judge == 2:
            break
else:
    judge = 10000000

if judge == 2:
    res = roop

print(res)

Solution (Python):
N = int(input())
a = [0] + [int(input()) for _ in range(N)]
ans = 0
idx = 1
flag = False
for i in range(N):
    ans += 1
    if a[idx] == 2:
        print(ans)
        flag = True
        break
    idx = a[idx]
else:
    print(ans if flag else -1)

Solution (Python):
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
flag = -1
b = 0
for i in range(n):
    if A[b] == 2:
        flag = i+1
        break;
    else:
        b = A[b]-1
print(flag)

Solution (Python):
n = int(input())
a = []
a.append(1)
for i in range(n):
	x = int(input())
	a.append(x)
y = 0
check = True
for i in range(n+1):
	y = a[y]
	if a[y] == 2:
		print(i+1)
		check = False
		break
if check:
	print('-1')

Solution (Python):
N = int(raw_input())
a = [int(raw_input()) for i in xrange(N)]
i = 0
ans = 0
while True:
    if a[i] == -1:
        break
    ni = a[i] - 1
    a[i] = -1
    i = ni
    ans += 1
    if i == 1:
        break
print ans if a[i] != -1 else -1

Solution (Python):



n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
if 2 not in a:
    print(-1)
else:
    cnt = 0
    before_num = 1
    lighted_bottons = [0 for i in range(n + 1)]
    while True:
        cnt += 1
        if a[before_num - 1] == 2:
            print(cnt)
            break
        else:
            if lighted_bottons[a[before_num - 1]] == 1:
                print(-1)
                break
            before_num = a[before_num - 1]
            lighted_bottons[before_num] = 1

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

checked = [0 for _ in range(n)]

cnt = 0
light = 0

while(True):
    if checked[light]:
        print(-1)
        break
    checked[light] = True
    light = inp[light] - 1
    cnt += 1
    if light == 1:
        print(cnt)
        break

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

aa = []
for i in range(n):
    t = int(raw_input())
    aa.append(t)

cnt = 0
b = dict({1: True})
a = aa[0]
while 1:
    cnt += 1
    if a == 2:
        print cnt
        exit()
    a = aa[a-1]
    if a in b:
        print -1
        exit()
    b[a] = True
    if cnt == n:
        print -1
        exit()

Solution (Python):


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

i=1
c=0

while(1):
    if i == 2:
        print c
        break
    elif c >= N:
        print -1
        break
    c+=1
    i=a[i-1]

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    A = []
    for x in range(N):
        y = int(input())
        y -= 1
        A.append(y)
    pos = 0
    cnt = 0
    visited = set()
    while (cnt <= N + 1):
        visited.add(pos)
        if (pos == 1):
            print (cnt)
            exit()
        pos = A[pos]
        cnt += 1
        if pos in visited:
            break

    print (-1)

Solution (Python):
import sys
N = int(input())
ls = []
for i in range(N):
    ls.append(int(input()))
i = 0
cnt = 1
while(cnt <= N):
    k = ls[i]
    if (k == 2):
        print(cnt)
        sys.exit()
    cnt += 1
    i = k-1
print(-1)

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

index = 0
count = 0


while True:
    count += 1
    check[index] = 0
    index = a[index]

    if check[index] == 0:
        print(-1)
        break
    if index == 1:
        print(count)
        break

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

past = [0 for _ in range(n)]

cnt = 0
now = 1

while(True):
	if past[now-1] == 1:
		print(-1)
		exit(0)
	if now == 2:
		print(cnt)
		exit(0)
	past[now-1] = 1
	cnt += 1
	now = a[now-1]

Solution (Python):


N=int(input())
a=[int(input()) for i in range(N)]
light=[0 for i in range(N)]
light[0]=1
push=0
botton=0

for k in range(N):

 light[botton]=0
 light[a[botton]-1]=1

 botton=a[botton]-1
 push=push+1
 if light[1]==1:
   print(push)
   break




if light[1]!=1:
  print(-1)

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

now = 0
cnt = -1
for i in range(1, N):
  now = a[now]
  if(now == 1):
    cnt = i
    break

print(cnt)

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

used = set()
now = 1
ret = 0
while True:
    if now == 2:
        print(ret)
        exit()
    elif now in used:
        print(-1)
        exit()
    used.add(now)
    now = a[now - 1]
    ret += 1

Solution (Python):

"""
http://abc065.contest.atcoder.jp/tasks/abc065_b

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    N = int(input())
    a = [int(input()) for _ in range(N)]
    a.insert(0, '@')            


    possible = False
    next_btn = a[1]
    visited = set([1])
    while next_btn not in visited:
        if next_btn == 2:
            possible = True
            break
        visited.add(next_btn)
        next_btn = a[next_btn]
    if possible:
        print(len(visited))
    else:
        print(-1)


if __name__ == '__main__':
    main(sys.argv[1:])

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

Solution (Python):
def solve():
    n = int(input())
    lst = [int(input())-1 for _ in[0]*n]
    visit = set()
    current = 0
    while True:
        visit.add(current)
        current = lst[current]
        if current == 1:
            print(len(visit))
            break
        if current in visit:
            print(-1)
            break

if __name__=="__main__":
    solve()

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

btn = 0
hist = set()
ans = 0
while True:
    if btn == 1:
        break

    if btn in hist:
        ans = -1
        break

    hist.add(btn)
    btn = a[btn]
    ans += 1

print(ans)

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

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

n = 1
saw = [0] * N
now = 1

while True:
    if a[now-1] == 2:
        break
    else:
        if saw[a[now-1]-1] == 1:
            n = -1
            break
        else:
            now = a[now-1]
            saw[now-1] = 1
            n += 1

print(n)

Solution (Python):
import sys

n = int(input())
a = [int(x) for x in sys.stdin]

x = 1
i = 0

while i < n and x != 2:
    x = a[x-1]
    i += 1

if x == 2:
    print(i)
else:
    print(-1)

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

x = 1
i = 0

while i < n and x != 2:
    x = a[x-1]
    i += 1

if x == 2:
    print(i)
else:
    print(-1)

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

x = 1
y = [0] * n

while y[x-1] != 2 and x != 2:
    y[x-1] += 1
    x = a[x-1]

if x == 2:
    print(y.count(1))
else:
    print(-1)

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

x = 1
i = 0

while i < 100000 and x != 2:
    x = a[x-1]
    i += 1

if x == 2:
    print(i)
else:
    print(-1)

Solution (Python):

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

s = set()
v = 1
for i in range(1,N+1):
    if v == 2:
        print(len(s))
        break
    elif v in s or i == N:
        print(-1)
        break
    else:
        s.add(v)
        v = a[v]

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

    idx, flag = 1, 1
    for i in range(1, n+1):
        idx = a_list[idx-1]
        if idx == 2:
            print(i)
            flag = 0
            break

    if flag: print(-1)
    

if __name__ == "__main__":
    main()

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

a_n=[int(input()) for i in range(N)]
import sys

i=1
count=0
for k in range(N):
    if a_n[i-1]==2:
        count+=1
        print(count)
        sys.exit()
    else:
        i=a_n[i-1]
        count+=1

print(-1)

Solution (Python):
n = int(input())
a = [int(input()) if(i) else 0 for i in range(n+1)]
distance = [None] * (n+1)
queue = [(1, 0)]
while(len(queue)):
    p, d = queue.pop()
    if(p==2):
        print(d)
        break
    if(distance[p]!=None or d==100001):
        print(-1)
        break
    distance[p] == d
    nextp = a[p]
    queue.append((nextp, d+1))

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

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

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

now = 1
count = 0

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

if count == N:
    print(-1)

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

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

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

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

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

i = 0
j = 0
count = 0
ans = 0

while i >= 0:
    if i == 1:
        ans += 1
        break
    else:
        j = i
        i = li[i] - 1
        count += 1
        li[j] = 0
if ans == 1:
    print(count)
else:
    print(-1)

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

    a = list(A)
    ans = 0
    try:
        idx = a.index(1)
    except:
        idx = 0
        ans += 1

    while True:
        btn = a[idx]
        if btn == 0:
            ans = -1
            break
        elif btn == 2:
            break
        a[idx] = 0
        idx = btn - 1
        ans += 1

    print(ans)

main()

Solution (Python):
import collections

N = int(input())
a = collections.deque()
for i in range(N):
    a.append(int(input()))
    
s = a[0]
cnt = 1

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

Solution (Python):
import sys

N = int(input())
a = [int(i) for i in sys.stdin]
    
s = a[0]
cnt = 1

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

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

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

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

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

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

cnt = 0
s = a[0]

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

Solution (Python):

import sys
bot = [ int(i) for i in sys.stdin ]
now = bot[1]
count = 1
for i in range(bot[0]):
    if now == 2:
        print(count)
        exit()
    count += 1
    now =  bot[now]
print(-1)

Solution (Python):
from collections import defaultdict, deque
import sys

N = int(input())
d = defaultdict(int)
for i in range(N):
    d[i+1] = int(input())



q = deque()
q.append(1)

point = 1
count = 1

while N > 0:
    if d[point] == 2:
        print(count)
        sys.exit()
        
    q.append(d[point])
    point = d[point]
    count += 1
    N -= 1

print(-1)

Solution (Python):
N = int(input())
buttons = [int(input()) for i in range(N)]
light_offset = 0
cnt = 0

while True:
    
    if light_offset == 1:
        print(cnt)
        break
    light_offset = buttons[light_offset] - 1
    cnt += 1
    
    if cnt > N:
        print(-1)
        break

Solution (Python):
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))

n = ri()
s = [-1]
ans = 0
light = 1
flag = False
for i in range(n):
    s.append(ri())
for i in range(n):
    ans += 1
    light = s[light]
    if(light == 2):
        flag = True
        break
print(ans if(flag) else -1)

Solution (Python):
n=int(input())
arr=[]
for _ in range(n):
    arr.append(int(input())-1)
cur=0
for i in range(n):
    nxt = arr[cur]
    if nxt==0:
        print(-1)
        break
    elif nxt==1:
        print(i+1)
        break
    cur=nxt
else:
    print(-1)

Solution (Python):



import sys
import re
import math
import collections
import itertools
import functools

DEBUG = True
DEBUG = False

def dbg(*args):
    if DEBUG:
        print("DBG: ", file=sys.stderr, end="")
        print(*args, file=sys.stderr)



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

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

    pressed = [False] * (N+1)
    button = 1
    cnt = 0
    while not pressed[button]:
        pressed[button] = True
        cnt += 1
        button = A[button]
        if button == 2:
            print(cnt)
            return

    print(-1)

if __name__ == "__main__": main()

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

count=0
next_push=0
while(next_push != 1 and count < 1e5+1):
    next_push = a[next_push]
    count += 1

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

Solution (Python):
import sys
a = [int(i) for i in sys.stdin]
now = a[1]
count = 0
while count!=a[0]:
    count += 1
    if now == 2:
        print(count)
        exit()
    now = a[now]
print(-1)

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

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

Solution (Python):
from collections import defaultdict


def main():
    N = int(input())
    a_list = [int(input()) for _ in range(N)]
    used = set()
    now = 1
    ans = 0
    while True:
        if now == 2:
            print(ans)
            return
        now = a_list[now - 1]
        if now in used:
            print(-1)
            return
        used.add(now)
        ans += 1

if __name__ == '__main__':
    main()

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

ans = 0
i = 0
used = set([0, 1])
while True:
    if ans != 0 and i in used:
        break
    used.add(i)
    i = A[i]
    ans += 1
if i != 1:
    ans = -1
print ans

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

jmps = [int(input()) - 1 for _ in range(N)]

now = 0
ans = 0
for i in range(N):
    if now == 1:
        ans = i
        break
    now = jmps[now]

ans = ans or -1
print(ans)

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

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

now = 0
ans = 0
for i in range(N):
    if now == 1:
        ans = i
        break
    now = jmps[now]

ans = ans or -1
print(ans)

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

idx = 0
success = False
for i in range(N):
    idx = a[idx]-1
    if idx == 1:
        print("%d"%(i+1))
        success = True
        break
        
if success == False:
    print("%d"%(-1))

Solution (Python):
n = int(input())
arr = [int(input()) for i in range(n)]
push = 1
count = 0
while count <= n:
    push = arr[push - 1]
    count += 1
    if push == 2:
        print(count)
        break
else:
    print(-1)

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

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

i = 0
now = 0
while True:
    if now == 1:
        print(i)
        break
    if i == N:
        print("-1")
        break
    now = a[now] - 1
    i = i + 1

Solution (Python):


n = int(input())
ls = []
for i in range(n):
    ls.append(int(input()))
lighted = [False for i in range(n)]
inx = 1
lighted[0] = True
cnt = 0
while 1:
    dest = ls[inx - 1]
    if not lighted[dest - 1]:
        if dest == 2:
            cnt += 1
            break
        else:
            lighted[dest - 1] = True
    else:
        cnt = -1
        break
    inx = dest
    cnt += 1
    
print(cnt)

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

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

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

Solution (Python):

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

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

Solution (Python):
from sys import stdin 
a = [0]
n = int(stdin.readline())
for _ in xrange(n):
 a.append(int(stdin.readline()))
ans = -1
cur = 0
x = 1
s = set()
while True:
 if x in s:
  break
 if x==2:
  ans = cur
  break
 s.add(x)
 cur+=1
 x = a[x]
print ans

Solution (Python):
n = int(input())
arr = [int(input()) for i in range(n)]
push = 1
count = 0
while count <= n:
    push = arr[push - 1]
    count += 1
    if push == 2:
        print(count)
        break
else:
    print(-1)

Solution (Python):
def solve(nexts):
    pushed = {0}
    now_on = 0
    count = 0
    while True:
        now_on = nexts[now_on]
        count += 1
        if now_on in pushed:
            return -1
        if now_on == 1:
            return count
        pushed.add(now_on)


n = int(input())
print(solve([int(input()) - 1 for _ in range(n)]))

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

vis = [False for _ in range(n)]

ans = 0
now = 0
while True:
    if vis[now]:
        ans = -1
        break

    vis[now] = True
    ans += 1
    now = a[now]
    if now == 1:
        break

print(ans)

Solution (Python):
N = int(input())
A = [int(input()) - 1 for _ in range(N)]
now = 0
se = set([])
ans = 0

while True:
    if now == 1:
        break
    now = A[now]
    if now in se:
        print(-1)
        exit()
    se.add(now)
    ans += 1
print(ans)

Solution (Python):
def solve():
    n = int(input())
    l = []

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

    a = 1
    hist = set([])
    while(True):
        if(a-1 in hist):
            return -1
        if(a-1 == 1):
            return len(hist)
        hist.add(a-1)
        a = l[a-1]

if(__name__ == "__main__"):
    print(solve())

Solution (Python):

import numpy as np
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
    
p=1
cnt=0
while 1:
    cnt+=1
    p = a[p-1]
    if(p==2):
        print(cnt)
        break
    if(cnt>N):
        print(-1)
        break

Solution (Python):
import sys

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

count = 0
now = 1

while count < N:
    count += 1
    now = a[now - 1]
    if now == 2:
        print(count)
        sys.exit()

print(-1)

Solution (Python):

import sys
N = int(input())
a = [ int(input()) for tmp in range(N) ]
ans = 0
index = 0
while ans < len(a):
    sav = index 
    index = a[sav]-1 
    ans += 1
    if index == 1:
        print(ans)
        sys.exit()
    if a[index] == sav+1: 
        print("-1")
        sys.exit()
print("-1")

Solution (Python):
n = int(input())
buttons = [int(input()) for i in range(n)]
pushing = 1
counter = 0
while counter <= n:
    pushing = buttons[pushing - 1]
    counter += 1
    if pushing == 2:
        print(counter)
        break
else:
    print(-1)

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

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

count=1
ans=0

while c[1]==0:

    if c[count-1] != 0:
        print(-1)
        exit()
    else:
        c[count-1]=1
        count=a[count-1]
        ans += 1


print(ans-1)

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

for i in range(N):
	a[i] = int(a[i])
cnt = 0
trn = 0
while trn != 1:
	if trn == -2:
		cnt = -1
		break
	

	hako = trn
	trn = a[trn] - 1
	a[hako] = -1
	cnt += 1
	
print cnt

Solution (Python):


import sys
import subprocess
import json
import time
import math
import re
import sqlite3

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

visited = [False] * N
cur = 0
cnt = 0
while True:
    if visited[cur]:
        print(-1)
        exit(0)    
    if cur == 1:
        print(cnt)
        exit(0)

    visited[cur] = True    
    cur = a[cur]
    cnt += 1

Solution (Python):
import sys

n=int(raw_input())
l=[]
cnt=0
for i in xrange(n):
 l.append(int(raw_input()))
if l[0]==2:
 print 1
 sys.exit()
i=0
cnt=1
next=0
while i<n:
 next=l[next]-1
 if l[next]==2:
  print cnt+1
  sys.exit()
 cnt+=1
 i+=1
print -1

Solution (Python):
n = int(raw_input())
l = [[0,False]]
for _ in range(n):
    l.append([int(raw_input()), False])
l[1][1] = True
p = 1
while True:
    p = l[p][0]
    if l[p][1] == True:
        break
    l[p][1] = True
    if p == 2:
        break
if l[2][1] == True:
    c = 0
    for [_,f] in l:
        if f == True:
            c += 1
    print c - 1
else:
    print -1

Solution (Python):
N=int(input())
a=[int(input()) for i in range(N)]
b=[]
j=1
k=1
c=1
if 2 not in a:
    print(-1)
else:
    while j in range(1,N+1):
         c += 1
         if a[k-1]==1:
                 print(-1)
                 break
         elif a[k-1]==2:
                 print(c-1)
                 break
         else:
            j += 1
            k = a[k-1]
    if c>=(N+1):
        print(-1)

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

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

Solution (Python):
N=int(input())
a=[int(input()) for _ in range(N)]
b=[False]*N
count=0
i=0
b[i]=True
flag=True
while b[1]!=True:
    if(b[a[i]-1]==True):
        print(-1)
        flag=False
        break
    b[a[i]-1]=True
    i=a[i]-1
    count+=1
if flag:
    print(count)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()



def main():
    n = I()
    a = [I()-1 for _ in range(n)]
    f = [False] * n
    t = 0
    c = 0
    while True:
        if t == 1:
            return c
        if f[t]:
            return -1
        f[t] = True
        t = a[t]
        c += 1


print(main())

Solution (Python):
n = int(raw_input())
lst = []
btn = []
for i in range(0, n):
	lst.append(True)
	btn.append(int(raw_input()))
tmp = 0
cnt = 0
while lst[tmp]:
	lst[tmp] = False
	if tmp == 1:
		break
	elif lst[btn[tmp]-1]:
		cnt += 1
	else:
		cnt = -1
	tmp = btn[tmp]-1

print cnt

Solution (Python):

import sys

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


if 2 not in a:
	print "-1"
	sys.exit()

i=1

while a[i-1]!=2:

	if a[i-1]==0:
		print "-1"
		sys.exit()

	
	before=i
	i=a[i-1]
	a[before-1]=0
	
	

print a.count(0)+1

Solution (Python):
import sys

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

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

if light != 2:
    print(-1)

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

	A = [0]

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

	counter = 1
	index = 1
	
	if A[index] == 2:
		print(counter)
		return
	counter += 1
	index = A[index]
	for i in range(N):
		
			
			
		if A[index] == 2:
			print(counter)
			return
		counter += 1
		
		index = A[index]
	print("-1")
	

training()

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

while True:
    i = a[i-1]
    count += 1
    if i == 1 or count == n+1:
        print(-1)
        break
    elif i == 2:
        print(count)
        break

Solution (Python):
n = int(input()) 
xs = [int(input())-1 for i in range(n)]
v = [False] * n

i = 0
ans = 0
while True:
  if i == 1:
    break
  elif v[i]:
    ans = -1
    break
  v[i] = True
  ans += 1
  i = xs[i]

print(ans)

Solution (Python):
import sys
N=int(input())
a=[int(input()) for i in range(N)]
i=1
s=1
while i<N:
  if a[s-1]==2:
    print(i)
    sys.exit()
  s=a[s-1]
  i+=1
  
print(-1)

Solution (Python):
import sys


def main():
    n = int(input())
    buttons = [int(input()) for _ in range(n)]

    def push(x):
        return buttons[x - 1]

    seq = [push(1)]

    for _ in range(n):
        if seq[-1] == 2:
            print(len(seq))
            return 0
        seq += [push(seq[-1])]

    print(-1)

    return 0


if __name__ == '__main__':
    sys.exit(main())

Solution (Python):
import sys




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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


n = read_int()
a = [read_int() -1 for i in range(n)]
res = -1
s = set()
i = 0
step = 1
while a[i] not in s:
    if a[i] == 1:
        res = step
    s.add(a[i])
    i = a[i]
    step += 1
print(res)

Solution (Python):
n = int(input())
btns = [None]*n
for i in range(n):
    btns[i] = int(input())
cnt = 0
nb = 1
while nb != 2:
    cnt += 1
    nb = btns[nb-1]
    if cnt > n:
        break
if cnt > n:
    print(-1)
else:
    print(cnt)

Solution (Python):

import sys
button = [int(i) for i in sys.stdin]
now = 1
count = 0
for i in range(button[0]):
  if now == 2:
    print(count)
    exit()
  now = button[now]
  count += 1
print(-1)

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