#include <iostream>
#include <vector>
using namespace std;

int main() {

    int n;
    while ( cin >> n && n != 0 ) {

	vector<int> b;
	int v;
	for ( int i=0; i<n; ++i ) {
	    cin >> v;
	    b.push_back( v );
	}

	int cnt = 0;
	while ( cnt <= 10000 ) {

	    //三角形になっているか
	    bool tri_num = true;
	    if ( b[0] != 1 ) tri_num = false;
	    for ( int i=1; i<b.size(); ++i ) {
		if ( b[i] != b[i-1]+1 ) {
		    tri_num = false;
		    break;
		}
	    }
	    if ( tri_num ) break;

	    vector<int> b_tmp;
	    for ( int i : b ) {
		if ( i > 1 ) b_tmp.push_back( i-1 );
	    }
	    b_tmp.push_back( b.size() );
	    b = b_tmp;;

	    ++cnt;
	}

	if ( cnt > 10000 ) cout << -1 << endl;
	else cout << cnt << endl;

    }

    return 0;
}

