// yuki 927 Second Permutation // 2019.11.24 bal4u #include #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) int a[10+'0']; int main() { int i, c, n, f; do c = gc(), ++a[c]; while (c >= '0'); n = 0; for (i = '0'; i <= '9'; ++i) if (a[i]) ++n; if (n < 2) goto Err; f = 0; for (i = '9'; n > 2; --i) if (a[i]) { while (a[i]) pc(i), f = 1, --a[i]; --n; } while (a[i] == 0) --i; c = i; while (--a[i]) pc(c), f = 1; --i; while (a[i] == 0) --i; if (!f && i == '0') goto Err; pc(i), pc(c); while (--a[i]) pc(i); pc('\n'); return 0; Err: puts("-1"); return 0; }