#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;

class TheInteger {
public:
	long long find(long long n, int k) 
	{
		stringstream ss;
		ss << n;
		string s;
		ss >> s;

		if( s.size() < k )
			s = string(k-s.size(), '0') + s;

		LL a = solve(s, k);
		if( a )
			return a;
		return solve("0"+s, k);
	}

	LL solve(string s, int k)
	{
		// no change
		if( s[0] != '0' )
		{
			set<char> d;
			for(int i=0; i<s.size(); ++i)
				d.insert( s[i] );
			if( d.size() == k )
				return toll(s);
		}

		// a-th digit larger
		int lim = s[0]=='0' ? 1 : s.size();
		for(int a=lim-1; a>=0; --a)
			for(char v=s[a]+1; v<='9'; ++v)
			{
				set<char> d;
				for(int i=0; i<a; ++i)
					d.insert(s[i]);
				d.insert(v);
				if( d.size() > k )
					continue;
				else if( d.size() == k )
				{
					s[a] = v;
					for(int i=a+1; i<s.size(); ++i)
						s[i] = *d.begin();
					return toll(s);
				}
				else
				{
					int rest = k - d.size();
					if( s.size()-a-1 < rest )
						continue;
					s[a] = v;

					int fi = s.size()-rest;

					for(int i=a+1; i<fi; ++i)
						s[i] = '0';

					for(char z='0'; z<='9'; ++z)
						if( !d.count(z) )
						{
							s[fi++] = z;
							if( fi == s.size() )
								break;
						}

					return toll(s);
				}
			}
		return 0;
	}

	LL toll( string s )
	{
		stringstream ss; ss << s;
		LL n; ss >> n;
		return n;
	}
};

// 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 long long &Expected, const long long &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>) {
	long long n = 47LL; 
	int k = 1; 
	long long RetVal = 55LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<1>) {
	long long n = 7LL; 
	int k = 3; 
	long long RetVal = 102LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<2>) {
	long long n = 69LL; 
	int k = 2; 
	long long RetVal = 69LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<3>) {
	long long n = 12364LL; 
	int k = 3; 
	long long RetVal = 12411LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<4>) {
	long long n = 1LL; 
	int k = 10; 
	long long RetVal = 1023456789LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<5>) {
	long long n = 111111111111111LL; 
	int k = 10; 
	long long RetVal = 111111203456789LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<6>) {
	long long n = 111111111111111LL; 
	int k = 8; 
	long long RetVal = 111111112034567LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<7>) {
	long long n = 100000000000000LL; 
	int k = 10; 
	long long RetVal = 100000023456789LL; 
	return verify_case(RetVal, TheInteger().find(n, k)); }
int Test_(Case_<8>) {
	long long n = 100000000000000LL; 
	int k = 5; 
	long long RetVal = 100000000000234LL; 
	return verify_case(RetVal, TheInteger().find(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

