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:
import copy
N=int(input())
a=[0]*(N+1)
cnta=copy.copy(a)
for i in range(1,N+1):
    a[i]=int(input())

button=1

for i in range(100000):
    cnta[button]=cnta[button]+1
    if cnta[2]==1:
        print(sum(cnta)-1)
        break
    elif cnta[button]>=2:
        print("-1")
        break
    button=a[button]

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

def main():
    N = int(readline())
    A = []
    for _ in range(N):
        A.append(int(readline())-1)
    check = [0]*N
    check[0] = 1
    nxt = 0
    ans = 1
    while True:
        nxt = A[nxt]
        if check[nxt] == 1:
            ans = -1
            break
        if nxt == 1:
            break
        check[nxt] = 1
        ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
p=0
for i in range(n):
  p=a[p]-1
  if p==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()))

check = [1]*n
p = 1
ans = 1
total = 0
while check[p-1] == 1:
    total += 1
    check[p-1] = 0
    if a[p-1] == 2:
        ans = 0
        break
    
    p = a[p-1]


if ans == 1:
    total = -1
print(total)

Submission status: Accepted

Solution code:
n, *a = map(int, open(0))
done = [0] * n
b = 1
cnt = 0
while not done[b - 1]:
    if b == 2:
        print(cnt)
        exit()
    done[b - 1] = 1
    b = a[b - 1]
    cnt += 1
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
btn=[0]*n
ans=1
i=0
while True:
  if btn[i]==1:
    print(-1)
    exit()
  if a[i]==2:
    print(ans)
    exit()
  btn[i]=1
  i=a[i]-1
  ans+=1

Submission status: Accepted

Solution code:
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
# print(a)
cnt = 1
x = a[0]-1
if a[0] == 2:
    print(cnt)
    exit()
for _ in range(10**5+10):

    if a[x] == 2:
        cnt += 1
        print(cnt)
        exit()
    else:
        x = a[x]-1
        cnt += 1
print(-1)

Submission status: Accepted

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

j=0
ans=0
count=[0]*N
while j!=1:
    ans+=1
    j=A[j]-1
    count[j]+=1
    if count[j]>=2:
        ans=-1
        break

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(200000)
n = int(input())
a = [int(input()) for i in range(n)]

switch = [False] * (n + 1)
def button(x,cnt):
    if switch[x]:
        print(-1)
        return -1
    if x == 2:
        print(cnt)
        return
    switch[x] = True
    button(a[x - 1], cnt + 1)
    return cnt

ans = button(1, 0)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N):
    a=int(input())
    A.append(a)
count=0
for i in range(N):
    if i==0:
        now=0
        nex=A[0]
        count+=1
    else:
        
        nex=A[nex-1]
        count+=1
    if nex==2:
        print(count)
        exit()

print(-1)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = [inp() for _ in range(n)]
now = 0
s = set()
s.add(0)
cnt = 0
while True:
    nx = a[now] - 1
    cnt += 1
    if nx == 1:
        print(cnt)
        break
    if nx in s:
        print(-1)
        break
    s.add(nx)
    now = nx

Submission status: Accepted

Solution code:
N = int(input())
button = list()
for i in range(N):
    button.append(int(input()))

pre  = 1
# print(button)
i = 0
c = 1
rireki = [0]*N
while all: 
    if(button[i]==2):
        print(c)
        break
    rireki[i] +=1
    if(rireki[i]==2):
        print(-1)
        break
    i = button[i]-1
    c+=1

Submission status: Accepted

Solution code:
n = int(input())
buttons = [int(input()) for _ in range(n)]
pushed = [False] * n
b = 0
ans = 0
while True:
    ans += 1
    pushed[b] = True
    b = buttons[b] - 1
    if pushed[b]:
        ans = -1
        break
    if b == 1:
        break
        
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    cur = 0
    cnt = [-1] * N
    cnt[0] = 0
    while cur != 1:
        c = cnt[cur]

        cur = a[cur]
        if ~cnt[cur]: break
        cnt[cur] = c + 1

    print(cnt[1] if ~cnt[1] else -1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]

next = 0
ans = 0
while ans <= n:
    ans += 1
    if A[next] == 2:
        print(ans)
        exit()
    next = A[next]-1

print(-1)

Submission status: Accepted

