main = do
    (n:as) <- map read . words <$> getContents :: IO [Int]
    print $ if check as then solve as else (-1)

check as = head as == 0 && and (zipWith f as (tail as))
    where f a b | a + 1 >= b = True
                  | otherwise = False

solve as = sum $ zipWith f as (tail as)
    where f a b | b == 0 = 0
                | a > b = 2
                | otherwise = 1