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()); long[] sum = new long[n]; long max = 0; for(int i = 0; i < n; i++){ String[] str = br.readLine().split(" "); sum[i] = Long.parseLong(str[0]); sum[i] += 4*Long.parseLong(str[1]); max = Math.max(max,sum[i]); } long cnt = 0; for(int i = 0; i < n; i++){ long 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); } }