import java.util.Scanner; public class No178 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int max = 0, N, ans = 0; N = Integer.parseInt(sc.next()); int[] a = new int[N]; int[] b = new int[N]; for (int i = 0; i < N; i++) { a[i] = Integer.parseInt(sc.next()); b[i] = Integer.parseInt(sc.next()); max = Math.max(max, a[i] + 4 * b[i]); } for (int i = 0; i < N; i++) { int po = max - (a[i] + 4 * b[i]); if(po % 2 == 1){ System.out.println(-1); return; } ans += po / 2; } System.out.println(ans); } }