import Data.List
import Data.Ord
import Data.Char
import Data.Ratio
import Data.Maybe

main = do
  getLine
  as <- map (read::String->Int) .lines <$> getContents
  print $train (zip [1..] as) [1]

train as nlist
  |next==2 = length nlist
  |elem next nlist = -1
  |otherwise = train as (next:nlist)
    where n = head nlist; next = fromJust $ lookup n as
  