local mmi, mma = math.min, math.max local mfl = math.floor local function comp(a, b) return a < b end local function lower_bound(ary, x) local num = #ary if num == 0 then return 1 end if not comp(ary[1], x) then return 1 end if comp(ary[num], x) then return num + 1 end local min, max = 1, num while 1 < max - min do local mid = mfl((min + max) / 2) if comp(ary[mid], x) then min = mid else max = mid end end return max end local n, m = io.read("*n", "*n") local a = {} for i = 1, n do a[i] = io.read("*n") end local t = {} for i = 1, n - 1 do t[i] = {} end local leftmin = false local rightmax = false for i = 1, m do local b = io.read("*n") local lb = lower_bound(a, b) if 1 == lb then if not leftmin then leftmin = b end elseif n < lb then rightmax = b else table.insert(t[lb - 1], b) end end local ret = 0 if leftmin then ret = ret + a[1] - leftmin end if rightmax then ret = ret + rightmax - a[n] end for i = 1, n - 1 do if 0 < #t[i] then local ti = t[i] local lenmax = mma(ti[1] - a[i], a[i + 1] - ti[#ti]) for j = 1, #ti - 1 do lenmax = mma(lenmax, ti[j + 1] - ti[j]) end ret = ret + a[i + 1] - a[i] - lenmax end end ret = tostring(ret * 1LL):gsub("LL", "") print(ret)