#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 1;
const long long INF = 4e18;
long long n, k, l, x, y, ans, a[maxn], b[maxn], dp[5001][5001];
bool mark[maxn];
vector<long long> v;
long long solve(int i, int j) {
  if (dp[i][j] != -1) return dp[i][j];
  long long s1 = (long long)1e14, s2 = (long long)1e14, s = i * x + j * y;
  if (i != 0) s1 = (long long)solve(i - 1, j) + abs(a[s - 1] - a[s - x]);
  if (j != 0) s2 = (long long)solve(i, j - 1) + abs(a[s - 1] - a[s - y]);
  return dp[i][j] = min(s1, s2);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < 5000; i++)
    for (int j = 0; j < 5000; j++) dp[i][j] = -1;
  sort(a, a + n);
  for (int i = 0; i < n; i++)
    if (!mark[i]) {
      l = 0;
      for (int j = i; j < n; j += k) l++, mark[j] = 1;
      v.push_back(l), b[l]++;
    }
  sort(v.begin(), v.end());
  x = v[0], y = v[v.size() - 1];
  if (x == y) {
    for (int i = 0; i < n; i += x) ans += abs(a[i] - a[i + x - 1]);
    return cout << ans, 0;
  }
  dp[0][0] = 0;
  dp[0][1] = abs(a[0] - a[y - 1]);
  dp[1][0] = abs(a[0] - a[x - 1]);
  cout << solve(b[x], b[y]);
  return 0;
}
