#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <algorithm>
#include <numeric>
long long int power(long long int base, int exp) {
	switch (exp) {
	case 0: return 1;
	case 1: return base;
	default: return power(base, exp & 1) * power(base * base, exp >> 1);
	}
}
static std::string old_password;
static long long int old;
long long int diff(const long long int new_value) {
	return std::min(std::abs(old - new_value), power(10, old_password.size()) - std::abs(old - new_value));
}
long long int new_password(const long long int new_temp, const long long int digit, int used) {
	if (digit == 0) {
		return new_temp;
	}
	std::vector<int> suspects(10); std::iota(suspects.begin(), suspects.end(), 0);
	suspects.erase(std::remove_if(suspects.begin(), suspects.end(), [used](int i) {return (used & (1 << i)) != 0; }), suspects.end());
	std::sort(suspects.begin(), suspects.end(), [new_temp, digit](int i, int j) {return diff(new_temp + digit * i) > diff(new_temp + digit * j); });
	long long int max_value = new_password(new_temp + digit * suspects.front(), digit / 10, used | (1 << suspects.front()));
	for (auto i = 1; i < 3 && i < suspects.size(); ++i) {
		const auto temp = new_password(new_temp + digit * suspects[i], digit / 10, used | (1 << suspects[i]));
		if (diff(max_value) < diff(temp) || (diff(max_value) == diff(temp) && temp < max_value)) {
			max_value = temp;
		}
	}
	return max_value;
}
int main() {
	std::cin >> old_password;
	old = std::stoll(old_password);
	auto result_value = new_password(0, power(10, old_password.size() - 1), 0);
	std::string result(old_password.size(), '0');
	for (int i = old_password.size() - 1; i >= 0; --i) {
		result[i] = '0' + result_value % 10;
		result_value /= 10;
	}
	std::cout << result << std::endl;
}
