#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <map>
#define LOCAL
#ifdef LOCAL
#define cin in
#define cout out
#endif
#define FOREACH(i, n) for (typeof(n.begin()) i = n.begin(); i != n.end(); ++i)
#define MEMSET(p, c) memset(p, c, sizeof(p))
using namespace std;
template < class T,class T2 >
T2 fac( T n)
{
return n< 2 ? 1 : fac< T,T2> ( n- 1 ) * n;
}
template < class T>
T nb_combinaisons( T k,T n)
{
if ( n< k)
return nb_combinaisons< T> ( n,k) ;
return fac< T,long long int > ( n) / ( fac< T,long long int > ( n- k) * fac< T,long long int > ( k) ) ;
}
string itoa_2( long long int n)
{
string s;
stringstream ss;
ss<< n;
ss>> s;
while ( s.size ( ) ! = 9 )
s= '0' + s;
return s;
}
int main( )
{
#ifdef LOCAL
ifstream in( "input.txt" ) ;
ofstream out( "output.txt" ) ;
#endif
int n,k;
cin >> n>> k;
string s;
cin >> s;
sort( s.begin ( ) ,s.end ( ) ) ;
int mini_global= 10000000 ;
for ( int c= 0 ; c< 10 ; c++ )
{
int c2;
for ( c2= 0 ; c2< s.size ( ) && s[ c2] < c+ '0' ; c2++ ) ;
int total= abs ( s[ c2] - '0' - c) ;
int bas= c2- 1 ,haut= c2+ 1 ;
for ( c2= 0 ; c2< k- 1 ; c2++ )
{
if ( haut>= s.size ( ) || ( bas>= 0 && abs ( s[ bas] - '0' - c) <= abs ( s[ bas] - '0' - c) ) )
{
total+ = abs ( s[ bas] - '0' - c) ;
bas-- ;
}
else
{
total+ = abs ( s[ haut] - '0' - c) ;
haut++ ;
}
}
mini_global= min( mini_global,total) ;
}
cout << mini_global;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHNzdHJlYW0+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGF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WdpbigpLHMuZW5kKCkpOwogICBpbnQgbWluaV9nbG9iYWw9MTAwMDAwMDA7CiAgIGZvcihpbnQgYz0wO2M8MTA7YysrKQogICB7CiAgICBpbnQgYzI7CiAgICBmb3IoYzI9MDtjMjxzLnNpemUoKSYmc1tjMl08YysnMCc7YzIrKyk7CiAgICBpbnQgdG90YWw9YWJzKHNbYzJdLScwJy1jKTsKICAgIGludCBiYXM9YzItMSxoYXV0PWMyKzE7CiAgICBmb3IoYzI9MDtjMjxrLTE7YzIrKykKICAgIHsKICAgICAgICBpZihoYXV0Pj1zLnNpemUoKXx8KGJhcz49MCYmYWJzKHNbYmFzXS0nMCctYyk8PWFicyhzW2Jhc10tJzAnLWMpKSkKICAgICAgICB7CiAgICAgICAgICAgIHRvdGFsKz1hYnMoc1tiYXNdLScwJy1jKTsKICAgICAgICAgICAgYmFzLS07CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIHRvdGFsKz1hYnMoc1toYXV0XS0nMCctYyk7CiAgICAgICAgICAgIGhhdXQrKzsKICAgICAgICB9CiAgICB9CiAgICBtaW5pX2dsb2JhbD1taW4obWluaV9nbG9iYWwsdG90YWwpOwogICB9CiAgIGNvdXQ8PG1pbmlfZ2xvYmFsOwp9CgoKCg==