program p1133;
var maxv,n,i,j:longint;
v:array[0..30] of longint;
f:array[0..20000] of longint;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
begin
assign(input,'npack.in');
assign(output,'npack.out');
reset(input);
rewrite(output);
readln(maxv);
readln(n);
for i:=1 to n do
readln(v[i]);
fillchar(f,sizeof(f),0);
for i:=1 to n do
for j:=maxv downto v[i] do
if f[j-v[i]]+v[i]<=maxv then f[j]:=max(f[j-v[i]]+v[i],f[j]);
writeln(maxv-f[maxv]);
close(input);
close(output);
end.