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