import std.stdio;

immutable int NA = -1;

long solve (int n)
{
	auto a = new int [n];
	foreach (ref c; a)
	{
		readf (" %s", &c);
	}
	a ~= NA;
	if (a[0] > 0)
	{
		return NA;
	}
	long res = 0;
	foreach_reverse (i; 0..n - 1)
	{
		if (a[i + 1] > a[i] + 1)
		{
			return NA;
		}
		else if (a[i + 2] != a[i + 1] + 1)
		{
			res += a[i + 1];
		}
	}
	return res;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		writeln (solve (n));
	}
}
