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 mx = max(D) mn = min(D) if mx == 0: ans = L * (mx - mn) + B[0] else: D = [d - mn 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 ans = L*loop + walk print(ans)