#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const ll INF=1e9;
int main(){
      int n;
      while(cin>>n&&n){
            int cnt=0,a[100000],h=n;
            REP(i,n)cin>>a[i];
            int k=1;bool f=true;
            while(cnt<=10000){
                  bool f=true;
                  REP(i,h){
                        if(a[i]!=i+1)f=false;
                  }
                  if(f)break;
                  int now=0;
                  REP(i,h)a[i]--;
                  REP(i,h){
                        if(a[i]>0){
                              a[now]=a[i];
                              now++;
                        }
                  }
                  a[now]=h;
                  h=now+1;
                  cnt++;
            }
            if(cnt>10000)cout<<-1<<endl;
            else cout<<cnt<<endl;
      }
}