package yukicoder; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Yuki178 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int a[] = new int[n]; int b[] = new int[n]; boolean a_flg = false; boolean b_flg = false; ArrayList array = new ArrayList(); for (int i = 0; i < n ; i++ ) { a[i] = scan.nextInt(); b[i] = scan.nextInt(); } scan.close(); int ans = 0; for (int i = 0; i < n ; i++ ) { ans = a[i] + 4 * b[i]; if(ans != 0) { if(ans % 2 == 0 ){ a_flg = true; }else if(ans % 3 == 0 ){ b_flg = true; } } array.add(ans); } Collections.sort(array); int max = Collections.max(array); ans = 0; for(Integer num : array){ ans += ((max - num) / 2); } if( a_flg == b_flg == true){ System.out.println(-1); }else { System.out.println(ans); } } }