"use strict";
var global_context = global ? global : window;

(function DefineUnderScore() {
    var ArrayProto = Array.prototype;
    var slice = ArrayProto.slice;
    this._ = {};
    _.each = function( obj, iterator, context ) {
        Object.keys(obj).forEach(function(key) {
            if ( iterator.call( context, obj[key], key, obj ) === true )
                return true;
        });
    };
    _.extend = function(obj) {
        _.each(slice.call(arguments, 1), function(source) {
            Object.keys(source).forEach(function(key) {
                obj[key] = source[key];
            });
        });
    };
}).call(global_context);

function Deferred() {
    var queue = [];
    var obj = {
        done: function(func, context) {
            if ( func instanceof Function )
                queue.push({
                    callback: func,
                    context: context
                });
            return this;
        },
        call: function() {
            var args = arguments;
            setTimeout(function() {
                queue.forEach(function(obj) {
                    obj.callback.apply(obj.context, args);
                });
            }, 0);
        }
    };
    return obj;
}

function RawInput() {
    var deferred = new Deferred();
    var stdin = process.stdin;
    stdin.resume();
    stdin.setEncoding('ascii');
    var buffer = '';
    stdin.on('data', function(chunk) {
        buffer += chunk.toString();
    });
    stdin.on('end', function() {
        deferred.call(buffer);
    });
    return deferred;
}

(function DefineSolution( namespace ) {
    var Solution = function() {
        this.input_list = [];
    };
    Solution.prototype = {
        input: function() {
            if ( this.separator instanceof Function )
                this.input_list = this.separator.call( this, this.raw_data );
            else
                this.input_list.push(this.raw_data);
        },
        run: function(raw_data) {
            this.raw_data = raw_data;
            this.input();
            while ( this.has_next() )
                this.solve(this.next()).output();
        },
        read_line: function() {
            return this.input_list.splice(0,1)[0];
        },
        read_number: function() {
            var line = this.read_line();
            return parseInt(line);
        },
        read_numbers: function() {
            var line = this.read_line();
            var terms = line.split(' ');
            return terms.map(function(term) {
                return parseInt(term);
            });
        },
        check_eof: function() {
            return ! this.input_list.length;
        },
        has_next: function() {
            return ! this.check_eof();
        }
    };
    this[namespace] = Solution;
}).call(global_context, 'Solution' );

(function ExtendSolution( namespace ) {
    var NONE = -1;
    var n, A;

    function check_goal() {
        for ( var i = 0; i < n; ++ i ) {
            if ( i + 1 !== A[i] )
                return false;
        }
        return true;
    }

    function move() {
        A = A.map(function(a) {
            return a - 1;
        }).filter(function(a) {
            return a > 0;
        });
        A.push(n);
        n = A.length;
    }

    _.extend(Solution.prototype, {
        separator: function(data) {
            var res = data.split('\n');
            return res;
        },
        check_eof: function() {
            return parseInt(this.input_list[0]) === 0;
        },
        next: function() {
            var n = this.read_number();
            var A = this.read_numbers();
            return {
                n: n,
                A: A
            };
        },
        solve: function(options) {
            n = options.n;
            A = options.A;
            for ( var i = 0; i < 10000; ++ i ) {
                if ( check_goal() ) {
                    this.result = i;
                    return this;
                }
                move();
            }
            this.result = NONE;
            return this;
        },
        output: function() {
            console.log(this.result);
        }
    });
})();

var solution = new Solution;
RawInput.call(global_context).done(solution.run, solution);

Measured memory_bytes: 48844