#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
int min_count(std::vector<int> blocks) {
	const auto sum = std::accumulate(blocks.begin(), blocks.end(), 0);
	const auto sqrt = static_cast<int>(std::sqrt(sum * 2));
	if (sqrt * (sqrt + 1) / 2 != sum) return -1;
	for (auto times = 0; times <= 10000; ++times) {
		if (blocks.size() == sqrt) {
			bool is_triangle = true;
			for (auto i = 0; i < blocks.size(); ++i) {
				if (blocks[i] != i + 1) {
					is_triangle = false;
					break;
				}
			}
			if (is_triangle) return times;
		}
		const auto size = blocks.size();
		for (auto& b : blocks) --b;
		blocks.erase(std::remove(blocks.begin(), blocks.end(), 0), blocks.end());
		blocks.push_back(size);
	}
	return -1;
}
int main() {
	while (true) {
		int n; std::cin >> n; if (n == 0) break;
		std::vector<int> blocks(n); for (auto& b : blocks) std::cin >> b;
		std::cout << min_count(blocks) << '\n';
	}
}
