import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; // SList, DList, BinaryHeap import std.typecons; // Tuple, Nullable, BigFlags import std.math; // math functions import std.numeric; // gcd, fft import std.bigint; // BigInt import std.random; // random import std.bitmanip; // BitArray import core.bitop; // bit operation import std.regex; // RegEx import std.uni; // unicode void main() { auto n = readln.chomp.to!int; return; auto a = new long[](n), b = new long[](n); foreach (i; 0..n) { auto rd = readln.split.to!(long[]); a[i] = rd[0]; b[i] = rd[1]; } if (n == 1) { writeln(min(a[0], b[0])); return; } auto n1 = n/2, n2 = n- n1; long[] s1, s2; foreach (i; 0..1 << n1) { auto sa = 0L, sb = 0L; foreach (j; 0..n1) { if (i.bitTest(j)) sa += a[j]; else sb += b[j]; } s1 ~= sa - sb; } foreach (i; 0..1 << n2) { auto sa = 0L, sb = 0L; foreach (j; 0..n2) { if (i.bitTest(j)) sa += a[n1+j]; else sb += b[n1+j]; } s2 ~= sa - sb; } s2.sort(); auto s2s = s2.assumeSorted; auto r = long.max; foreach (s1i; s1) { auto t0 = s2s.equalRange(-s1i); if (!t0.empty) { writeln(0); return; } auto t1 = s2s.lowerBound(-s1i); if (!t1.empty) r = min(r, (s1i + t1.back).abs); auto t2 = s2s.upperBound(-s1i); if (!t2.empty) r = min(r, (s1i + t2.front).abs); } writeln(r); } pragma(inline) { pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; } pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); } pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); } pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); } }