local ffi = require 'ffi' local C, ct = ffi.C, ffi.typeof("int32_t[1]") ffi.cdef 'int scanf(const char *, ...);' print((function (n) local a,p,q,m,t,r={},ct(),ct(),0,0,0 C.scanf("%d %d",p,q) t=p[0]+4*q[0] a[1],m=t,t for i=2,n do C.scanf("%d %d",p,q) t=p[0]+4*q[0] a[i]=t if t>m then m=t end end for i=1,#a do t=m-a[i] if t%2~=0 then return -1 end r=r+t/2 end return r end)(io.stdin:read("*n")))