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