PROGRAM medic(input,output,f1,f2);
type
fu=array [1..1000] of longint;
var
f1,f2:text;
m,t:longint;
a,b,c:fu;
time,money,i:longint;
procedure fuqiang(a,b:fu);
begin
if a[i]>t then exit;
if a[i]<=t then
begin
time:=a[i]+time;
if time>t then exit;
if time<=t then
money:=money+b[i];
end;
end;
begin
assign(f1,'medic.in');
assign(f2,'medic.out');
reset(f1);
rewrite(f2);
readln(f1,t,m);
time:=0;
money:=0;
for i:=1 to m do begin
read(f1,a[i]);
read(f1,b[i]);
fuqiang(a,b);
end;
writeln(f2,money);
close(f1);
close(f2);
end.