#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename D, typename S>
  D lexical_cast(const S& s) { D d; stringstream ss; ss<<s; ss>>d; return d; }

class TheSwap {
public:
	int findMax(int n, int k) 
	{
		set<string> canMake;
		canMake.insert( lexical_cast<string>(n) );

		while( k --> 0 )
		{
			set<string> prev;
			prev.swap(canMake);

			for(set<string>::iterator it=prev.begin(); it!=prev.end(); ++it)
			{
				string s = *it;
				for(int i=0; i<s.size(); ++i)
				for(int j=i+1; j<s.size(); ++j)
					if( !(i==0 && s[j]=='0') )
					{
						swap(s[i], s[j]);
						canMake.insert(s);
						swap(s[i], s[j]);
					}
			}
		}

		return canMake.empty() ? -1 : lexical_cast<int>(*canMake.rbegin());
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int n = 16375; 
	int k = 1; 
	int RetVal = 76315; 
	return verify_case(RetVal, TheSwap().findMax(n, k)); }
int Test_(Case_<1>) {
	int n = 432; 
	int k = 1; 
	int RetVal = 423; 
	return verify_case(RetVal, TheSwap().findMax(n, k)); }
int Test_(Case_<2>) {
	int n = 90; 
	int k = 4; 
	int RetVal = -1; 
	return verify_case(RetVal, TheSwap().findMax(n, k)); }
int Test_(Case_<3>) {
	int n = 5; 
	int k = 2; 
	int RetVal = -1; 
	return verify_case(RetVal, TheSwap().findMax(n, k)); }
int Test_(Case_<4>) {
	int n = 436659; 
	int k = 2; 
	int RetVal = 966354; 
	return verify_case(RetVal, TheSwap().findMax(n, k)); }
int Test_(Case_<5>) {
	int n = 325664; 
	int k = 2; 
	int RetVal = 665324; 
	return verify_case(RetVal, TheSwap().findMax(n, k)); }
int Test_(Case_<6>) {
	int n = 325664; 
	int k = 4; 
	int RetVal = 665432; 
	return verify_case(RetVal, TheSwap().findMax(n, k)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE

