N,M = map(int,raw_input().split(" ")) D = [-1000000000] + [input() for i in range(M)] + [10000000000000] pos = 0 ans = 0 for i in range(N): a,p,pos = min([(abs(pos-D[i]),i,D[i]) for i in range(len(D))]) ans += a D.pop(p) print ans