import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from bisect import bisect class BinaryIndexedTree(): def __init__(self, seq): self.size = len(seq) self.depth = self.size.bit_length() self.build(seq) def build(self, seq): data = seq size = self.size for i, x in enumerate(data): j = i + (i & (-i)) if j < size: data[j] += data[i] self.data = data def __repr__(self): return self.data.__repr__() def get_sum(self, i): data = self.data s = 0 while i: s += data[i] i -= i & -i return s def add(self, i, x): data = self.data size = self.size while i < size: data[i] += x i += i & -i def find_kth_element(self, k): data = self.data size = self.size x, sx = 0, 0 dx = 1 << (self.depth) for i in range(self.depth - 1, -1, -1): dx = (1 << i) if x + dx >= size: continue y = x + dx sy = sx + data[y] if sy < k: x, sx = y, sy return x + 1 N, L = map(int, readline().split()) X = tuple(map(int, readline().split())) Y = tuple(map(int, readline().split())) sushi = BinaryIndexedTree([0] + [1] * (N + N)) tea = BinaryIndexedTree([0] + [1] * (N + N)) x = X[0] sushi.add(1, -1) sushi.add(N + 1, -1) cycle = 0 for _ in range(N - 1): # 次の茶まで進む k = tea.get_sum(bisect(Y, x)) # 左にある茶の個数 i = tea.find_kth_element(k + 1) if i > N: i -= N cycle += 1 x = Y[i - 1] # 次の寿司まで進む k = sushi.get_sum(bisect(X, x)) i = sushi.find_kth_element(k + 1) if i > N: i -= N cycle += 1 x = X[i - 1] # 寿司を削除 sushi.add(i, -1) sushi.add(i + N, -1) # 手前のお茶を削除 k = tea.get_sum(bisect(Y, x) + N) i = tea.find_kth_element(k) if i > N: i -= N tea.add(i, -1) tea.add(i + N, -1) i = tea.find_kth_element(1) y = Y[i - 1] if x > y: y += L print(cycle * L + y)