import Prelude hiding (log)
import Control.Monad
-- import Control.Arrow ((&&&))
import Data.Array
-- import Data.List (nub, sort, group)
-- import Data.Maybe (fromMaybe)
-- import qualified Data.IntMap as M
type Lights = Array Int Int

main :: IO ()
main = do
    n <- readLn
    as <- replicateM n readLn
    print $ solve (conv n as, 0, []) 1

-- |
--
-- >>> solve (conv 3 [3,1,2],0,[]) 1
-- 2
--
-- >>> solve (conv 4 [3,4,1,2],0,[]) 1
-- -1
--
-- >>> solve (conv 5 [3,3,4,2,4],0,[]) 1
-- 3
solve :: (Lights, Int, [Int]) -> Int -> Int
solve (as, cnt, log) x
  | y == 2       = cnt + 1
  | y `elem` log = -1
  | otherwise    = solve (as, cnt + 1, x : log) y
  where y = as ! x

conv :: Int -> [Int] -> Lights
conv n as = array (1, n) (zip [1 .. n] as)
