program ti;
var
a: array[0..1000] of longint;
t,m,i,j,x,y : longint;
begin
assign(input,'medic.in'); reset(input);
assign(output,'medic.out'); rewrite(output);
readln(t,m);
fillchar(a,sizeof(a),0);
for i := 1 to m do
begin
readln(x,y);
for j := t-x downto 0 do
if a[j]+y > a[j+x] then a[j+x] := a[j]+y;
end;
writeln(a[t]);
close(input); close(output);
end.