n = int(input()) a = list(map(int,input().split())) ans = 0 a.sort() ans += a[1]-a[0] a[0] = a[1] for i in range(1,n-1): if a[i] == a[i-1]: continue if a[i] - a[i-1] < a[i+1] - a[i]: ans += a[i] - a[i-1] a[i] = a[i-1] else: ans += a[i+1] - a[i] a[i] = a[i+1] ans += a[n-1]-a[n-2] print(ans)