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