#include #include using namespace std; int t[1000]; int main() { int n, a, b, f, m = 0; long long cnt = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; t[i] = a + 4 * b; if (i == 0) f = t[i] % 2; if (f != t[i] % 2) { cout << -1 << endl; return 0; } m = max(t[i], m); } for (int i = 0; i < n; i++) { cnt += m - t[i]; } cout << cnt / 2 << endl; return 0; }