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