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