Solution code:
n = int(input())
bot = [int(input()) for i in range(n)]
bot = [-1] + bot
flag = [False]*(n+1)
count = 0
i = 1
while True:
  count += 1
  if flag[i]:
    print(-1)
    break
  flag[i] = True
  if bot[i] == 2:
    print(count)
    break
  i = bot[i]

Submission status: Accepted

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]

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

Submission status: Accepted

Solution code:
n = int(input())
a_inputs = [int(input())-1 for i in range(n)]

visited = [0]*n
x = 0

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=[0]*n
c=0
ans=0
b[0]=1
while True:
    c=a[c]-1
    ans+=1
    
    if c==1:
        print(ans)
        exit()
    if b[c]==1:
        print(-1)
        exit()
    b[c]=1
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

buttons = []

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

next = 0
cnt = 1
for i in range(n):
    next = buttons[next] - 1
    if next == 1:
        print(cnt)
        exit()
    cnt += 1
print(-1)

Submission status: Accepted

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

cnt, i = 0, 0
while cnt < N:
    cnt += 1
    if A[i] == 1:
        break
    i = A[i]
print(cnt if cnt < N else -1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
used = [False]*n
f = True
cur = 0
cnt = 0
while f:
    cnt += 1
    cur = a[cur]-1
    if cur == 1:
        break
    if used[cur]:
        f = False
        cnt = -1
    else:
        used[cur] = True
print(cnt)

Submission status: Accepted

Solution code:
N,*a = map(int,open(0).read().split())
a.insert(0,0)
flg = False
on = 1
cnt = 0
for i in range(N):
    on = a[on]
    cnt += 1
    if on == 2:
        print(cnt)
        exit()
print(-1)

Submission status: Accepted

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

  ans = 0
  idx = 0
  while True:
    if a[idx] == -1:
      print("-1")
      return
    if idx == 1:
      break
    ans += 1
    tmp = a[idx]
    a[idx] = -1
    idx = tmp
  print(ans)
  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
lamp = 1
ans=0
for i in range(n):
    if lamp - 1 == 1:
        print(i)
        sys.exit()
    lamp = a[lamp - 1]
print(-1)

Submission status: Accepted

Solution code:
N=int(input())
a=[]
for i in range(N):
  a.append(int(input()))
success=0
current=1
for i in range(N+5):
  if current==2:
    success=i
    break
  current=a[current-1]
if success==0:
  print(-1)
else:
  print(success)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
A = [0]*n
i = 0
ok = False
cnt = 0
for _ in range(n):
  cnt += 1
  A[i] = 1
  if a[i] == 2: 
    ok = True
    break
  i = a[i]-1
  if A[i] == 1: break
print(cnt if ok else -1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input())-1 for i in range(N)]
used = [False] * N
a = 0
ans = 0
f = False

while True:
    if used[a]: 
        f = True
        break
    if a == 1: break
    used[a] = True
    ans += 1

    a = A[a]

if f: ans = -1
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

check = [True] * (N + 1)

count = 0
tmp = 1
while check[tmp]:
    count += 1
    check[tmp] = False
    if A[tmp - 1] == 2:
        print (count)
        exit()
    tmp = A[tmp - 1]

