import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;

void main() {
    auto N = readln.chomp.to!int;
    auto goal = readln.split.to!(int[]); 

    auto current = new int[N];

    int cnt = 0;
    while (true) {
        if (current == goal) break;
        auto idxList = zip(current,goal).enumerate.filter!(a => a.value[0] != a.value[1]).map!(a => a.index);

        size_t before = size_t.max-1;
        foreach (idx; idxList) {
            if (idx != before+1) cnt++;
            current[idx]++;
            before = idx;
        }
    }
    writeln(cnt);
}

