import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }

void main() {
  auto N = readln.chomp.to!int;
  auto A = readln.chomp.map!(c => cast(int)(c - '0')).array.sort();

  int solve() {
    int[] d = new int[N-1];
    foreach(i; 0..N-1) {
      d[i] = A[i] > A[i+1] ? A[i] - A[i+1] : A[i+1] - A[i];
    }

    if (N == 2) return d[0] < 0 ? -d[0] : d[0];
    
    auto answer = d[1] < d[2] ? d[2] - d[1] : d[1] - d[2];
    foreach(i; 0..N-2) {
      auto n = d[i] < d[i+1] ? d[i+1] - d[i] : d[i] - d[i+1];
      answer -= n;
      if (answer < 0) answer *= -1;
    }

    return answer;
  }

  solve().writeln();
}
