import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); TreeMap map = new TreeMap<>(); for (int i = 0; i < n; i++) { int value = sc.nextInt() + sc.nextInt() * 4; map.put(value, map.getOrDefault(value, 0) + 1); } long max = map.lastKey(); long ans = 0; for (int x : map.keySet()) { if (max % 2 != x % 2) { System.out.println(-1); return; } ans += (max - x) / 2 * map.get(x); } System.out.println(ans); } }