long s,m;f,x,i;main(p){for(gets(&p);~scanf("%d%d",&x,&p);x>0?s+=x*-~i,m+=x:0,f+=x&i>0,i++)x+=p*4,s-=x-=m;s=!printf("%ld",f?-1:s/2);}