using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Magatro { static void Main() { long ans = 0; int N = int.Parse(Console.ReadLine()); long[] a = new long[N]; for(int i = 0; i < N; i++) { string[] s = Console.ReadLine().Split(' '); int w = int.Parse(s[0]); w += int.Parse(s[1]) * 4; a[i] = w; } long max = a.Max(); for(int i = 0; i < N; i++) { long p = (max - a[i]); if (p % 2 == 1) { Console.WriteLine(-1); return; } else { ans += p / 2; } } Console.WriteLine(ans); } }