#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdlib>
#include <cstring>
#include <sstream>

#define REP(i, n) for(int i=0;i<(n);++i)
#define REPE(i, n) for(int i=0;i<=(n);++i) 
#define REPA(i, a, b) for(int i=(a);i<(b);++i)
#define REPAE(i, a, b) for(int i=(a);i<=(b);++i) 

using namespace std;

typedef unsigned long long int ULINT;
typedef long long int LINT;
typedef pair<int,int> PPI;

int main() {
for(;;) {
		string str;
		cin >> str;
		int avail = 0;
		bool ok = false;
		if(str == "END") break;
		if(str[0] == '-')
		{
			for(int i = str.size() - 1; i >= 1; --i)
			{
				if(str[i] != '9')
				{
					++str[i];
					ok = true;
					break;
				}
			}
			if(!ok)
			{
				cout << '-' << '1' << str.substr(1) << endl;
			}
			else
			{
				cout << str << endl;
			}
			continue;
		}

		for(int i = str.size() - 2; i >=0; --i)
		{
			avail += '9' - str[i+1];
			if(str[i] != '0' && avail >= 2)
			{
				--str[i];
				sort(str.begin() + i + 1, str.end(), greater<char>());
				int nahr = 0;
				for(int j = i + 1; j < str.size(); ++j)
				{
					if(str[j] != '9')
					{
						++str[j];
						++nahr;
						--j;
					}
					if(nahr == 2)
					break;
				}
				ok = true;
				break;
			}
			
		}
		if(ok)
		{	
			if(str[0] == '0')
			str = str.substr(1);
			cout << str << endl;
		}
		else
		{
			sort(str.begin(), str.end());
			bool found = false;
			for(int i = str.size() - 1; i >= 0; --i)
			{
				if(str[i] != '9')
				{
					++str[i];
					found = true;
					break;
				}
			}
			if(!found)
			{
			cout << "-1" << str << endl;
			}
			else
			{
			cout << '-' << str << endl;
			}
		}
		}

	return 0;
}