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