#include using namespace std; typedef long long ll; signed main(){ int N; cin >> N; vector< int > A( N ), B( N ); for( int i = 0; i < N; ++i ) cin >> A[ i ] >> B[ i ]; vector< int > val( N ); for( int i = 0; i < N; ++i ) val[ i ] = A[ i ] + 4 * B[ i ]; int maxv = *max_element( val.begin(), val.end() ); int ans = 0; for( int i = 0; i < N; ++i ){ int d = maxv - val[ i ]; if( d & 1 ) cout << -1 << endl, exit( 0 ); ans += d / 2; } cout << ans << endl; return 0; }