#include using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; using i128 = __int128; int power(i64 a, i64 b) { i64 res = 1; for (; b; b /= 2, a = 1LL * a * a) { if (b % 2) { res = 1LL * res * a; } } return res; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); i64 n, k; std::cin >> n >> k; std::vector f; while (n) { f.push_back(n % 10); n /= 10; } std::reverse(f.begin(), f.end()); std::vector nums, ans; auto dfs = [&](auto&& self, int dep, i64 cur) -> void { if (dep == f.size()) { ans.push_back(cur); return ; } for (int i = 0; i <= f[dep]; i++) { self(self, dep + 1, cur * 10 + i); } }; dfs(dfs, 0, 0); std::sort(ans.begin(), ans.end()); // for (i64 x : ans) std::cout << x << " "; std::cout << ans[--k] << '\n'; return 0; }