#P7960. Enhanced Counting Game

    ID: 21144 Type: Default 1000ms 256MiB

Enhanced Counting Game

Enhanced Counting Game

The Enhanced Counting Game is a twist on a popular counting game. In this game, each player in turn calls out a positive integer. However, a number x cannot be called if and only if there exist positive integers y and z such that x = y\cdot z and the decimal representation of y contains the digit 7. Formally, define \[ p(x)=\begin{cases}1,&\text{if the decimal representation of } x \text{ contains } 7,\\0,&\text{otherwise.}\end{cases} \] A positive integer x is not allowed to be called if and only if there exist positive integers y and z with x=y\cdot z and \(p(y)=1\>.

For example:

  • If the previous number reported is 6, then although 6 is allowed to be called, the next number 7 is forbidden (since 7 itself contains the digit 7). Therefore, the next valid number is 8.
  • If the previous number is 33, then numbers 34 (since \(34=17\times2\) and 17 contains 7) and 35 (since \(35=7\times5\)) are invalid, and the next valid number is 36.
  • If the previous number is 69, then all numbers from 70 to 79 are invalid because they all contain the digit 7, making 80 the next number.

Given the number x that the first player (Small r) has just called, Small z wants to determine his next valid number quickly. However, if x itself is not valid (i.e. it cannot be called), then Small r loses the game immediately. Your task is to help Small z by checking the validity of x and, if valid, computing the smallest valid number greater than x to call next.

inputFormat

The input consists of a single integer x (1 ≤ x ≤ 10^9), representing the last number called by Small r.

outputFormat

If x is not allowed to be called according to the game rules, output r lost (without quotes). Otherwise, output the smallest valid integer greater than x that can be called by Small z.

sample

6
8