#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define minimize(a, x) a = std::min(a, x) #define maximize(a, x) a = std::max(a, x) typedef long long ll; int const inf = 1<<29; int main() { int N; cin >> N; int max = 0; int as[N], bs[N]; rep(i, N) { int a, b; cin >> a >> b; as[i]=a,bs[i]=b; maximize(max, a + 4 * b); } ll ans = 0; rep(i, N) { if((max-as[i]-4*bs[i]) % 2) { puts("-1"); exit(0); } ans += (max-as[i]-4*bs[i]) / 2; } cout << ans << endl; return 0; }