#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; int A[1005], B[1005]; int main(){ int N; cin >> N; int mx = 0; rep(i,N){ cin >> A[i] >> B[i]; mx = max(mx, A[i]+4*B[i]); } int ret = 0; rep(i,N){ if((mx-(A[i]+4*B[i]))%2==1){ cout << -1 << endl; return 0; } ret += (mx-(A[i]+4*B[i]))/2; } cout << ret << endl; return 0; }