#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXW = 10004;
int N;
int b[2][MAXW];

bool tri(int a[MAXW]) {
  for(int i = 0; i < MAXW && a[i] != 0; ++i) {
    if(a[i] != i+1) return false;
  }
  return true;
}

int solve() {
  if(tri(b[0])) return 0;
  for(int t = 0; t < 10000; ++t) {
    int p = t%2;
    int q = 1-p;
    fill(b[q], b[q]+MAXW, 0);
    int h = 0;
    int j = 0;
    for(int i = 0; i < MAXW && b[p][i] != 0; ++i, ++j) {
      ++h;
      if(b[p][i] - 1 == 0) --j;
      else b[q][j] = b[p][i] - 1;
    }
    b[q][j] = h;
    if(tri(b[q])) return t+1;
  }
  return -1;
}

int main() {
  while(cin >> N && N) {
    fill(b[0], b[2], 0);
    for(int i = 0; i < N; ++i) {
      cin >> b[0][i];
    }
    cout << solve() << endl;
  }
  return 0;
}