#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

bool exist(const string &s, char tgt) {
  for (auto ch: s) {
    if (ch == tgt) return true;
  }
  return false;
}

int main () {

  string s;
  cin >> s;

  int64_t a = 0;
  for (auto ch: s) {
    a = a * 10 + (ch - '0');
  }

  vector<int> v;
  rep(i, s.size()) v.push_back(1);
  while(v.size() < 10) {
    v.push_back(0);
  }
  reverse(v.begin(), v.end());
  int64_t pow10n = 1;
  rep(i, s.size()) pow10n *= 10;
  int64_t mx = 0;
  string arg_mx = to_string(pow10n - 1);
  do {
    vector<int> w;
    rep(i, 10) {
      if (v[i]) w.push_back(i);
    }
    do {
      int64_t b = 0;
      for (auto d: w) {
        b = b * 10 + d;
      }
      auto tmp = min(abs(a - b), pow10n - abs(a - b));
      if (tmp >= mx) {
        string str_b = "";
        for (auto d: w) {
          str_b += (char)(d + '0');
        }
        if (tmp > mx) {
          mx = tmp;
          arg_mx = str_b;
        } else if(str_b < arg_mx) {
          arg_mx = str_b;
        }
      }
    } while(next_permutation(w.begin(), w.end()));
  } while(next_permutation(v.begin(), v.end()));

  cout << arg_mx << endl;
  return 0;
}
