unittest
{
	assert( [ "5 3", "10 30 40 50 20" ].parse.expand.solve == 30 );
	assert( [ "3 1", "10 20 10" ].parse.expand.solve == 20 );
	assert( [ "2 100", "10 10" ].parse.expand.solve == 0 );
	assert( [ "10 4", "40 10 20 70 80 10 20 70 80 60" ].parse.expand.solve == 40 );
}



import std.conv;
import std.math;
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 hs = input.front.split.to!( long[] );
	return tuple( nk[ 1 ], hs );
}

auto solve( long k, long[] hs )
{
	auto dp = new long[ hs.length ];
	
	dp[ 0 ] = 0L;
	for( auto i = 1L; i < dp.length; i++ )
	{
		auto m = long.max;
		for( auto ki = 1L; 0 <= i - ki && ki <= k; ki++ )
		{
			auto mi = i - ki;
			auto d = dp[ mi ] + ( hs[ mi ] - hs[ i ] ).abs;
			m = ( d < m ) ? d : m;
		}
		dp[ i ] = m;
	}
	
	return dp[ $ - 1 ];
}
