#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> v;

bool check(vector<int> &v)
{
	for (int i = 0; i < v.size(); i++){
		if (v[i] != i + 1) return false;
	}
	return true;
}

void solve()
{
	int cnt = 0;
	while (true){
		if (check(v)){
			printf("%d\n", cnt);
			return;
		}
		else if (cnt > 10000){
			printf("-1\n");
			return;
		}
		
		int tmp = v.size();
		for (int i = 0; i < v.size(); i++){
			v[i]--;
			if (v[i] == 0){
				i = v.erase(v.begin() + i) - v.begin();
				--i;
			}
		}
		v.push_back(tmp);
		
		cnt++;
	}
}

int main()
{
	while (scanf("%d", &n), n){
		v.resize(n);
		for (int i = 0; i < n; i++){
			scanf("%d", &v[i]);
		}
		solve();
	}
}