#include using namespace std; #define LL long long #define REP(i, n) for(int i = 0; i < n; i++) #define ALL(a) (a).begin(),(a).end() int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector ws(n); REP(i, n) { LL a, b; cin >> a >> b; ws[i] = a + 4 * b; } LL wsMax = *std::max_element(ALL(ws)); LL ans = 0; REP(i, n) { if ((wsMax - ws[i]) % 2 != 0) { cout << -1 << endl; return 0; } else { ans += (wsMax - ws[i]) / 2; } } cout << ans << endl; return 0; }