program medic(input,output);
var
i,j,k:longint;
n,t,m:longint;
time:array [1..1000] of longint;
cost:array [1..10000] of longint;
ans:array [1..100000] of longint;
begin
assign(input,'medic.in');
assign(output,'medic.out');
reset(input);
rewrite(output);
readln(t,m);
for i:=1 to m do
begin
readln(cost[i],time[i]);
for j:=t downto time[i] do
if (ans[j-cost[i]]+time[i])>ans[j] then
ans[j]:=ans[j-cost[i]]+time[i];
end;
writeln(ans[t]);
close(input);
close(output);
end.