Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Change a Password

Password authentication is used in a lot of facilities. The office of JAG also uses password authentication. A password is required to enter their office. A password is a string of $N$ digits '0'-'9'. This password is changed on a regular basis. Taro, a staff of the security division of JAG, decided to use the following rules to generate a new password from an old one.

The new password consists of the same number $N$ of digits to the original one and each digit appears at most once in the new password. It can have a leading zero. (Note that an old password may contain same digits twice or more.)

The new password maximizes the difference from the old password within constraints described above. (Definition of the difference between two passwords is described below.)

If there are two or more candidates, the one which has the minimum value when it is read as an integer will be selected.

The difference between two passwords is defined by min($|a - b|, 10^N - |a - b|$), where $a$ and $b$ are the integers represented by the two passwords. For example, the difference between "11" and "42" is 31, and the difference between "987" and "012" is 25.

Taro would like to use a computer to calculate a new password correctly, but he is not good at programming. Therefore, he asked you to write a program. Your task is to write a program that generates a new password from an old password.

Input

The input consists of a single test case. The first line of the input contains a string $S$ which denotes the old password. You can assume that the length of $S$ is no less than 1 and no greater than 10. Note that old password $S$ may contain same digits twice or more, and may have leading zeros.

Output

Print the new password in a line.

Sample Input 1

201

Output for the Sample Input 1

701

Sample Input 2

512

Output for the Sample Input 2

012

Sample Input 3

99999

Output for the Sample Input 3

49876

Sample Input 4

765876346

Output for the Sample Input 4

265874931

Solution in C (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
#include<string.h>
#include<math.h>

typedef int32_t i32;
typedef int64_t i64;

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))
#define ALLOC(size,type) ((type*) calloc ((size), sizeof (type)))

const i32 f = 10;

i64 max_diff;
i64 max_val;
void calc (i32 k, i32 *a, i64 v, i32 len) {
  if (k == f - 1) {
    i64 t = 0;
    i64 p = 1;
    for (i32 i = 0; i < len; ++i) {
      t = 10 * t + a[i];
      p *= 10;
    }
    i64 diff = MIN (ABS (t - v), p - ABS(t - v));
    if (diff > max_diff || (diff == max_diff && max_val > t)) {
      max_diff = diff;
      max_val = t;
    }
    return;
  }
  calc (k + 1, a, v, len);
  for (i32 i = k + 1; i < f; ++i) {
    i32 swap = a[k];
    a[k] = a[i];
    a[i] = swap;
    calc (k + 1, a, v, len);
  }
  i32 q = a[k];
  for (i32 i = k + 1; i < f; ++i) {
    a[i - 1] = a[i];
  }
  a[f - 1] = q;
}

void run (void) {
  char s[11];
  scanf ("%s", s);
  i32 len = strlen (s);
  i64 v = 0;
  for (i32 i = 0; i < len; ++i) {
    v = 10 * v + s[i] - '0';
  }
  i32 a[10];
  for (i32 i = 0; i < f; ++i) {
    a[i] = i;
  }
  max_diff = 0;
  max_val = 1000000000000;
  calc (0, a, v, len);
  i64 p = 1;
  for (i32 i = 1; i < len; ++i) {
    p *= 10;
  }
  if (max_val < p) putchar ('0');
  printf ("%" PRIi64 "\n", max_val);
}

int main (void) {
  run();
  return 0;
}