// ロジック検討中.
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL MAX = 1e9;

int main() {
    
    // 1. 入力情報取得.
    LL N;
    scanf("%llu", &N);
    LL Y[N];
    for(int i = 0; i < N; i++) scanf("%llu", &Y[i]), Y[i] += 1e9;

    // 2. 昇順sort.
    sort(Y, Y + N);
    
    // 3. Noelちゃんが動かす必要のある距離の総和の最小値 を 計算する.
    // -> 二分探索で, opt を 探す
    LL hi = Y[N - 1];
    LL lo = Y[0];
    LL opt = 1 + (hi + lo) / 2;
    LL dist = 1e14, cur = 0;
    int counter = 0;
    // 無限ループ防止のため, カウンター入れる.
    while(counter < 100){
        
        // 3-1. 移動距離を保存.
        cur = 0;
        for(int i = 0; i < N; i++) cur += abs(opt - Y[i]);
        
        // 3-2. hi, lo, opt 更新.
        // cout << " hi=" << hi << " lo=" << lo << " opt=" << opt << endl;
        if(cur < dist) hi = (lo + hi) / 2LL;
        else           lo = (lo + hi) / 2LL;
        opt = 1 + (hi + lo) / 2;
        
        // 3-3. カウンター を インクリメント.
        counter++;
        
        // 3-4. 最小移動を距離更新.
        dist = min(dist, cur);
        
        // 3-5. 終了.
        if(hi - lo <= 0) break;
        
    }
    
    // 4. 出力.
    printf("%llu\n", dist);
    return 0;
    
}