print (-1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
A.insert(0,0)
hist = [0 for _ in range(N+1)]
ind = 1
hist[1] = 1
cnt = 0
flag = 0
while True:
    cnt += 1
    ind = A[ind]
    if ind==2:
        flag = 1
        break
    if hist[ind]==1:
        flag = 0
        break
    hist[ind] = 1
if flag==1:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
button = [int(input()) for _ in range(N)]

cnt = 0
now = 0
while 1:
    n = button[now]
    cnt += 1
    if n==2:
        break
    if cnt>N+2:
        cnt = -1
        break
    now = n-1
print(cnt)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections

# import bisect

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    d = []
    for _ in range(n):
        d.append(ni())

    idx = 0
    for i in range(10 ** 5 + 1):
        idx = d[idx] - 1
        if idx == 1:
            print(i + 1)
            exit(0)

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

d = {}

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

now = 1
ans = -1
for i in range(N+100):
    now = d[now]
    if now == 2:
        ans = i+1
        break
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [0] + [int(input()) for _ in range(n)]
    checked = [False for _ in range(n + 1)]
    answer = 0
    ind = 1
    while True:
        answer += 1
        checked[ind] = True
        ind = a[ind]
        if checked[ind]:
            answer = -1
            break
        elif ind == 2:
            break
    print(answer if answer > -1 else -1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(int(input()) for _ in range(n))

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

    print(cnt)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
As = [int(input()) for i in range(n)]
As = [a-1 for a in As]
cnt = 1
v = 0
while cnt <= n:
    v = As[v]
    if v == 1:
        print(cnt)
        exit()
    cnt += 1
print("-1")

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
for i in range(n):
    a = int(input())
    dic[i] = a-1
already = set()
already.add(0)
cur = 0
count = 0
while True:
    cur = dic[cur]
    count += 1
    if cur == 1:
        print(count)
        break
    if cur in already:
        print(-1)
        break
    already.add(cur)

Submission status: Accepted

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

s = 0
for i in range(n + 10):
    nxt = a[s] - 1
    if nxt == 1:
        print(i + 1)
        exit()
    s = nxt
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
now = 0
for ans in range(n):
  now = A[now]-1
  if now == 1:
    print(ans+1)
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
x = 1
for i in range(N+5):
    x = A[x-1]
    if x==2:
        print(i+1)
        exit()
print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())

a=[]
for i in range(n):
  a.append(int(input()))
b=0  
s=0
while s !=1:
  s=a[s]-1
  b+=1
  if b==n:
    b=-1
    break
print(b)

Submission status: Accepted

Solution code:
n = int(input())
arr = []
tmp = 0
res = -1
for i in range(n):
  arr.append(int(input()))
for i in range(n):
  if arr[tmp] == 2 and res == -1:
    res = i + 1
  tmp = arr[tmp] - 1
print(res)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
nxt = a[0]-1
cnt = 1
if a[0]-1 == 1:
  print(cnt)
  exit()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# B
n = int(input())
a = [0]
for i in range(n):
    a.append(int(input()))
    
i = 1
ans = 0
while True:
    ans += 1
    if a[i] == 2:
        print(ans)
        break
    if ans > len(a):
        print(-1)
        break
    i = a[i]

Submission status: Accepted

Solution code:
n=int(input())
A=[]
for i in range(n):
  A.append(int(input()))
k=1
for i in range(n):
  if A[k-1]==2:
    print(i+1)
    exit()
  k=A[k-1]
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
lst = [0 for _ in range(n + 1)]
num_dict = {}
for i in range(n):
    num_dict[i+1] = int(input())

count = 0
num = 1
lst[1] = -1
while 1:
    count += 1
    num = num_dict[num]
    if num == 2:
        print(count)
        break
    if lst[num] == -1:
        print(-1)
        break
    lst[num] = -1

Submission status: Accepted

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

def solve(A):
    ans = 0
    visited = [True] + [False]*(N-1)
    i = 0
    while i!=1:
        i = A[i]-1
        if visited[i]==True:
            break
        visited[i]=True
        ans += 1
    if i==1:
        return ans 
    return -1
print(solve(A))

Submission status: Accepted

Solution code:
N = int(input())
A = list()
light = 1
count = 0
for i in range(N):
  A.append(int(input()))
for i in range(N):
  if light == 2:
    print(count)
    count = 0
    break
  else:
    light = A[int(light)-1]
    count += 1
    if count  == N:
      print(-1)
      break

Submission status: Accepted

Solution code:
n = int(input())
a_lst = [int(input()) for _ in range(n)]

button = a_lst[0]

cnt = 1 #必ずボタン1は押すため一回はカウント
ans = 0
for i in range(n):
    button = a_lst[button-1]
    if a_lst[0] == 2:
        ans = 1
        break
    if button == 2:
        ans = cnt + 1 #2と書かれたボタンを押す分の一回を追加
        break
    cnt += 1

if cnt == n+1 and ans == 0: #cntの初期値が1だからn+1
    print("-1")
else:
    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())

B = [int(input())-1 for i in range(n)]


def fun(k):
    if B[k] == 1:
     	return -5                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n+1):
    cnt = fun(cnt)
    ans +=1
    
    if cnt ==-5:
        print(ans)
        sys.exit() 
print(-1)
sys.exit()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()

n=I()
A=[I()-1 for i in range(n)]
i=0
point=0
while i<n:
    point=A[point]
    i+=1
    if point==1:
        print(i)
        sys.exit()
print(-1)

Submission status: Accepted

