import java.util.*; public class Exercise103{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] sum = new long[n]; long max = 0; for(int i = 0; i < n; i++){ long a = sc.nextLong(); long b = sc.nextLong() * 4; sum[i] = a + b; max = Math.max(max, a + b); } long 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); } }