program medic;
type jl=record
t,p:integer;
end;
sz=array[1..100]of jl;
var s:sz;
boo:array[1..100]of boolean;
i,m,t,n:integer;
f:text;
procedure main(j,k,g:word);
var i:integer;
begin
for i:=1 to m do
if not(boo[i]) then
if k>=s[i].t then
begin
boo[i]:=true;
main(i,k-s[i].t,g+s[i].p);
boo[i]:=false;
end else
if g>n then n:=g;
end;
begin
assign(f,'medic.in');
reset(f);
readln(f,t,m);
for i:=1 to m do
readln(f,s[i].t,s[i].p);
close(f);
main(1,t,0);
assign(f,'medic.out');
rewrite(f);
write(f,n);
close(f);
end.