long long n,m;a[1001],s,x;main(p){for(gets(p);~scanf("%d%d",&x,&p);m=x>m?x:m)a[s++]=x=x+p*4;for(;s&&~n%2;n+=m-a[--s]);s=!printf("%lld",n&1?-1:n/2);}