def merge(x, y): x.reverse() y.reverse() res = [] while x and y: if x[-1] < y[-1]: res.append(x.pop()) else: res.append(~y.pop()) while x: res.append(x.pop()) while y: res.append(~y.pop()) return res N, L = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = merge(A, B) p = 0 D = [-1] * (2 * N) for i, c in enumerate(C): if c >= 0: p += 1 else: p -= 1 D[i] = p m = min(D) D = [d - m for d in D] loop = max(D) - 1 walk = -1 for c, d in zip(C, D): if c < 0 and d == loop: walk = ~c print(L*loop + walk)