Problem:
 Score : 200 points    Problem Statement Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.  The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N .
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i .
When Button i is not lighten up, nothing will happen by pressing it.  Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.  Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.      Constraints  2 ≤ N ≤ 10^5  1 ≤ a_i ≤ N         Input Input is given from Standard Input in the following format:  N  a_1  a_2 : a_N       Output Print -1 if it is impossible to lighten up Button 2 .
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2 .        Sample Input 1 3
3
1
2      Sample Output 1 2  Press Button 1 , then Button 3 .       Sample Input 2 4
3
4
1
2      Sample Output 2 -1  Pressing Button 1 lightens up Button 3 , and vice versa, so Button 2 will never be lighten up.       Sample Input 3 5
3
3
4
2
4      Sample Output 3 3

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

i = 1
cnt = 0
while i != 2 and cnt <= n + 1:
    i = a[i - 1]
    cnt += 1
    
if n < cnt:
    print(-1)
else:
    print(cnt)

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

cnt = 1
idx = 1
for i in range(n):
    if a[idx] == 2:
        print(cnt)
        break

    else:
        idx = a[idx]
        cnt += 1
else:
    print(-1)

Solution (Python):
N=int(raw_input())
a=[-1]
for _ in range(N):
    a.append(int(raw_input()))
 
 
i=1
c=0
while(1):
    if i==2:
        print c
        break
    elif c>=N:
        print -1
        break
    c+=1
    i=a[i]

Solution (Python):
n = int(input())
b = []
b_old = []
for i in range(n):
    tmp = int(input())
    b.append(tmp)
n_b = 1
count = 0
flag = 0
while(count < n):
    n_b = b[n_b-1]
    count += 1
    if n_b == 2:
        print(count)
        flag = 1
        break
if flag == 0:
    print("-1")

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

    b = 1
    pushed = set()
    push_count = 0
    while True:
        push_count += 1
        pushed.add(b)
        b = a[b - 1]
        if b in pushed:
            print(-1)
            break

        if b == 2:
            print(push_count)
            break

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
N = int(input())
a = np.zeros(N+1)
for i in range(N):
    a[i+1] = int(input())
i = 1
n = 0
for j in range(N):
    n = n+1
    i = a[i]
    if i == 2:
        print(n)
        break
if i!=2:
    print(-1)

Solution (Python):


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

for idx in range(0,N,1):
    a.append(int(input())-1)


lit=0
length=0
ans=None
for idx in range(0,N,1):
    lit=a[lit] 
    length+=1
    if lit == 2-1:
        ans=True
        break
        



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

Solution (Python):
import sys
input = sys.stdin.readline
def main():
  N = int(input())
  al = []
  for _ in range(N):
    al.append(int(input())-1)
  bl = [False]*N
  i = 0
  nb = 0
  while bl[i] == False:
    bl[i] = True
    i = al[i]
    nb += 1
    if i == 1:
      print(nb)
      break
  else:
    print(-1)

main()

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

flag = [False] * N

current = 1
count = 0

for i in range(N):
    count += 1
    current = a[current - 1]
    if current == 2:
        break
    elif i == (N - 1):
        count = -1
    elif flag[current - 1]:
        count = -1
        break
    else:
        flag[current - 1] = True

print(count)

Solution (Python):
n = int(input())
buttons = []
for _ in range(n):
    buttons.append(int(input()))
def calc():
    count = 0
    now = 1
    flag = True
    for i in range(n):
        if not flag:
            pass
        elif now == 2:
            print(count)
            flag = False
        else:
            now = buttons[now-1]
        count += 1
    if flag:
        print(-1)

calc()

Solution (Python):
input_count = int(input().rstrip())
button_list = [0,]

for _ in range(input_count):
    button_list.append(int(input().rstrip()))    

push_count = 0
next_button = 1
while True:
    next_button = button_list[next_button]
    push_count += 1
    
    if next_button == 2:
        print(push_count)
        break
    
    if push_count == len(button_list):
        print(-1)
        break

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

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
ch = [0] * (10**5 + 1)
numB = 1
ch[1] = numB
c = 1
while True:
    numB = a[numB-1]
    if numB == 2:
        print(c)
        break
    if ch[numB] == 1:
        print(-1)
        break
    c += 1
    ch[numB] = 1

