import Data.IntMap ((!))
import qualified Data.IntMap as M
import Data.List
import Control.Monad

main = do
 n <- readLn
 as <- map read . words <$> getContents
 putStrLn $ maybe "-1" show (trained n (M.fromAscList (zip [1..] as)))

trained n as = do
 let lg = take n $ unfoldr (\i -> Just (i, as!i)) 1
 guard (elem 2 lg)
 return $ length $ (takeWhile (/=2) lg)