#P8795. Recovering the Original Number

    ID: 21959 Type: Default 1000ms 256MiB

Recovering the Original Number

Recovering the Original Number

Little Blue has a number (x). In each operation, he chooses a prime (p) such that (p < x) and then repeatedly increases (x) by 1 until (x) becomes a multiple of (p) (if (x) is already a multiple of (p), no change happens). After performing two such operations (possibly with different primes), the final number becomes (n).

Little Q observed the result (n) after these two operations and wonders what the original number (x) could have been. If there are multiple possibilities, he is interested in the smallest possible initial (x); if no valid (x) exists, output (-1).

Formally, let the operation be defined as follows: for a given integer (x) and a prime (p < x), define [ \operatorname{op}(x,p)=\begin{cases} x, & \text{if } p\mid x,\ x+(p-(x\bmod p)), & \text{if } p \nmid x. \end{cases} ] We need to find the smallest (x) (with (x \ge 2)) such that there exist primes (p_1) and (p_2) with (p_1 < x) and (p_2 < \operatorname{op}(x,p_1)) satisfying [ \operatorname{op}(\operatorname{op}(x,p_1),p_2)=n. ] If no such (x) exists, output (-1).

inputFormat

The input consists of a single integer (n) ((1\le n\le 10^5), for example), representing the final number observed after two operations.

outputFormat

Output a single integer: the smallest possible original number (x) satisfying the conditions, or (-1) if no such (x) exists.

sample

7
-1

</p>