Solution (Python):
import sys

N = int(input())
a=[]
a.append(0)

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

b=[]
for i in range(N+1):
    b.append(1)

index=1
counter=1
if a[1]==2:
    print(1)
    sys.exit()

while True:
    counter+=1
    b[index]=-1
    index = a[index]
    if b[index]==-1:
        print(-1)
        break
    if a[index]==2:
        print(counter)
        break

Solution (Python):
def ini(): return int(input())
def inli(): return list(map(int, input().split()))
def inf(): return float(input())
def inlf(): return list(map(float, input().split()))
def inl(): return list(input())
def pli(): return "".join(list(map(str, ans)))

n =ini()
a=[]
b = [0 for i in range(n)]
for i in range(n):
    a.append(ini())

j = 1
ans = 0
while j !=2:
    if b[j-1] !=0:
        ans = -1
        break
    ans +=1
    b[j-1] +=1
    j = a[j-1]

print(ans)

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
switch = True
i = 1
cnt = 1
roop = [1 for i in range(N)]
while switch == True:
    if roop[i-1] == 0:
        print(-1)
        break
    
    nextbottun =  a[i-1]
    if nextbottun == 2:
        print(cnt)
        break
    roop[i-1] = 0
    i = nextbottun
    cnt += 1

Solution (Python):


import sys

def main(argv):
    N = sys.stdin.readline()
    while N:
        A = [int(raw_input()) - 1 for i in xrange(0, int(N))]

        counter = 0
        visited = set()
        index = 0
        while index != 1 and index not in visited:
            visited.add(index)
            counter += 1
            index = A[index]

        if index != 1:
            counter = -1
        print("{0}".format(counter))

        N = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

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

    index = 0
    count = 0
    number = 1
    flags = [0 for _ in range(N)]
    flags[index] = 1
    while(number != 2):
        number = a[index]
        index = number - 1
        count += 1
        if flags[index] == 1:
            break
        else:
            flags[index] = 1

    if number == 2:
        print(count)
    else:
        print(-1)

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

flg=[0]*(N+1)
count=0
i=1

while count<N:
    if i==2:
        print(count)
        quit()
    i=a[i]
    count+=1

print('-1')
quit()

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

count = 0
nextbottun = 1

while(nextbottun != 2 and count <= 100000):
    nextbottun = a[nextbottun - 1]
    count += 1

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

Solution (Python):
if(__name__=="__main__"):
    N = int(input())
    a =[int(input()) for i in range(N)]
        
    n = -1
    flash = 0
    for i in range(N):
        flash = a[flash] - 1
        if(flash == 1):
            n = i + 1
            break
    
    print(n)

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

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

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

Solution (Python):




N = int(input())

Bo = []

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


S = Bo[0]

if S == 2:
    print(1)

else:
    
    ans = []
    
    for i in range(N):
        S = Bo[S - 1]
        
        if S == 2:
            C = i + 2
            ans.append(C)
        
    if len(ans) != 0:
        print(ans[0])
    
    else:
        print(-1)

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

Solution (Python):


n = input()
a = [input() for _ in xrange(n)]
i = 1
c = 0

while True:
    a[i-1], i = 0, a[i-1]
    c += 1

    if i == 2:
        print c
        exit()

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

Solution (Python):

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

light = 1
count = 0
while count < N:
    
    light = a[light]
    count += 1

    if light == 2:
        print(count)
        break

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

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

a = []

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

c = a[0]

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

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

count = 0
point = 0

while True:
	number = l[point]-1
	l[point] = 1
	point = number
	count += 1
	if number == 1:
		break
	elif number == 0:
		count = -1
		break

print(count)

Solution (Python):
N = int(input())
a = [0]
for i in range(N):
    a.append(int(input()))
haspushed = [0]*(N+1)
reach = 0
itr = 1
i = a[1]
haspushed[1] = 1
while haspushed[i] == 0:
    haspushed[i] = 1
    if i == 2:
        reach=1
        break
    i = a[i]
    itr += 1

if reach == 1:
    print(itr)
else:
    print(-1)

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

