var i,j,k:longint;
z,s:array[1..10]of int64;
he:int64;
t,p,q,r,l,ss,c,m,n:int64;sum:array[0..20]of int64;
procedure work;
begin
readln(n,m);
for i:=1 to m do begin
readln(z[i],s[i]);
end;
ss:=1;he:=0;
for i:=1 to m do begin ss:=ss*z[i];end;
for i:=1 to m do begin
sum[i]:=ss div z[i];q:=sum[i];
while sum[i] mod z[i]<>1 do sum[i]:=sum[i]+q;
end;
for i:=1 to m do begin
he:=he+sum[i]*s[i];
end;
he:=he mod ss;
if he>n then begin writeln('-1');exit;end;
while he<=n do begin he:=he+ss;end;
he:=he-ss;
he:=n-he;
writeln(he);
end;
begin
assign(input,'HanXin.in');
assign(output,'HanXin.out');
reset(input);
rewrite(output);
work;
close(input);
close(output);
end.