using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;

class Program
{

    static void Main(string[] args)
	{
        int N = int.Parse(Console.ReadLine());
        string[] input = Console.ReadLine().Split(' ');

        int[] hight = new int[N];

        int count = 0;

        for (int i = 0; i < N; i++)
        {
            hight[i] = int.Parse(input[i]);

            if (i == 0) continue;

            while(hight[i-1] > hight[i])
            {
                hight[i]++;
                count++;
            }
        }

        Console.WriteLine(count);

    }
}