import java.util.Scanner; public class Main_yukicoder178 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] s = new long[n]; long max = 0; boolean flag = true; for (int i = 0; i < n; i++) { int a = sc.nextInt(); int b = sc.nextInt(); s[i] = (long)a + b * 4; max = Math.max(max, s[i]); if (i > 0 && s[i] % 2 != s[i - 1] % 2) { flag = false; } } if (flag) { long ret = 0; for (int i = 0; i < n; i++) { ret += (max - s[i]) / 2; } System.out.println(ret); } else { System.out.println("-1"); } sc.close(); } }