import math N, M = map(int,raw_input().split()) Dm = [] count = 0 position = 0 cost = 0 for i in range(M): Dm.append(input()) while count < N: for i in range(len(Dm)): if i == 0: dif = math.fabs(position - Dm[i]) temp1 = Dm[i] temp2 = i else: if(dif > math.fabs(position - Dm[i])): dif = math.fabs(position - Dm[i]) temp1 = Dm[i] temp2 = i del Dm[temp2] cost += dif position = temp1 count += 1 print int(cost)