import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] a = new int[N]; int[] b = new int[N]; for (int i = 0; i < N; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); } sc.close(); int max = 0; for (int i = 0; i < N; i++) { max = Math.max(a[i] + 4 * b[i], max); } long c = 0; for (int i = 0; i < N; i++) { if ((max - a[i] - 4 * b[i]) % 2 == 0) { c += (max - a[i] - 4 * b[i]) / 2; } else { System.out.println(-1); return; } } System.out.println(c); } }