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

#GL0000326. Bội số có tổng chữ số nhỏ nhất (Small Multiple)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Bội số có tổng chữ số nhỏ nhất (Small Multiple)

Small Multiple

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given a positive integer KK, consider all positive multiples of KK, including numbers with arbitrarily many decimal digits. Let S(X)S(X) be the sum of the digits in the usual decimal representation of a positive integer XX.

Determine the minimum possible value of S(X)S(X) among all positive multiples XX of KK. Print only the minimum digit sum, not the multiple itself, which may be far too large to store.

Input

One line containing the integer KK.

Output

Print one integer: the minimum digit sum of a positive multiple of KK.

Subtasks

  • Subtask 1 (20 points): 2≤K≤10002\le K\le1000.
  • Subtask 2 (30 points): 2≤K≤200002\le K\le20000.
  • Subtask 3 (50 points): 2≤K≤1000002\le K\le100000.

Examples

Example 1

Input

6

Output

3

Explanation

12=6×212=6\times2 and S(12)=1+2=3S(12)=1+2=3; no positive multiple of 66 has a smaller digit sum.

Example 2

Input

41

Output

5

Explanation

11111=41×27111111=41\times271 has digit sum 55, and no smaller digit sum is possible.

Example 3

Input

79992

Output

36

Explanation

7999279992 itself is a positive multiple with digit sum 7+9+9+9+2=367+9+9+9+2=36. The remainder-graph shortest path confirms that no smaller sum is achievable.