const
	MaxN = 1000100;

var
	inFile, outFile : text;
	n, k, x, pileNum, currIndex, nextIndex, i : longint;
	sol : int64;
	num, left, right : array[0..MaxN] of longint;

BEGIN
	
	assign(inFile, 'zetoni.in');
	assign(outFile, 'zetoni.out');
	reset(inFile); rewrite(outFile);

	read(inFile, n, k);
	for i := 1 to n do begin
		read(inFile, num[i]);
		left[i] := i - 1;
		right[i] := i + 1;
	end;
	
	num[0] := k + 1; left[0] := 0; right[0] := 1;
	num[n + 1] := k + 1; left[n + 1] := n; right[n + 1] := n + 1;

	sol := -1;
	currIndex := 1;
	pileNum := n + 2;
	while (pileNum > 2) do begin
	
		if (num[ left[currIndex] ] <= num[ right[currIndex] ]) then
			nextIndex := left[currIndex]
		else
			nextIndex := right[currIndex];

		if (num[nextIndex] >= num[currIndex]) then begin
		
			sol := sol + (num[nextIndex] - num[currIndex]);
			pileNum := pileNum - 1;
			right[ left[currIndex] ] := right[currIndex];
			left[ right[currIndex] ] := left[currIndex];

		end;

		currIndex := nextIndex;
		
	end;

	writeln(outFile, sol);
	close(inFile);
	close(outFile);
	
END.
