import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;
static import std.ascii;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    long N = lread();
    auto A = new long[](N);
    foreach (i; 0 .. N)
    {
        A[i] = lread();
    }
    long len;
    long cur;
    long ans;
    if (A[0] != 0)
    {
        writeln(-1);
        return;
    }
    foreach (i; 1 .. N)
    {
        // writefln("%s %s %s", i, cur, A[i]);
        if (A[i] - cur <= 1)
        {
            len++;
            cur = A[i];
            continue;
        }
        if (A[i] == 0)
        {
            ans += len;
            len = 0;
            continue;
        }
        writeln(-1);
        return;
    }
    writeln(ans + len);
}
