#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second template inline bool chmax(A &a, B b) { if (a inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } typedef long long ll; typedef pair pii; typedef pair pll; typedef pair pip; const ll INF = 1ll<<29; const ll MOD = 1000000007; const double EPS = 1e-9; int n; ll a[32], b[32]; int main() { cin >> n; REP(i, n) scanf("%lld %lld", a + i, b + i); vector v; REP(i, 1 << (n/2)) { ll suma = 0, sumb = 0; REP(j, n / 2) { if (i >> j & 1) suma += a[j]; else sumb += b[j]; } v.push_back(suma - sumb); } sort(ALL(v)); ll ans = 1ll<<60; REP(i, 1<<(n-n/2)) { ll suma = 0, sumb = 0; REP(j, n-n/2) { if (i >> j & 1) suma += a[j + n/2]; else sumb += b[j + n/2]; } ll dif = suma - sumb; vector::iterator it = lower_bound(ALL(v), -dif); if (it != v.begin()) --it; REP(i, 3) { if (it == v.end()) break; chmin(ans, abs(dif + *it)); } } cout << ans << endl; return 0; }