PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000172. Số Smith (Smith Numbers)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số Smith (Smith Numbers)

Smith Numbers

Phuoc Hung OJ ID: PRIME000177
Original source: UVa 10042

Problem statement

A Smith number is a composite integer whose digit sum equals the sum of the digits of all its prime factors, with multiplicity. Given nn, find the smallest Smith number strictly greater than nn.

Input

One positive integer nn.

Output

Print the smallest Smith number strictly greater than nn.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10 0001\le n\le 10\,000.
  • Subtask 2 (30 points): 10 001≤n≤10 000 00010\,001\le n\le 10\,000\,000.
  • Subtask 3 (50 points): 10 000 001≤n≤999 999 99910\,000\,001\le n\le 999\,999\,999.

The result may be greater than the maximum input value.

Example

Input

4937774

Output

4937775

Explanation: The factorization is 4937775=3⋅5⋅5⋅658374937775=3\cdot5\cdot5\cdot65837. Both digit sums equal 4242, and this is the first Smith number above the input.