var a,b:array[0..1010]of longint;
f:array[0..1010,0..1010]of boolean;
n,i,j,min:longint;
procedure init;
begin
readln(n); min:=1000;
for i:=1 to n do read(a[i],b[i]);
a[n+1]:=1000000; a[0]:=a[n+1];
end;
procedure try(x,y:longint);
var i,t,l,r:longint;
begin
if f[x,y] then exit else f[x,y]:=true;
if x=n+1 then if y<min then min:=y;
if (x>n)or(y>min) then exit;
if x-5<0 then l:=0 else l:=x-5;
if x+5>n+1 then r:=n+1 else r:=x+5;
for i:=r downto l do
begin
if b[i]+a[i]=0 then continue;
t:=(y+1) mod (b[i]+a[i]);
if t=0 then t:=a[i]+b[i];
if t<=a[i] then try(i,y+1);
end;
end;
procedure work;
begin
try(0,0);
for i:=1 to 1001 do if f[n+1,i] then break;
if f[n+1,i] then writeln(i) else writeln('NO');
end;
begin
assign(input,'rivera.in');reset(input);
assign(output,'rivera.out');rewrite(output);
init;
work;
close(input);close(output);
end.