### COCI 2007/2008, Contest #4

Your program will be given an integer X. Find the smallest number larger than X consisting of the same digits as X.

### Input

The first line of input contains the integer X (1 ≤ X ≤ 999 999).

The first digit in X will not be a zero.

### Output

Output the result on a single line. If there is no such number, output 0.

`156`

`165`

`330`

`0`

`27711`

### Output

`71127`

Point Value: 5
Time Limit: 1.00s
Memory Limit: 32M

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

• (0/0)
I have a solution that works perfectly with the test cases. I used import itertools and generated all the permutations into one list, then looped through it to find correct answer. It works pretty well, but since I get a traceback error, I'm wondering if it doesn't take this import.

• (0/0)
Sorry about this. This problem's testdata was directly uploaded from the COCI site, and contained carriage returns. I've removed these and rejudged your latest submission, which now gets AC.

• (2/0)
I wanted to avoid using itertools or a similar tool to brute force all the interesting combinations and then pick up the minimum, so I changed my point of view analyzing the input as a string and thinking if a simple swap+sorting could have done the trick.

Well, I did after a night of sleep and learnt something new again: thanks guys for the ever interesting problems :)!

[I hope I am not annoying you, but seeing that you are doing a good work, yet I presume still a hard one, you surely deserve some praise once in a while, ne?]