# グラフ # 頂点0を作成し、全ての赤の頂点に対して長さ0の辺を貼る # 全ての頂点を昇順に並べ、隣合った点に距離を表す辺を貼る # 最小全域木 N,M = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) G = [[] for i in range(N + M + 1)] for i in range(1, N + 1): G[0].append([i, 0]) A = [[A[i], i + 1] for i in range(N)] B = [[B[i], N + i + 1] for i in range(M)] AB = A + B AB = sorted(AB, key = lambda x:x[0]) for i in range(N + M - 1): dist = abs(AB[i + 1][0] - AB[i][0]) to_v = AB[i + 1][1] from_v = AB[i][1] G[to_v].append([from_v, dist]) G[from_v].append([to_v, dist]) import heapq as hq q = [] hq.heappush(q, (0, 0)) visited = [False] * (N + M + 1) INF = 1 << 60 best = [INF] * (N + M + 1) ans = 0 while q: d, v = hq.heappop(q) if visited[v]: continue visited[v] = True ans += d for child, dist in G[v]: if visited[child]: continue if best[child] <= dist: continue best[child] = dist hq.heappush(q, (dist, child)) print(ans)