Solution code:
def main():
    button = [-1]
    n = int(input())
    for i in range(n):
        button.append(int(input()))
    count = 0
    push = 1
    while count <= n:
        push = button[push]
        count += 1
        if push == 2:
            break
    print(count) if count < n else print(button[0])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(500*500)
n = int(input())
a = [int(input()) for _ in range(n)]
def search(i, p):
    if p > n:
        print(-1)
        exit()
    elif i == 2:
        print(p)
        exit()
    else:
        search(a[i-1], p+1)

search(1, 0)

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
cnt = 0
a = [0] + a
i = 1
while 1:
    tmp = i
    i = a[i]
    cnt += 1
    if tmp == i:
        print(-1)
        break
    elif i == 2:
        print(cnt)
        break
    elif cnt > n:
        print(-1)
        break

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
p = 1
while True:
    p = A[p-1]
    cnt += 1
    if p == 2 or cnt > n:
        break
if p == 2:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
i = 1
count = 0
btn = {1}
if 2 not in a:
  print(-1)
  exit()
while(1):
  i = a[i-1]
  count += 1
  if i in btn:
    print(-1)
    break
  if i == 2:
    print(count)
    break
  btn.add(i)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))
now = 1
for i in range(150000):
  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)]
i = 1
count = 0
btn = [1]
while(1):
  i = a[i-1]
  count += 1
  if count>n:
    print(-1)
    break
  if i == 2:
    print(count)
    break
  btn.append(i)

Submission status: Accepted

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

def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()
a = []
for _ in range(N):
    a.append(INT() - 1)

light_idx = 0

ans = 0
while True:
    light_idx = a[light_idx]
    ans += 1

    if light_idx == 1:
        break

    if ans > N:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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

answer=0
pos=1
while True:
  if answer>N:
    print(-1)
    break
  elif pos==2:
    print(answer)
    break
  else:
    pos=alist[pos]
    answer+=1

Submission status: Accepted

Solution code:
N=int(input())

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

answer=0
pos=1
set_pos=set()
while True:
  if pos==2:
    print(answer)
    break
  else:
    pos=alist[pos]
    answer+=1

  if pos in set_pos:
    print(-1)
    break
  else:
    set_pos.add(pos)

Submission status: Accepted

Solution code:
N = int(input())
a_li = [int(input()) for i in range(N)]
a_li.insert(0,-1)
osu = 1
for i in range(N):
    ima = a_li[osu]
    if ima == 2:
        print(i+1)
        exit()
    osu = ima
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = []
ex = [0]*N
light = 0
c = 0
for i in range(N) :
    A.append(int(input()))

while light != 1 :
    if ex[light] == 1 :
        print('-1')
        exit()
    ex[light] = 1
    light = A[light]-1
    c += 1
    

print(c)

Submission status: Accepted

Solution code:
#B
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
lamp = 1
ans=0
for i in range(n):
    if lamp - 1 == 1:
        print(i)
        sys.exit()
    lamp = a[lamp - 1]
print(-1)

Submission status: Accepted

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

start = 0
counter = 1
temp_index = a[start]

while counter <= 10**5:

    if temp_index - 1 == 1:
        print(counter)
        exit()

    temp_index = a[temp_index-1]

    counter += 1

print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
keep=1
count=0
ababa=0
for i in range(n):
  count+=1
  keep=a[keep-1]
  if keep==2:
    ababa=1
    print(count)
    break
if ababa==0:
  print(-1)

Submission status: Accepted

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

while i != 1 and count <=10 ** 5:
  i = a[i] -1 
  count += 1

if i == 1:
  print(count)

else:
  print('-1')

Submission status: Accepted

Solution code:
n = int(input())
a = [[int(input())-1, False] for _ in range(n)]
a[0][1] = True
now = 0
cnt = 0
while True:
    now = a[now][0]
    if a[now][1]:
        print(-1)
        break
    a[now][1] = True
    cnt += 1
    if now == 1:
        print(cnt)
        break

Submission status: Accepted

Solution code:
N = int(input())
lis = []
for i in range(N):
    lis.append(int(input()))
a = 1
for j in range(N):
    a = lis[a-1]
    if a == 2:
        print(j+1)
        exit()
print(-1)

Submission status: Accepted

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

