n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.append(0) b.append(0) sm = 0 costs = [] pre = 0 for x, y in zip(a, b): z = (y - x) % m v = (z - pre) % m sm += v costs.append(m - v) pre = z cnt = sm // m costs.sort() ans = sum(costs[:cnt]) print(ans)