#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;

bool is_finish(list<int> t){
  int a = 0;
  for(list<int>::iterator it = t.begin() ; it != t.end() ; it++){
    if(*it != ++a) return false;
  }
  return true;
}

int main(){
  int n;
  list<int> block;
  while(cin >> n, n){
    int a;
    for(int i = 0 ; i < n ; i++){
      cin >> a;
      block.push_back(a);
    }
    
    int ans = 0;
    while(!is_finish(block)){
      if(ans > 10000) break;
      int cnt = 0;
      
      for(list<int>::iterator it = block.begin() ; it != block.end() ; it++){
	(*(it))--;
	cnt++;
      }
      block.push_back(cnt);
      block.remove(0);
      ans++;
    }
    if(ans > 10000) cout << -1 << endl;
    else cout << ans << endl;
    block.clear();
  }
  return 0;
}