#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	string X;
	cin >> X;
	sort(X.rbegin(), X.rend());
	if (count(X.begin(), X.end(), X[0]) == X.size()) {
		cout << -1 << endl;
		return 0;
	}
	prev_permutation(X.begin(), X.end());
	if (X[0] == '0') {
		cout << -1 << endl;
		return 0;
	}
	cout << X << endl;
	return 0;
}