program cojs1786;
type
node=record
p,x:longint;
ot:int64;
end;
var
n,k,t,ji,sum:int64;
i,j,m:longint;
a:array[1..10] of node;
begin
assign(input,'HanXin.in');
assign(output,'HanXin.out');
reset(input);
rewrite(output);
readln(n,m);
ji:=1;
for i:=1 to m do
begin
read(a[i].p,a[i].x);
ji:=ji*a[i].p;
end;
for i:=1 to m do
a[i].ot:=ji div a[i].p;
sum:=0;
for i:=1 to m do
begin
t:=a[i].ot;
while t mod a[i].p<>1 do
begin
inc(t,a[i].ot);
if t mod a[i].p=0 then begin writeln(-1); halt; end;
end;
t:=t*a[i].x;
inc(sum,t);
end;
while sum>n do
dec(sum,ji);
while sum<n do
inc(sum,ji);
dec(sum,ji);
if sum<0 then begin writeln(-1);halt; end;
writeln(n-sum);
close(input);
close(output);
end.