var
n,k,p,j,i,head,tail,r,ans:longint;
s:int64;
a:array[0..100001]of longint;
function min(x,y:int64):int64;
begin
if x>y then min:=y else min:=x;
end;
begin
assign(input,'suma.in');reset(input);
assign(output,'suma.out');rewrite(output);
readln(n,k,p);
for i:=1 to n do
begin
readln(r);
a[i]:=r mod p;
end;
s:=0;head:=1;tail:=0;
ans:=maxlongint;
for i:=1 to n do
begin
s:=0;
for j:=i to n do
begin
s:=s+a[j];
if s mod p>=k then ans:=min(ans,s mod p);
if ans=k then break;
end;
if ans=k then break;
end;
writeln(ans);
close(input);close(output);
end.