unittest
{
	assert( [ "5 5", "3949 3774 3598 3469 3424" ].parse.expand.solve == 1541 );
	assert( [ "5 3", "7 4 2 6 4" ].parse.expand.solve == 7 );
}



import core.bitop;
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nk = input.front.split.to!( long[] );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	return tuple( nk[ 1 ], as );
}

auto solve( long k, long[] as )
{
	auto ans = long.max;
	foreach( b; 1L .. 1L << as.length )
	{
		if( b._popcnt != k ) continue;
		auto t = 0L;
		auto p = reduce!max( 0L, as[ 0 .. b.bsf ] );
		for( auto i = 0L; 0 < b; i++, b >>= 1 )
		{
			if( ( b & 1 ) == 0 ) continue;
			if( as[ i ] <= p )
			{
				p++;
				t += p - as[ i ];
			}
			else
			{
				p = as[ i ];
			}
		}
		ans = min( ans, t );
	}
	return ans;
}
