const
  MaxN = 1000100;
  MaxK = 1000010;

var
  inFile, outFile : text;
  n, k, i, position : longint;
  a, r, sorted : array[0..MaxN] of longint;
  c : array[0..MaxK] of longint;
  sum : int64;


begin
        assign(inFile, 'magacin.in');
        assign(outFile, 'magacin.out');
        reset(inFile); rewrite(outFile);

	readln(inFile, n, k);
        for i := 1 to n do
          read(inFile, a[i]);

	sum := 0;
        for i := 1 to n do begin
          r[i] := a[i] mod k;
          sum := sum + (a[i] div k);
        end;

	// sortiramo niz ostataka counting sortom
	fillchar(c, sizeof(c), 0);

        for i := 1 to n do
          c[ r[i] ] := c[ r[i] ] + 1;
        for i := 1 to k - 1 do
          c[i] := c[i] + c[i - 1];
        for i := n downto 1 do begin
          sorted[ c[ r[i] ] ] := r[i];
          c[ r[i] ] := c[ r[i] ] - 1;
        end;

	position := sum mod n;

	if (position = 0) then
	        writeln(outFile, sorted[n] - sorted[1])
	else
		writeln(outFile, sorted[position] + k - sorted[position + 1]);

	close(inFile);
	close(outFile);
end.
