#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

void solve(int n){
    vector<int> v(n);
    for (auto &&i : v) scanf("%d", &i);
    for (int i = 0; i <= 10000; ++i) {
        int ok = v[0] == 1;
        for (int j = 0; j+1 < v.size(); ++j) {
            ok &= (v[j+1]-v[j] == 1);
        }
        if(ok) {
            cout << i << "\n";
            return;
        }
        for (auto &&j : v) {
            j--;
        }
        int sz = v.size();
        v.erase(remove(v.begin(),v.end(), 0), v.end());
        v.emplace_back(sz);
    }
    puts("-1");
}

int main() {
    int n;
    while(cin >> n, n){
        solve(n);
    }
    return 0;
}
