#include using namespace std; const int N = 2e5 + 5; int n, y, x, a[N], now, m, sz[N], ans = 1e9, it[N]; vectore[N], g[N]; int main(){ cin >> n; for(int i = 1; i <= n; ++i){ cin >> a[i] >> y; g[y].push_back(a[i]); } cin >> m; for(int i = 1; i <= m; i++){ cin >> x >> y; e[x].push_back(y); } for(int i = 0; i < 1000; ++i){ g[i].push_back(-1); sort(g[i].begin(), g[i].end()); it[i] = 0; } for(int i = 0; i < 1000; ++i){ for(auto v : e[i]){ for(int j = 0; j < 1000; ++j){ for(; it[j] < g[j].size() && g[j][it[j]] < i; it[j]++){ } if(it[j] != g[j].size()){ ans = min(ans, g[j][it[j]] - i + abs(v - j)); } if(it[j] > 1){ ans = min(ans, i - g[j][it[j] - 1] + abs(v - j)); } } } } cout << ans; return 0; }