import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;
 
void main() {
    int N;
    int[] B;
    bool input() {
        scanf("%d\n", &N);
        if (N == 0) return false;
        B = readln.chomp.split(" ").map!(to!int).array;
        return true;
    }
    
    int[] step(int[] xs) {
        int p = cast(int)xs.length;
        foreach (ref x; xs) {
            x--;
        }
        int[] ys;
        foreach (x; xs) {
            if (x == 0) continue;
            ys ~= x;
        }
        ys ~= p;
        return ys;
    }

    bool check(int[] xs) {
        foreach (i; 0 .. xs.length) {
            if (xs[i] != i + 1) return false;
        }
        return true;
    }

    void solve() {
        int[] X = B.dup;
        foreach (i; 0 .. 10000 + 1) {
            if (check(X)) {
                writeln(i);
                return;
            }
            X = step(X);
        }
        writeln(-1);
    }

    while (input) solve;

}