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(M)]) ans += a D.pop(p) print ans