import sys import heapq input = sys.stdin.readline def read_values(): return tuple(map(int, input().split())) def read_list(): return list(map(int, input().split())) def main(): n,m=read_values() a=read_list() b=read_list() ba=[0] for aa,bb in zip(a,b): ba.append((bb-aa)%m) ba.append(0) dif=list() c=0 for i in range(n+1): dif.append((ba[i+1]-ba[i])%m) c+=ba[i+1]