import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int k, int[] a) {
    int ans = int.max;
    int n = cast(int) a.length;
    for (int i = 1; i < (1 << n); i++) {
        int[] selected;
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                selected ~= a[j];
            }
        }

        if (selected.length == k) {
            int h = selected[0];
            int cost = 0;
            for (int j = 1; j < selected.length; j++) {
                if (h < selected[j]) {
                    h = selected[j];
                }
                else {
                    // selected[j] を h+1 まで増やす
                    cost += (h + 1) - selected[j];
                    h = h + 1;
                }
            }
            ans = min(ans, cost);
        }
    }
    return ans;
}

void main() {
    auto nk = readints;
    int k = nk[0];
    auto a = readints;
    writeln(calc(k, a));
}
