Problem:
Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36

Solution in Pascal (FPC 2.6.2) (judge verdict: Wrong Answer)

var n:longint;
    dist:array[0..100050]of longint;
    q:array[0..5000050]of longint;
    v:array[0..100050]of boolean;
procedure spfa;
var  head,tail:longint;
     i,now,y:longint;
begin
  head:=1;
  tail:=0;
  for i:=1 to n-1 do
  dist[i]:=i;
  dist[0]:=1 << 25;
  for i:=0 to n-1 do
  begin
    inc(tail);
    q[tail]:=i;
    v[i]:=true;
  end;
  while head<=tail do
  begin
    now:=q[head];
    for i:=0 to 9 do
    begin
      y:=(now*10+i) mod n;
      if dist[y]>dist[now]+i then
      begin
        dist[y]:=dist[now]+i;
        if not v[y] then
        begin
          v[y]:=true;
          inc(tail);
          q[tail]:=y;
        end;
      end;
    end;
    inc(head);
    v[y]:=false;
  end;
end;
begin
  read(n);
  spfa;
  writeln(dist[0]);
end.