import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range;
class EOFException : Throwable { this() { super("EOF"); } }
class Reader {string[] t;int p;
  void next() {++p;while(p>=t.length) {if(stdin.eof) throw new EOFException; t=readln.split;p=0;}}
  string String() { next; return t[p]; }
  int Int() { next; return to!int(t[p]); }
  long Long() { next; return to!long(t[p]); }
  real Real() { next; return to!real(t[p]); }
};
Reader reader;

void main() {
  reader = new Reader;
	try {
    while(1) {
      int N = reader.Int;
      if (!N) break;
      int[] b = readln.split.map!(to!int).array;
      int ans = -1;
      foreach(iter; 0..10001) {
        bool ok = 1;
        foreach(i; 0..b.length) {
          if (b[i] != i+1) {
            ok = 0;
            break;
          }
        }
        if (ok) {
          ans = iter;
          break;
        }
        int[] c;
        foreach(ref x; b) {
          x--;
          if (x) c ~= x;
        }
        c ~= b.length;
        b = c;
      }
      writeln(ans);
    }
	} catch (EOFException) {}
}