import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; int max = 0; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt() + sc.nextInt() * 4; max = Math.max(max, arr[i]); } long count = 0; for (int x : arr) { if (x % 2 != max % 2) { System.out.println(-1); return; } count += (max - x) / 2; } System.out.println(count); } }