#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n;
	while (scanf("%d", &n), n) {
		vector<int>b(n);
		rep(i, n)scanf("%d", &b[i]);
		for (int cnt = 0;; cnt++) {
			if (cnt > 10000) {
				puts("-1"); break;
			}
			bool ok = true;
			rep(i, b.size()) {
				if (b[i] != i + 1)ok = false;
			}
			if (ok) {
				printf("%d\n", cnt); break;
			}
			rep(i, b.size())b[i]--;
			b.push_back(b.size());
			b.erase(remove(b.begin(), b.end(), 0), b.end());
		}
	}
}