import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.math.*; public class Main { public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int n = Integer.parseInt(br.readLine()); int[] sum = new int[n]; int max = 0; for(int i = 0; i < n; i++){ String[] str = br.readLine().split(" "); sum[i] = Integer.parseInt(str[0]); sum[i] += 4*Integer.parseInt(str[1]); max = Math.max(max,sum[i]); } int cnt = 0; for(int i = 0; i < n; i++){ int k = max - sum[i]; if(k%2!=0){ sb.append(-1); System.out.println(sb); return; } cnt += k/2; } sb.append(cnt); System.out.println(sb); } }