#include using namespace std; int n, now = 0, m, e[100001][2] = { 0 }, g[100001] = { 0 }, p[100001][4] = { 0 }; int mins(int a, int b) { return a < b ? a : b; } int maxs(int a, int b) { return a > b ? a : b; } void party(int a, int b) { // cout << a << " " << b << ":"; if (g[a] == 0 && g[b] == 0) { g[a] = ++now; g[b] = now; int a0 = e[a][0], b0 = e[b][0], a1 = e[a][1], b1 = e[b][1]; p[now][0] = mins(a0, b0); p[now][1] = mins(a1, b1); p[now][2] = maxs(a0, b0); p[now][3] = maxs(a1, b1); } else { while (p[g[a]][0] == -1) { a = p[g[a]][3]; } while (p[g[b]][0] == -1) { b = p[g[b]][3]; } if (g[a] > g[b]) { int o = a; a = b; b = o; } /*cout << g[a] << "?"; cout << g[b] << "?" << endl;*/ if (g[a] == 0) { g[a] = g[b]; int a0 = e[a][0], a1 = e[a][1]; p[g[b]][0] = mins(a0, p[g[b]][0]); p[g[b]][1] = mins(a1, p[g[b]][1]); p[g[b]][2] = maxs(a0, p[g[b]][2]); p[g[b]][3] = maxs(a1, p[g[b]][3]); } else { if (g[a] != g[b]) { p[g[b]][0] = mins(p[g[a]][0], p[g[b]][0]); p[g[b]][1] = mins(p[g[a]][1], p[g[b]][1]); p[g[b]][2] = maxs(p[g[a]][2], p[g[b]][2]); p[g[b]][3] = maxs(p[g[a]][3], p[g[b]][3]); p[g[a]][0] = -1, p[g[a]][3] = b; } } }/* for (int i = 0; i <= 3; i++) { cout << p[g[a]][i] << " "; } cout << endl; for (int i = 0; i <= 3; i++) { cout << p[g[b]][i] << " "; } cout << endl;*/ } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> e[i][0] >> e[i][1]; } for (int i = 1; i <= m; i++) { int yi, er; cin >> yi >> er; //cout << i << "||"; party(yi, er); } int ans = 400000000; for (int i = 1; i <= now; i++) { if (p[i][0] != -1) { int zhou = p[i][2] - p[i][0] + p[i][3] - p[i][1]; ans = mins(zhou * 2, ans); } } cout << ans; max() }