program bing;
var
f1,f2:text;
t,m,v,v0:integer;
a:array[1..100,1..2]of integer;
b:array[1..100] of boolean;
procedure init;
var i,j:integer;
begin
assign(f1,'medic.in');reset(f1);
assign(f2,'medic.out');rewrite(f2);
readln(f1,t,m);
for i:=1 to m do begin
b[i]:=true;
for j:=1to 2 do read(f1,a[i,j]); end;
v:=0;v0:=0;
end;
procedure s(x:integer);
var i:integer;
begin
for i:=1 to m do
if (b[i]=true)and(a[i,1]<=x) then
begin
v:=v+a[i,2]; x:=x-a[i,1];b[i]:=false;
if x=0 then
begin
if v>v0 then v0:=v;end
else s(x);
x:=x+a[i,1];
v:=v-a[i,2];
b[i]:=true;
end
else if v>v0 then v0:=v;
end;
begin
init;
s(t);
write(f2,v0);
close(f1);close(f2);
end.