main :: IO ()
main = do
  n <- readLn
  if n == 0
    then return ()
    else do
      ls <- getLine
      let ls' = map read . words $ ls
      print $ solve ls'
      main

solve :: [Int] -> Int
solve ls
  | isTriNum (sum ls) = loop ls 0
  | otherwise = -1
  where
    isTriNum :: Int -> Bool
    isTriNum n = check n 1
      where
        check :: Int -> Int -> Bool
        check n c
          | n == 0 = True
          | n < 0 = False
          | otherwise = check (n-c) (c+1)
          
    loop :: [Int] -> Int -> Int
    loop ls c
      | c > 10000 = -1
      | isTri ls = c
      | all (0 <) ls = loop ((map (\x -> x-1) ls) ++ [length ls]) (c+1)
      | otherwise = loop (filter (0 <) ls) c
      where
        isTri :: [Int] -> Bool
        isTri ls = all (\(a,b) -> a == b) (zip ls [1..])