#include <bits/stdc++.h>

using namespace std;

int n, t;

void compute(vector<int> &a, vector<int> &v, int &ans){
    int temp_ans = 0;
    for(int i = 0; i < n; ++i){
        int temp = INT_MAX;
        for(int j = 0;j < t; ++j){
            temp = min(temp, abs(a[i] - v[j]));
        }
        temp_ans += temp;
    }
    ans = min(ans, temp_ans);
}

void f(std::vector<int> &a, vector<int> &v, int &ans, int pos){
    if(pos < 0 and v.size() != t){
        return;
    }

    if(v.size() == t){
        compute(a, v, ans);
        return;
    }

    v.push_back(a[pos]);
    f(a, v, ans, pos-1);
    v.pop_back();
    f(a, v, ans, pos-1);

    return;
}

int main(){ 

    cin >> t;

    cin >> n;
    std::vector<int> a(n);

    for(int i = 0; i < n; ++i)
        cin >> a[i];
    int ans = INT_MAX;
    std::vector<int> v;
    f(a, v, ans, n-1);

    cout << ans;
    return 0;
}