program tz;
type score=array[1..100,1..2] of integer;
var a:score;p,s,t,m,i,j:integer;biaozhi:boolean;f1,f2:text;
begin
assign(f1,'medic.in');
assign(f2,'medic.out');
reset(f1);rewrite(f2);
readln(f1,t,m);
for i:=1 to m do
begin
for j:=1 to 2 do
read(f1,a[i,j]);
end;
s:=0;p:=0;
for i:=1 to m do
begin
if a[i,1]<t then p:=p+a[i,1];
if (p<>0)and(p<=t) then s:=s+a[i,2];
end;
writeln(f2,s);
close(f1);
close(f2);
end.