import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		int[] A = new int[N];
		int ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Integer.parseInt(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				for(int j=0; j<N; j++) {
					if(A[i-1] != A[i]) {
						A[i]++;
						ans++;
					} else
						break;
				}
			}
		}
		
		System.out.println(ans);
	}

}