i = 1
already_pressed = set([i])
for count in range(1,N+1):
    i=a[i]
    if i in already_pressed:
        print(-1)
        exit()
    if i==2:
        print(count)
        exit()
    already_pressed.add(i)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
seen = {0}
i = 0
ans = 0
while True:
    i = a[i]-1
    ans += 1
    if i==1:
        print(ans)
        break
    if i in seen:
        print(-1)
        break
    seen.add(i)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
tmp=0
for i in range(n+1):
  tmp=a[tmp]-1
  if tmp==1:
    print(i+1)
    exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    n = II()
    aa = {i: II()-1 for i in range(n)}
    seen = set()
    s = 0
    cnt = 0
    while True:
        v = aa[s]
        cnt += 1
        if v == 1:
            break
        if not aa[s] in seen:
            seen.add(aa[s])
            s = aa[s]
        else:
            print(-1)
            return None
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
As = []
for i in range(N):
    As.append(int(input()))
btn = 1
for i in range(N):
    btn = As[btn-1]
    if btn == 2:
        print(i+1)
        exit(0)
print(-1)

Submission status: Accepted

Solution code:
# N = int(input())
# As = []
# ans = 0
# for i in range(N):
#     As.append(int(input()))
# if 2 in As:
#     pressed_btns = {2}
#     sagasu_btn = 1
#     while True:
#         if sagasu_btn in pressed_btns:
#             print(sagasu_btn)
#             print(pressed_btns)
#             print(-1)
#             exit(0)
#         sagasu_btn = As.index(sagasu_btn + 1)
#         pressed_btns.add(sagasu_btn)
#         ans+=1
#         if sagasu_btn == 0:
#             print(ans)
#             exit(0)

# else:
#     print(-1)

# 普通に順番にシミュレーションする．ただし，「ボタン２が永遠に光らない」場合は「すでに光ったボタンが光る時」である．
# その場合，ボタンが光る回数は合計でNを超えるので， N回試行してボタン２が光らなければ永遠にボタン２は光らない．

N = int(input())
As = []
for i in range(N):
    As.append(int(input()))
btn = 1
for i in range(N):
    btn = As[btn-1]
    if btn == 2:
        print(i+1)
        exit(0)
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) - 1 for i in range(N)]
c,s = 1, a[0]

while s != 2 and c < N:
    c = c + 1
    s = a[s-1]

now = 0
c = 0

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

Submission status: Accepted

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

cnt = 1
pos = 0
while cnt < A:
    if a[pos] == 1:
        print(cnt)
        exit()
    else:
        pos = a[pos]
        cnt += 1
print(-1)

Submission status: Accepted

Solution code:
import numpy as np
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = []
[a.append(int(readline())) for _ in range(n)]

check = [0] * n
check[0] = 1
next = 0
times = 0
while True:

    next = a[next] - 1
    times += 1

    if next == 1 :
        print(times)
        break
    
    if check[next] != 0:
        print(-1)
        break

    check[next] += 1

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
a = [0] + a
count = 0
i = 1
while i != 2:
    j = i
    i = a[i]
    a[j] = -1
    count  += 1
    if i == -1:
        count = -1
        break
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
check = [0 for i in range(n)]
check[0] = 1
i = 0
cnt = 0
while True:
	cnt += 1
	x = a[i]
	if x-1 == 1:
		print (cnt)
		exit()
	if check[x-1] == 1:
		print(-1)
		exit()
	else:
		check[x-1] = 1
	i = x-1

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

    a[i] = 0
    i = next_i

Submission status: Accepted

Solution code:
n = int(input())
num = 0
count = 0
a_list = []

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

OK = False

for i in range(n):
    num = a_list[num]
    count += 1
    if num == 1:
        OK = True
        break
if OK:    
    print(count)
else:
    print(-1)

Submission status: Accepted

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

push = set([])
ans = 0
b = 0

while 1:
  if b in push:
    print(-1)
    exit()
  push.add(b)
  b = A[b]
  ans += 1

  if b== 1:
    print(ans)
    break

Submission status: Accepted

Solution code:
N=int(input())
a=[]
count=0
ischecked=[0]*N
q=[0]
for i in range(N):
    a.append(int(input())-1)
while q:
    now=q.pop()
    count+=1
    nex=a[now]
    if nex==1:
        print(count)
    elif ischecked[now]==1:
        print(-1)
    else:
        ischecked[now]=1
        q.append(nex)