#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int main() {
	int n;
	while (cin >> n, n) {
		vector<int> b(n);
		REP(i, n) scanf("%d", &b[i]);
		
		int ans = 0;
		
		while (true) {
			bool flag = true;
			REP(i, b.size()) if (b[i] != i + 1) flag = false;
			if (flag) break;
			
			if (ans > 10000) { ans = -1; break; }

			vector<int> tmp;
			REP(i, b.size()) if (b[i] > 1) tmp.push_back(b[i] - 1);
			tmp.push_back(b.size());

			b = tmp;
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}