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