program sum;
var a:array[0..100000]of int64;
t,k,m,n,i,j,p,min:longint;
begin
assign(input,'suma.in');
reset(input);
assign(output,'suma.out');
rewrite(output);
readln(n,k,p);
min:=maxlongint;
for t:=1 to n do
begin
read(i);
a[t]:=(a[t-1]+i)mod p;
end;
for i:=1 to n do
for j:=i to n do
if (a[j]-a[i-1])mod p>=k then
if (a[j]-a[i-1])mod p<min then
begin
min:=a[j]-a[i-1];
if min=0 then
begin
writeln(0);
close(output);
halt;
end;
end;
writeln(min);
close(output);
end.