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