#include using namespace std; #define rep(i,n) for(int i=0;i> n >> m; vector a(n), b(m); rep(i,n)cin >> a[i]; rep(i,m)cin >> b[i]; vector> x; for (int i = 0, j = 0; i < n || j < m;) { if (j == m || (i < n && a[i] < b[j])) { x.emplace_back(a[i++], true); } else { x.emplace_back(b[j++], false); } } int ans = x.back().first - x.front().first; int pre = -1, mac = 0; rep(i,n + m) { if (i) { mac = max(mac, x[i].first - x[i - 1].first); } if (x[i].second) { if (pre != -1)ans -= mac; pre = i, mac = 0; } } cout << ans << "\n"; return 0; }