#coding: utf-8 ##yuki_178 import sys st_max=0 ws=[] wsc=0 n=int(raw_input()) for i in xrange(n): a,b=map(int,raw_input().split()) st=a+b*4 if st>st_max: st_max=st ws.append(st) for i in ws: if (st_max-i)%2==1: print -1 sys.exit() else: wsc+=(st_max-i)/2 print wsc