import java.awt.Point; import java.awt.Rectangle; import java.awt.geom.Rectangle2D; import java.io.BufferedInputStream; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentSkipListSet; public class Main { public static final int C = 1000000007; static final int CY = 1000000000; //static boolean MAP[][]; static int N; static int B; static int[][] POS; static String S; static int X[][]; static int HA[]; static long DP[]; static int ST[][]; static ConcurrentSkipListSet TS; static ArrayList AL; public static void main(String[] args) { StringBuilder sb = new StringBuilder(); BufferedInputStream bs = new BufferedInputStream(System.in); Scanner sc = new Scanner(bs); N = sc.nextInt(); long[] a = new long[N]; long min = Long.MAX_VALUE; for (int i =0; i < N; i++) { a[i] = sc.nextLong()*1 + sc.nextLong()*4; min = Math.min(min, a[i]); } long ans = 0; for (int i=0; i < N; i++) { long num = a[i] - min; if (num %2 != 0) { ans = -1; break; } ans += num/2; } System.out.println(ans); } }