
https://www.acmicpc.net/problem/16943
순열 함수(next_permutation) 이용하면 so easy~
※ 0으로 시작하면 안된다는 조건 주의
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
	string A,B;
	int tmp=0, result=-1;
	
	cin >> A >> B;
	sort(A.begin(), A.end());
	do {
		tmp = stoi(A);
		if(tmp < stoi(B) && tmp > result)
			result = tmp;
	} while(next_permutation(A.begin(), A.end()));
	
	
	if(result < pow(10, A.size()-1))
		result = -1;
	
	cout << result << endl;
	
	return 0;
}