#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from bisect import bisect_left import itertools N, K = map(int, readline().split()) P = [0] + list(map(int, readline().split())) B = [0] + list(map(int, readline().split())) if K == 0: print(0) exit() D = [sum(abs(x - y) for x, y in zip(P, B))] for _ in range(K - 1): diff = 0 Q = [] while P[-2] > P[-1]: x = P.pop() diff -= abs(x - B[len(P)]) Q.append(x) x = P.pop() diff -= abs(x - B[len(P)]) Q.append(x) if not P[-1]: P = list(range(N + 1)) D.append(sum(abs(x - y) for x, y in zip(P, B))) continue p = P.pop() diff -= abs(p - B[len(P)]) n = bisect_left(Q, p) for x in itertools.chain([Q[n]], Q[:n], [p], Q[n + 1:]): diff += abs(x - B[len(P)]) P.append(x) D.append(D[-1] + diff) print(sum(D))