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