Олимпиадный тренинг

Задача 34848. Task from the exam


Задача

Темы:
Andrey was preparing for the Unified State Exam in computer science and met the following task in the demo version of the Unified State Examination in 2015:
The machine receives a four-digit number as input. Based on this number, a new number is built according to the following rules.
1. Add the first and second, as well as the third and fourth digits of the original number.
2. The resulting two numbers are written one after the other in descending order (without separators).
Example. Initial number: 3165. Sums: 3+1 = 4; 6+5 = 11. Result: 114.
Specify the smallest number, as a result of which the machine will return the number 1311.

Andrei decided that for self-testing he would write a program that solves a similar problem. We think you can do it too.
The program receives as input some natural number N, which can contain two, three or four digits.

The program should print such a smallest4-digit integer number K, after applying the algorithm described above to it, the number N is obtained. If such a number does not exist, the program should print the number 0.
 
Input Output
1311 2949