import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] arr = new int[n + 2]; String[] line = br.readLine().split(" ", n); for (int i = 1; i <= n; i++) { arr[i] = Integer.parseInt(line[i - 1]); } arr[0] = Integer.MIN_VALUE / 2; arr[n + 1] = Integer.MAX_VALUE / 2; Arrays.sort(arr); long total = 0; for (int i = 1; i <= n; i++) { if (arr[i] - arr[i - 1] < arr[i + 1] - arr[i]) { total += arr[i] - arr[i - 1]; arr[i] = arr[i - 1]; } else { total += arr[i + 1] - arr[i]; arr[i] = arr[i + 1]; } } System.out.println(total); } }