import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
import std.container;
import std.math;
import std.typecons;

alias Edge = Tuple!(ulong, "dst", ulong, "cost");

void main() {
    ulong n, k;

    readf(" %s %s\n", &n, &k);
    long[] x = readln.chomp.split.to!(long[]);

    auto t = new Edge[][](n, 0);
    foreach(i; 0..n)
        foreach(j; 0..n)
            t[i] ~= Edge(j, abs(x[j] - x[i]));

    dijkstra(t, 0)[k - 1].writeln;
}

auto dijkstra(Edge[][] edges, int s) {
    alias Distance = Tuple!(ulong, "node", ulong, "distance");
    auto heap = BinaryHeap!(Array!Distance,"a.distance > b.distance")();
    heap.insert(Distance(s, 0));

    auto ret = new long[edges.length];
    ret[] = long.max;
    ret[s] = 0;

    while (!heap.empty) {
        auto v = heap.front;
        heap.removeFront();

        if (ret[v.node] < v.distance) continue;

        foreach (e; edges[v.node])
        {
            if (ret[e.dst] > ret[v.node] + e.cost) {
                ret[e.dst] = ret[v.node] + e.cost;
                heap.insert(Distance(e.dst, ret[e.dst]));
            }
        }
    }

    return ret;
}
