program suma;
var n,k,p,i,j,m,s:longint;
A:array of longint;
begin
assign(input,'suma.in'); reset(input);
assign(output,'suma.out'); rewrite(output);
readln(n,k,p);
SetLength(A,n+1);
for i:=1 to n do readln(A[i]);
for i:=1 to n do A[i]:=(A[i]+A[i-1]);
m:=p;
for i:=1 to n do
for j:=1 to n do
begin
s:=(abs(A[i]-A[j]))mod p;
if (s>=k)and(s<m) then m:=s;
end;
writeln(m);
close(input); close(output);
end.