using System;
using System.Text;
namespace CSharpPlayground
{
public class Program
{
static void Main()
{
string[] txt = Console.ReadLine().Split(' ');
string numberN = txt[0];
string numberK = txt[1];
StringBuilder sb = new StringBuilder();
int k = Int32.Parse(numberK);
ulong n = ulong.Parse(numberN);
int[] digits = new int[numberN.Length];
for (int i = 0; i < numberN.Length; i++)
digits[i] = Int32.Parse(numberN[i].ToString());
if (numberN.Length < k)
{
sb.Clear();
for (int i = 0; i < k; i++)
sb.Append(5);
}
else if (CountFivesOnUlong(n + 1) >= k)
{
sb.Append(n + 1);
}
else if (k == 1)
{
while (true)
{
n = n + 1;
if (CountFivesOnUlong(n) >= 1)
{
sb.Append(n);
break;
}
}
}
else
{
digits = AddZeroAtFirst(digits);
for (int i = digits.Length - 1; i > 0; i--)
{
if (Check(digits, n, k))
break;
if (digits[i] < 5)
{
digits[i] = 5;
if (Check(digits, n, k))
{
sb.Append(ConvertToUlong(digits));
break;
}
}
else if (digits[i] >= 5)
{
if (digits[i - 1] == 9)
{
digits[i] = 5;
if (Check(digits, n, k))
{
sb.Append(ConvertToUlong(digits));
break;
}
for (int j = i - 1; j >= 0; j--)
{
if (digits[j] == 9)
digits[j] = 0;
else
{
digits[j]++;
break;
}
}
if (Check(digits, n, k) && CountFives(digits) == k)
{
sb.Append(ConvertToUlong(digits));
break;
}
}
else
{
digits[i - 1]++;
digits[i] = 5;
if (Check(digits, n, k))
{
sb.Append(ConvertToUlong(digits));
break;
}
}
}
}
if(CountFives(digits) > k)
{
for(int i = 1; i < digits.Length - 1; i++)
{
if(digits[i] == 5)
{
digits[i] = 0;
if (Check(digits, n, k))
{
sb.Clear();
sb.Append(ConvertToUlong(digits));
}
else
digits[i] = 5;
}
}
}
}
Console.WriteLine(sb);
}
private static int CountFives(int[] digits)
{
int sum = 0;
for (int i = 0; i < digits.Length; i++)
if (digits[i] == 5)
sum++;
return sum;
}
private static bool Check(int[] digits, ulong n, int k)
{
if (CountFives(digits) >= k && ConvertToUlong(digits) > n)
return true;
else
return false;
}
private static ulong ConvertToUlong(int[] digits)
{
string tmp = "";
for (int i = 0; i < digits.Length; i++)
{
tmp += digits[i];
}
return ulong.Parse(tmp);
}
private static int[] AddZeroAtFirst(int[] digits)
{
int[] tmp = new int[digits.Length + 1];
tmp[0] = 0;
for (int i = 1; i < tmp.Length; i++)
tmp[i] = digits[i - 1];
return tmp;
}
private static int CountFivesOnUlong(ulong n)
{
int counter = 0;
ulong number = n;
while (number != 0)
{
if (number % 10 == 5)
counter++;
number = number / 10;
}
return counter;
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dDsKCm5hbWVzcGFjZSBDU2hhcnBQbGF5Z3JvdW5kCnsKICAgIHB1YmxpYyBjbGFzcyBQcm9ncmFtCiAgICB7CiAgICAgICAgc3RhdGljIHZvaWQgTWFpbigpCiAgICAgICAgewogICAgICAgICAgICBzdHJpbmdbXSB0eHQgPSBDb25zb2xlLlJlYWRMaW5lKCkuU3BsaXQoJyAnKTsKICAgICAgICAgICAgc3RyaW5nIG51bWJlck4gPSB0eHRbMF07CiAgICAgICAgICAgIHN0cmluZyBudW1iZXJLID0gdHh0WzFdOwoKICAgICAgICAgICAgU3RyaW5nQnVpbGRlciBzYiA9IG5ldyBTdHJpbmdCdWlsZGVyKCk7CgogICAgICAgICAgICBpbnQgayA9IEludDMyLlBhcnNlKG51bWJlckspOwogICAgICAgICAgICB1bG9uZyBuID0gdWxvbmcuUGFyc2UobnVtYmVyTik7CgogICAgICAgICAgICBpbnRbXSBkaWdpdHMgPSBuZXcgaW50W251bWJlck4uTGVuZ3RoXTsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBudW1iZXJOLkxlbmd0aDsgaSsrKQogICAgICAgICAgICAgICAgZGlnaXRzW2ldID0gSW50MzIuUGFyc2UobnVtYmVyTltpXS5Ub1N0cmluZygpKTsKCgogICAgICAgICAgICBpZiAobnVtYmVyTi5MZW5ndGggPCBrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzYi5DbGVhcigpOwogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBrOyBpKyspCiAgICAgICAgICAgICAgICAgICAgc2IuQXBwZW5kKDUpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKENvdW50Rml2ZXNPblVsb25nKG4gKyAxKSA+PSBrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzYi5BcHBlbmQobiArIDEpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYgKGsgPT0gMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgd2hpbGUgKHRydWUpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgbiA9IG4gKyAxOwogICAgICAgICAgICAgICAgICAgIGlmIChDb3VudEZpdmVzT25VbG9uZyhuKSA+PSAxKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgc2IuQXBwZW5kKG4pOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBkaWdpdHMgPSBBZGRaZXJvQXRGaXJzdChkaWdpdHMpOwoKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSBkaWdpdHMuTGVuZ3RoIC0gMTsgaSA+IDA7IGktLSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZiAoQ2hlY2soZGlnaXRzLCBuLCBrKSkKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CgogICAgICAgICAgICAgICAgICAgIGlmIChkaWdpdHNbaV0gPCA1KQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgZGlnaXRzW2ldID0gNTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKENoZWNrKGRpZ2l0cywgbiwgaykpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNiLkFwcGVuZChDb252ZXJ0VG9VbG9uZyhkaWdpdHMpKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKGRpZ2l0c1tpXSA+PSA1KQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGRpZ2l0c1tpIC0gMV0gPT0gOSkKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgZGlnaXRzW2ldID0gNTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChDaGVjayhkaWdpdHMsIG4sIGspKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNiLkFwcGVuZChDb252ZXJ0VG9VbG9uZyhkaWdpdHMpKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gaSAtIDE7IGogPj0gMDsgai0tKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChkaWdpdHNbal0gPT0gOSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZGlnaXRzW2pdID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgewoKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZGlnaXRzW2pdKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoQ2hlY2soZGlnaXRzLCBuLCBrKSAmJiBDb3VudEZpdmVzKGRpZ2l0cykgPT0gaykKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzYi5BcHBlbmQoQ29udmVydFRvVWxvbmcoZGlnaXRzKSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBkaWdpdHNbaSAtIDFdKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBkaWdpdHNbaV0gPSA1OwoKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChDaGVjayhkaWdpdHMsIG4sIGspKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNiLkFwcGVuZChDb252ZXJ0VG9VbG9uZyhkaWdpdHMpKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBpZihDb3VudEZpdmVzKGRpZ2l0cykgPiBrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGZvcihpbnQgaSA9IDE7IGkgPCBkaWdpdHMuTGVuZ3RoIC0gMTsgaSsrKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoZGlnaXRzW2ldID09IDUpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRpZ2l0c1tpXSA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoQ2hlY2soZGlnaXRzLCBuLCBrKSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzYi5DbGVhcigpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNiLkFwcGVuZChDb252ZXJ0VG9VbG9uZyhkaWdpdHMpKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkaWdpdHNbaV0gPSA1OwogICAgICAgICAgICAgICAgICAgICAgICB9ICAKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKHNiKTsKICAgICAgICB9CgogICAgICAgIHByaXZhdGUgc3RhdGljIGludCBDb3VudEZpdmVzKGludFtdIGRpZ2l0cykKICAgICAgICB7CiAgICAgICAgICAgIGludCBzdW0gPSAwOwogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGRpZ2l0cy5MZW5ndGg7IGkrKykKICAgICAgICAgICAgICAgIGlmIChkaWdpdHNbaV0gPT0gNSkKICAgICAgICAgICAgICAgICAgICBzdW0rKzsKCiAgICAgICAgICAgIHJldHVybiBzdW07CiAgICAgICAgfQoKICAgICAgICBwcml2YXRlIHN0YXRpYyBib29sIENoZWNrKGludFtdIGRpZ2l0cywgdWxvbmcgbiwgaW50IGspCiAgICAgICAgewogICAgICAgICAgICBpZiAoQ291bnRGaXZlcyhkaWdpdHMpID49IGsgJiYgQ29udmVydFRvVWxvbmcoZGlnaXRzKSA+IG4pCiAgICAgICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KCiAgICAgICAgcHJpdmF0ZSBzdGF0aWMgdWxvbmcgQ29udmVydFRvVWxvbmcoaW50W10gZGlnaXRzKQogICAgICAgIHsKICAgICAgICAgICAgc3RyaW5nIHRtcCA9ICIiOwogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGRpZ2l0cy5MZW5ndGg7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdG1wICs9IGRpZ2l0c1tpXTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgcmV0dXJuIHVsb25nLlBhcnNlKHRtcCk7CiAgICAgICAgfQoKICAgICAgICBwcml2YXRlIHN0YXRpYyBpbnRbXSBBZGRaZXJvQXRGaXJzdChpbnRbXSBkaWdpdHMpCiAgICAgICAgewogICAgICAgICAgICBpbnRbXSB0bXAgPSBuZXcgaW50W2RpZ2l0cy5MZW5ndGggKyAxXTsKICAgICAgICAgICAgdG1wWzBdID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPCB0bXAuTGVuZ3RoOyBpKyspCiAgICAgICAgICAgICAgICB0bXBbaV0gPSBkaWdpdHNbaSAtIDFdOwoKICAgICAgICAgICAgcmV0dXJuIHRtcDsKICAgICAgICB9CgogICAgICAgIHByaXZhdGUgc3RhdGljIGludCBDb3VudEZpdmVzT25VbG9uZyh1bG9uZyBuKQogICAgICAgIHsKICAgICAgICAgICAgaW50IGNvdW50ZXIgPSAwOwogICAgICAgICAgICB1bG9uZyBudW1iZXIgPSBuOwogICAgICAgICAgICB3aGlsZSAobnVtYmVyICE9IDApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChudW1iZXIgJSAxMCA9PSA1KQogICAgICAgICAgICAgICAgICAgIGNvdW50ZXIrKzsKCiAgICAgICAgICAgICAgICBudW1iZXIgPSBudW1iZXIgLyAxMDsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgcmV0dXJuIGNvdW50ZXI7CiAgICAgICAgfQogICAgfQp9