n,m = map(int,input().split()) a = [0]+list(map(int,input().split()))+[0] b = [0]+list(map(int,input().split()))+[0] d = sorted((b[i+1]-a[i+1]-b[i]+a[i])%m for i in range(n+1)) c = sum(d)//m print((sum(d[:-c])+c*m-sum(d[-c:]))//2)