import java.io.*; import java.util.*; import java.math.*; class Main178 { public static void out (Object out) { System.out.println(out); } public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] nums = new int[n]; String[] line = br.readLine().split(" "); int a = Integer.parseInt(line[0]); nums[0] = a + Integer.parseInt(line[1]) * 4; int max = nums[0]; boolean isImpossible = false; for (int i = 1; i < n; i++) { line = br.readLine().split(" "); int a2 = Integer.parseInt(line[0]); if (a % 2 != a2 % 2) { isImpossible = true; break; } nums[i] = a2 + Integer.parseInt(line[1]) * 4; max = Math.max(max , nums[i]); } //out("max = " + max); long ans = 0; for (int m : nums) ans += (max - m); out(isImpossible ? -1 : ans / 2); } }