#include using namespace std; using ll = long long; using P = pair; #define rep(i, a, b) for(ll i = a; i < b; ++i) #define rrep(i, a, b) for(ll i = a; i >= b; --i) constexpr ll inf = 4e18; struct SetupIO { SetupIO() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(30); } } setup_io; template struct Edge { int from, to; T cost; int idx; Edge() : from(-1), to(-1), cost(-1), idx(-1) {} Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template struct Graph { Graph(int N) : n(N), es(0), g(N) {} int size() const { return n; } int edge_size() const { return es; } void add_edge(int from, int to, T cost = 1) { assert(0 <= from and from < n); assert(0 <= to and to < n); g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void add_directed_edge(int from, int to, T cost = 1) { assert(0 <= from and from < n); assert(0 <= to and to < n); g[from].emplace_back(from, to, cost, es++); } inline vector>& operator[](const int& k) { assert(0 <= k and k < n); return g[k]; } inline const vector>& operator[](const int& k) const { assert(0 <= k and k < n); return g[k]; } private: int n, es; vector>> g; }; template using Edges = vector>; struct UnionFind { UnionFind(int N) : n(N), data(N, -1) {} int merge(int a, int b) { assert(0 <= a and a < n); assert(0 <= b and b < n); int x = leader(a), y = leader(b); if(x == y) return x; if(-data[x] < -data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return x; } bool same(int a, int b) { assert(0 <= a and a < n); assert(0 <= b and b < n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a and a < n); if(data[a] < 0) return a; return data[a] = leader(data[a]); } int size(int a) { assert(0 <= a and a < n); return -data[leader(a)]; } vector> groups() { vector leader_buf(n), group_size(n); for(int i = 0; i < n; ++i) { leader_buf[i] = leader(i); ++group_size[leader_buf[i]]; } vector> result(n); for(int i = 0; i < n; ++i) { result[i].reserve(group_size[i]); } for(int i = 0; i < n; ++i) { result[leader_buf[i]].push_back(i); } result.erase(remove_if(result.begin(), result.end(), [&](const vector& v) { return v.empty(); }), result.end()); return result; } private: int n; vector data; }; template pair> kruskal(int n, Edges es) { sort(es.begin(), es.end(), [&](const Edge& a, const Edge& b) { return a.cost < b.cost; }); UnionFind uf(n); T cost = 0; Edges res; res.reserve(n - 1); for(const auto& e : es) { if(uf.same(e.from, e.to)) continue; cost += e.cost; uf.merge(e.from, e.to); res.emplace_back(e); } return {cost, res}; } int main(void) { int n, m; cin >> n >> m; vector a(n), b(m); Edges e; rep(i, 0, n) { cin >> a[i]; e.push_back({(int)i, n + m, 0}); } rep(i, 0, m) { cin >> b[i]; if(i > 0) e.push_back({n + (int)i, n + (int)i - 1, b[i] - b[i - 1]}); int idx = lower_bound(a.begin(), a.end(), b[i]) - a.begin(); if(idx < n) e.push_back({n + (int)i, idx, a[idx] - b[i]}); if(idx > 0) e.push_back({n + (int)i, idx - 1, b[i] - a[idx - 1]}); } cout << kruskal(n + m + 1, e).first << '\n'; }