var m,t,i,j,k,l,tot:longint;
n:int64;
ans,temp,fin,sum:int64;
p:array[0..100] of longint;
a:array[0..100] of longint;
f:array[0..100] of int64;
begin
assign(input,'HanXin.in');
assign(output,'HanXin.out');
reset(input);
rewrite(output);
ans:=1;
readln(n,m);
sum:=0;
for i:=1 to m do
begin
readln(p[i],a[i]);
ans:=ans*p[i];
end;
for i:=1 to m do
begin
tot:=1;
temp:=ans div p[i];
j:=1;
while true do
begin
if (j>p[i]) then begin
writeln('-1');
close(input);
close(output);
halt;
end;
if (((temp*j) mod p[i])=0) then
begin
writeln('-1');
close(input);
close(output);
halt; end;
if (((temp*j) mod p[i])=1)
then begin
inc(sum,temp*j*a[i]);
break;
end;
inc(j);
end;
end;
if (sum>n) then begin
writeln('-1');
end
else begin
while (sum<=n) do
begin
inc(sum,ans);
end;
dec(sum,ans);
writeln(n-sum);
end;
close(input);
close(output);
end.