#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;


int main(){

	int N,tmp,count,right,pre,sum;
	bool FLG,AGARI;
	vector<int> V;

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		V.clear();

		sum = 0;
		for(int i = 0; i < N; i++){
			scanf("%d",&tmp);
			V.push_back(tmp);
			sum += tmp;
		}

		FLG = false;

		for(int k = 1; ;k++){
			if(k*(k+1)/2 == sum){
				FLG = true;
				break;
			}
			if(k*(k+1)/2 > sum){
				break;
			}
		}

		if(!FLG){
			count = 10000;
		}else{
			count = 0;
		}

		FLG = false;
		while(count < 10000){

			AGARI = true;
			pre = 0;

			for(int i = 0; i < V.size(); i++){
				if(V[i] > 0){
					if(V[i] != pre+1){
						AGARI = false;
						break;
					}
					pre = V[i];
				}
			}

			if(AGARI){
				FLG = true;
				break;
			}

			right = 0;
			for(int i = 0; i < V.size(); i++){
				if(V[i] > 0){
					V[i]--;
					right++;
				}
			}
			V.push_back(right);

			for(int i = 0; i < V.size(); i++){
				if(V[i] == 0){
					V.erase(V.begin()+i);
				}
			}
			count++;
		}

		if(!FLG){
			printf("-1\n");
		}else{
			printf("%d\n",count);
		}
	}

	return 0;
}