// #include // Temp fix for gcc13 global pragma // #pragma GCC target("avx2,bmi2,popcnt,lzcnt") // #pragma GCC optimize("O3,unroll-loops") #include // #include using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif #ifdef LOCAL #include "Debug.h" #else #define debug_endl() 42 #define debug(...) 42 #define debug2(...) 42 #define debugbin(...) 42 #endif int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int n; cin >> n; vector> a(n); for(auto &[x, y]: a){ cin >> x >> y; } int m; cin >> m; vector> b(m); for(auto &[x, y]: b){ cin >> x >> y; } int res = numeric_limits::max(); for(auto rep = 2; rep; -- rep){ for(auto rep = 2; rep; -- rep){ vector>> event(2001); debug(a); debug(b); for(auto [x, y]: a){ event[x].push_back({0, y}); } for(auto [x, y]: b){ event[x].push_back({1, y}); } vector opt_dif(2001, numeric_limits::min() / 2); for(auto dif = 0; dif <= 2000; ++ dif){ ranges::sort(event[dif] | ranges::views::reverse); for(auto [type, sum]: event[dif]){ if(type == 0){ for(auto psum = 0; psum <= 2000; ++ psum){ res = min(res, abs(sum - psum) + dif - opt_dif[psum]); } } else{ opt_dif[sum] = max(opt_dif[sum], dif); } } } swap(a, b); } for(auto &[x, y]: a){ x = 1000 - x; } for(auto &[x, y]: b){ x = 1000 - x; } } cout << res << "\n"; return 0; } /* */