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



Sample input:
3
3
1
2


Sample output:
2

Submission status: Accepted

Solution code:
int_n = int(input())
ar_int = list()
for i in range(int_n):
    ar_int.append(int(input()))

target = 1
counter = 0

while counter < int_n:
    target = ar_int[target - 1]
    counter += 1
    if target == 2: # button 2
        print(counter)
        exit(0)
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

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

#同じ状態に戻ってくるとアウト

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


print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
B = [int(input()) for i in range(N)]
A = [0]
A.extend(B)
past = [1]
next_l = 1
#if not 2 in A:
#  print(-1)
#  exit(0)
for i in range(N):
  next_l = A[next_l]
  #if next_l in past or next_l > N:
  #  print(-1)
  #  exit(0)
  if next_l == 2:
    print(len(past))
    exit(0)
  past.append(next_l)
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
# coding: utf-8
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)

Submission status: Accepted

Solution code:
# coding: utf-8
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)

Submission status: Accepted

Solution code:
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):
#	print(k)
	k=a[k]-1
	if(k==1):
		ans=i+1
		break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#d...縦で入力された複数の要素を配列で取得
n = int(input())
d = []
for i in range(n):
    d.append(input())
# [d1, d2, d3, ..., dN]
a = [int(m) for m in d]#int型に直す

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)

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/abc065/tasks/abc065_b

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()
    # model()

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/abc065/tasks/abc065_b

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()
    # model()

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/abc065/tasks/abc065_b

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

Submission status: Accepted

Solution code:
n = int(input())
num_list = []
count = 1
for i in range(n):
    num_list.append(int(input())-1)
#[2,2,3,1,3]
#1があるindexに行くのが目標
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
botan = [int(input()) for i in range(n)]
light = 0 # 1番目のボタンが初め光ってる
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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]

Submission status: Accepted

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