program xb;
var
f1,f2:text;
f,v,w:array[1..1000] of longint;
n,k,i,max:integer;
procedure nb;
var i,j:integer;
begin
fillchar(f,sizeof(f),0);
for i:=1 to k do
for j:=n downto v[i] do
if f[j-v[i]]+w[i]>f[j] then f[j]:=f[j-v[i]]+w[i];
end;
begin
assign(f1,'medic.in');reset(F1);
assign(F2,'medic.out');rewrite(f2);
read(f1,n,k);
for i:=1 to k do read(f1,v[i],w[i]);
nb;
for i:=1 to n do if f[i]>max then max:=f[n];
write(F2,max);
close(f1);close(F2);
end.