import sys # sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 md = 10**9+7 # md = 998244353 class BitSum: def __init__(self, n): self.n = n+1 self.table = [0]*self.n def add(self, i, x): i += 1 while i < self.n: self.table[i] += x i += i & -i def sum(self, i): i += 1 res = 0 while i > 0: res += self.table[i] i -= i & -i return res def pos(self, x): idx = 0 for lv in range((self.n-1).bit_length()-1, -1, -1): mid = idx+(1 << lv) if mid >= self.n: continue if self.table[mid] < x: x -= self.table[mid] idx += 1 << lv return idx # 要素を先読みする代わりに、[0,10**6]でなくとも使える class Cset: def __init__(self, aa): aa = set(aa) self.dec = tuple(sorted(aa)) self.enc = {a: i for i, a in enumerate(self.dec)} self.mx = len(self.dec) self.size = 0 self.bit = BitSum(self.mx+1) self.nums = [False]*(self.mx+1) def __repr__(self): return "["+" ".join(str(self[i]) for i in range(self.size))+"]" def insert(self, a): if a not in self.enc: return False a = self.enc[a] if a < 0 or a > self.mx or self.nums[a]: return False self.size += 1 self.bit.add(a, 1) self.nums[a] = True return True def remove(self, value): if value not in self.enc: return False value = self.enc[value] if value < 0 or value > self.mx or not self.nums[value]: return False self.size -= 1 self.bit.add(value, -1) self.nums[value] = False return True def erase(self, index): i = index if i >= self.size or i < -self.size: return False if i < 0: i = self.size+i+1 else: i += 1 a = self.bit.pos(i) self.size -= 1 self.bit.add(a, -1) self.nums[a] = False return True def __getitem__(self, i): if i >= self.size or i < -self.size: return -1 if i < 0: i = self.size+i+1 else: i += 1 return self.dec[self.bit.pos(i)] def __contains__(self, item): return self.nums[self.enc[item]] def __len__(self): return self.size def bisect_left(self, a): if a not in self.enc: return -1 a = self.enc[a] if a < 0 or a > self.mx: return -1 if a == 0: return 0 return self.bit.sum(a-1) def bisect_right(self, a): if a not in self.enc: return -1 a = self.enc[a] if a < 0 or a > self.mx: return -1 return self.bit.sum(a) n, l = LI() xx0 = LI() yy0 = LI() ans = now = xx0[0] xx = Cset(xx0[1:]+yy0) yy = Cset(xx0[1:]+yy0) for x in xx0[1:]: xx.insert(x) for y in yy0: yy.insert(y) for _ in range(n-1): j = yy.bisect_left(now)%len(yy) # 一番近いお茶 i = xx.bisect_left(yy[j])%len(xx) # お茶後の寿司 x = xx[i] j = (yy.bisect_left(x)-1)%len(yy) # 次の寿司直前のお茶 y = yy[j] ans += (y-now)%l+(x-y)%l xx.remove(x) yy.remove(y) now = x # pDB(y, x, ans, xx, yy) ans += (yy[0]-now)%l print(ans)