INTEGER N,B,ANSWER
INTEGER,ALLOCATABLE,DIMENSION(:)::X
READ*,N
ALLOCATE(X(N))
READ*,X
Y=0
B=1
ANSWER=0
DO I=1,N
  ANSWER = ANSWER+1
  IF(X(B)==2) THEN
   PRINT"(I0)",ANSWER
   STOP
  ENDIF
  B=X(B)
END DO
PRINT"(I0)",-1
END