N, M = map(int,input().split()) D = [int(input()) for _ in range(M)] C = 0 P = 0 while N > 0: L = [] for i in D: L.append(abs(P - i)) X = min(L) Y = L.index(X) P = D.pop(Y) N -= 1 C += X print(C)