output_description: If we can make X equal to A by repeating the operation, print the minimum
  number of operations required. If we cannot, print -1.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      A_1
      :
      A_N
attempt: c = g = p = e = 0
  for i in range(int(input())):
      if not e:
          if (a := int(input())) > i:
              e = 1
          else:
              if i != 0:
                  (g, c, p) = (g + 1, c + 1, a) if a == p + 1 else (g + a, 0, a)
  print(-1 if e else g)
  
problem_description: Statement
  
  There is a sequence X of length N, where every element is initially 0. Let X_i
  denote the i-th element of X.
  
  You are given a sequence A of length N. The i-th element of A is A_i.
  Determine if we can make X equal to A by repeating the operation below. If we
  can, find the minimum number of operations required.
  
    * Choose an integer i such that 1\leq i\leq N-1. Replace the value of X_{i+1} with the value of X_i plus 1.
samples: [{"input": "4\n    0\n    1\n    1\n    2", "output": "3\n    \n\nWe can make X equal to A as follows:\n\n  * Choose i=2. X becomes (0,0,1,0).\n  * Choose i=1. X becomes (0,1,1,0).\n  * Choose i=3. X becomes (0,1,1,2).\n\n* * *"}, {"input": "3\n    1\n    2\n    1", "output": "-1\n    \n\n* * *"}, {"input": "9\n    0\n    1\n    1\n    0\n    1\n    2\n    2\n    1\n    2", "output": "8"}]