program suma;
var
sz:array[0..100000]of longint;
i,j,n,k,p,p1,p2,t,min,temp:longint;
begin
assign(input,'suma.in');
assign(output,'suma.out');
reset(input);
rewrite(output);
readln(n,k,p);
min:=2000000000;
sz[0]:=0;
for i:=1 to n do
begin
readln(sz[i]);
if sz[i]<min then min:=sz[i];
sz[i]:=sz[i]+sz[i-1];
end;
if min>k then begin
for i:=1 to n do
for j:=i+1 to n do
if ((sz[j]-sz[i-1])mod p>=k)and((sz[j]-sz[i-1])mod p<min) then begin
min:=(sz[j]-sz[i-1])mod p;
if min=k then break;
end;
end;
writeln(min);
close(input);
close(output);
end.