using System; using System.Collections.Generic; using System.Linq; namespace SortItems { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var list = new List() ; var max = 0; var ans = 0; for (var i = 0; i < n; i++) { var x = Console.ReadLine().Split().Select(int.Parse).ToArray(); list.Add(x[0] + x[1] * 4); } max = list.Max(); list.Reverse(); for (var i = 0; i < n; i++) { if ((max - list[i]) % 2 != 0) { Console.WriteLine(-1); return; } else { ans += (max - list[i]) / 2; } } Console.WriteLine(ans); } } }