#include<bits/stdc++.h>
#define f1(i, n) for(int i=1;i<=n;++i)
#define f0(i, n) for(int i=0;i<n;++i)
#define ull unsigned long long
#define ll long long
#define rev(a) reverse(a.begin(),a.end())
#define all(x) x.begin(),x.end()
#define so(A, n) sort(A+1, A+n+1)
using namespace std;
const int maxn = 1e6 + 1;
const int N = 5e5 + 1;
const ll mod = 1e18;
string minNum1(ll n) {
string s = to_string(n);
int num = s[0] - 48;
bool check = true;
int cnt = 0;
for (int i = 1; i < s.size(); ++i) {
if (s[i] > s[i - 1]) {
check = false;
break;
}
else if (s[i] < s[i - 1]) {
break;
}
}
if (cnt == s.size() - 1) return s;
string res = "";
if (num == 10) {
for (int i = 0; i <= s.size(); ++i) {
res += '1';
}
}
else {
for (int i = 0; i < s.size(); ++i) {
res += char(s[0] + (check ? 0 : 1));
}
}
return res;
}
// string minNum2(ll n){
// }
bool checkL1(ll n) {
string s = to_string(n);
for (int i = 1; i < s.size(); ++i) {
if (s[i] != s[i - 1]) return false;
}
return true;
}
bool checkL2_2(ll n) {
string s = to_string(n);
sort(s.begin(), s.end());
if (s[0] == s[1]) {
for (int i = 1; i < s.size() - 1; ++i) {
if (s[i] != s[i - 1]) return false;
}
return true;
}
else {
for (int i = 2; i < s.size(); ++i) {
if (s[i] != s[i - 1]) return false;
}
return true;
}
return false;
}
bool checkL2(ll n) {
if (checkL1(n)) return true;
if (checkL2_2(n)) return true;
return false;
}
ll resL2(ll n) {
while (1) {
if (checkL2(n)) {
break;
}
++n;
}
return n;
}
vector<ll> v;
ll taoso(int sz) {
ll res = 0;
for (int i = 0; i < sz; ++i) {
res = res * 10 + v[i];
}
return res;
}
vector<ll> numbers;
void gen(int so_chu_so) {
int n = so_chu_so;
for (int i = 0; i < n; ++i) {
v.push_back(0);
}
for (int j = 0; j <= 9; ++j) { // iter 0->9
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k) v[k] = j;
for (int k = 1; k <= 9; ++k) {
v[i] = k;
ll motcaigiday = taoso(n);
numbers.push_back(motcaigiday);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll x;
cin >> x;
int k;
cin >> k;
string resL1 = minNum1(x);
if (k == 0) {
cout << resL1;
}
else {
if (x <= 1e5) {
while (1) {
if (checkL2(x)) {
break;
}
++x;
}
ll a = stoll(resL1);
cout << min(a, x);
}
else
{
gen(resL1.size());
sort(numbers.begin(), numbers.end());
// for(int i=0;i<numbers.size();++i) cout<<numbers[i]<<endl;
vector<ll>::iterator res = lower_bound(numbers.begin(), numbers.end(), x);
cout << *res;
}
}
}