import itertools as iter import collections as coll import heapq as hq import bisect as bis from decimal import Decimal as dec from copy import deepcopy as dcopy import math import sys sys.setrecursionlimit(10**6) def input(): return sys.stdin.readline().rstrip() def getN(): return int(sys.stdin.readline()) def getNs(): return map(int,sys.stdin.readline().split()) def getList(): return list(map(int,sys.stdin.readline().split())) def strinps(n): return [sys.stdin.readline().rstrip() for _ in range(n)] pi = 3.141592653589793 mod = 10**9+7 MOD = 998244353 INF = math.inf dx = [1,0,-1,0]; dy = [0,1,0,-1] """ Union-Find from : https://github.com/customaddone/beginPython/blob/master/cgi-bin/library/unionfind/unionfind.py ref : https://algo-logic.info/union-find-tree/ """ class UnionFind(): #Uni = UnionFind(n) のようにする def __init__(self, n): self.n = n self.parents = [-1] * n #xの親(親がいないときは自身の番号を返す) def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] #xとyを関係付ける def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x # if x > y: # よりrootのインデックスが小さい方が親 # x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x #xとyが同じ組に属するかどうか def same(self, x, y): return self.find(x) == self.find(y) #xが属する組の大きさ def size(self, x): return -self.parents[self.find(x)] #xが属する組の全要素をリストとして取得 def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] #Union-Find木の根に当たる要素全てをリストとして取得 def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] #Union-Find木の根とそれに属する組の全要素 def all_group_members(self): return {r: self.members(r) for r in self.roots()} """ Main Code """ n,m = getNs() a = [i - 1 for i in getNs()] b = [i - 1 for i in getNs()] rb = max(a[-1],b[-1]) + 1 uni = UnionFind(n + m + 1) route = [] x = sorted(a + b) st = set(a) for i in range(n + m): if(x[i] in a): route.append((0, i, n + m)) for i in range(n + m - 1): route.append((x[i + 1] - x[i], i, i + 1)) route.sort() ans = 0 for c,s,t in route: if(uni.size(0) == n + m + 1): break if(uni.same(s,t)): continue uni.union(s,t) ans += c print(ans)