function toInt(d) {
  return parseInt(d)
}

function getDiff (d) {
  var result = 0;
  for (var i = 0; i < d.length - 1; i++) {
    result += Math.abs(d[i] - d[i + 1])
  }
  return result
}

function minIndex (d) {
  var min = 10000000000;
  var result;
  for (var i = 0; i < d.length; i++) {
    if (min > d[i]) {
      min = d[i]
      result = i;
    }
  }
  return result;
}

function maxIndex (d) {
  var max = 0;
  var result;
  for (var i = 0; i < d.length; i++) {
    if (max < d[i]) {
      max = d[i]
      result = i;
    }
  }
  return result;
}

function Main(input) {
  const data = input.split("\n").map(toInt)
  const N = data[0]
  const bucket = []
  for (var i = 0; i < N; i++) {
    bucket.push(data[i + 1])
  }

  const resultBucket = []

  var turn = 'min'
  while (resultBucket.length < N) {
    if (turn === 'min') {
      var i = minIndex(bucket)
      if (resultBucket.length === 0) {
        resultBucket.push(bucket[i])
        bucket.splice(i, 1)
      } else {
        var value = bucket[i]
        if (Math.abs(resultBucket[0] - value) > Math.abs(resultBucket[resultBucket.length - 1] - value)) {
          resultBucket.unshift(value)
          bucket.splice(i, 1)

          if (resultBucket.length === N) {
            return
          }

          var i2 = minIndex(bucket)
          resultBucket.push(bucket[i2])
          bucket.splice(i2, 1)
        } else {
          resultBucket.push(value)
          bucket.splice(i, 1)

          if (resultBucket.length === N) {
            return
          }
          var i2 = minIndex(bucket)
          resultBucket.unshift(bucket[i2])
          bucket.splice(i2, 1)
        }
      }
      turn = 'max'
    } else {
      var i = maxIndex(bucket)
      var value = bucket[i]
      if (Math.abs(resultBucket[0] - value) > Math.abs(resultBucket[resultBucket.length - 1] - value)) {
        resultBucket.unshift(value)
        bucket.splice(i, 1)

        if (resultBucket.length === N) {
          return
        }

        var i2 = maxIndex(bucket)
        resultBucket.push(bucket[i2])
        bucket.splice(i2, 1)
      } else {
        resultBucket.push(value)
        bucket.splice(i, 1)

        if (resultBucket.length === N) {
          return
        }
        var i2 = maxIndex(bucket)
        resultBucket.unshift(bucket[i2])
        bucket.splice(i2, 1)
      }

      turn = 'min'
    }
  }

  console.log(getDiff(resultBucket))
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"))