import qualified Data.Vector as V
f l i n=if i==1 then n else if n>(V.length l) then -1 else f l (l V.! i) (n+1)
main=do
 n<-getLine
 l<-V.fromList.map ((+(-1)).read).lines<$>getContents
 print$f l 0 0