num_button = 0
now_light = 0
pushed = set()
while True:
    if now_light in pushed:
        print(-1)
        break

    pushed.add(now_light)
    now_light = a[now_light]-1
    num_button += 1
    if now_light == 1:
        print(num_button)
        break

Solution (Python):
n = int(input())
a_list = []
for i in range(n):
    a_list.append(int(input()))
cnt = 1
i = 0
while a_list[i] != 2:
    i = a_list[i] - 1
    cnt += 1
    if len(a_list) < cnt:
        cnt = -1
        break

print(cnt)

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

now = 0
count = 0
while (now != 1):
    count += 1
    now = arr[now]
    if count > 100000:
        count = -1
        break
print count

Solution (Python):
n = int(input())
a_list = list([0])
for _ in range(n):
    a_list.append(int(input()))
    
count = 0
bangou = 1

while bangou > 0:
    if a_list[bangou] == 0:
        print("-1")
        break
    bangou_next = a_list[bangou]
    count += 1
    if bangou_next == 2:
        print(count)
        break
    
    a_list[bangou] = 0
    bangou = bangou_next

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

Solution (Python):
n = int(input())
controller = [int(input()) for _ in range(n)]
seq = []
button = controller[0]
seq.append(1)

for i in range(n):
    if button == 2:
        print(len(seq))
        exit(0)
    button = controller[button - 1]
    seq.append(button)

print(-1)

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

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

cnt=0
needle=1
while not(needle==2 or cnt >100097):
  needle=a[needle]
  cnt+=1

if cnt>100001:
  cnt=-1
print(cnt)

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


num = 1
for i in range(N):
    if a[num-1][1]:
        print(-1)
        break
    if a[num-1][0] == 2:
        print(i+1)
        break
    a[num-1][1] = True
    num = a[num-1][0]

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

    count_num = 1
    next_num = a[1]
    done_num = set([1])
    while True:
        if next_num in done_num:
            print(-1)
            break
        elif next_num == 2:
            print(count_num)
            break

        count_num += 1
        done_num.add(next_num)
        next_num = a[next_num]

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
c = 1
tf = False
for i in range(N):
    c = a[c-1]
    if c == 2:
        print(i+1)
        tf = True
        break
if not tf:
    print(-1)

Solution (Python):
def b():
    N = int(input())
    A = {i:int(input()) for i in range(1,N+1)}
    tmp = 1; times = 0
    for _ in range(N):
        tmp = A[tmp]; times += 1
        if tmp == 2:
            print(times)
            break
        elif tmp == 1:
            print(-1)
            break
    else:
        print(-1)


b()

Solution (Python):
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
cnt = 0
light = 1
for i in range(N):
	cnt += 1
	light = A[light - 1]
	if light == 2:
		break

if cnt == N:
	print(-1)
else:
	print(cnt)

Solution (Python):

import sys

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

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

print(n)

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

end = {}

end[1] = True
to = a[1]

cnt = 0
while True:
    cnt += 1
    if to == 2:
        print(cnt)
        __import__("sys").exit()
    if end.get(to) == None:
        end[to] = True
        to = a[to]
    else:
        break

print(-1)

Solution (Python):

import sys
N = int(input())
a = [int(input()) for _ in range(N)]
p = 1
for i in range(N):
    if p == 2:
        print(i)
        sys.exit()
    p = a[p-1]
print(-1)

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

ind = 0
cnt = 0
while 1:
    nex = a[ind]
    cnt += 1
    if nex == 2:
        print(cnt)
        break
    if memo[nex] == 1:
        print(-1)
        break
    memo[nex] = 1
    ind = nex-1

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

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

Solution (Python):
n = input()
l = []

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

count = 0
b = 1
while b != 2 and count <= n:
	count += 1
	b = l[b - 1]

if count > n:
	print -1
else:
	print count

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

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

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

Solution (Python):



from __future__ import division, print_function
import sys


def main():
    n = int(sys.stdin.readline().strip())
    a = []
    for _ in range(n):
        a.append(int(sys.stdin.readline().strip()))

    visited = set()

    i = 0
    counter = 0
    while True:
        if i in visited:
            print(-1)
            break
        visited.add(i)
        i = a[i] - 1
        counter += 1
        if i == 1:
            print(counter)
            break
    
    
    
    
    
    


if __name__ == '__main__':
    main()