w=gets.to_i.times.map{a,b=gets.split.map(&:to_i);a+4*b} r=w.first%2 ans=-1 if w.all?{|v|v%2==r} max=w.max ans=w.map{|v|max-v}.reduce(:+)/2 end p ans