Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K. The input is given as a single integer K, and the output should be the smallest possible sum of the digits in the decimal notation of a positive multiple of K. The constraints are that K is an integer and 2 ≤ K ≤ 10^5.

# Buggy version:
from heapq import heappush, heappop


def dijkstra(graph: list, n: int, v_s: int, INF: int = float('inf')) -> list:
    # graph[v_from] = [(cost, v_to), ...]
    dist = [INF] * n

    dist[v_s] = 0
    heap = [(0, v_s)]  # heap = [(dist[v], v), ...]
    while heap:
        dist2v, v_from = heappop(heap)
        if dist[v_from] < dist2v:
            continue
        for cost, v_to in graph[v_from]:
            dist_cand = dist2v + cost
            if dist_cand < dist[v_to]:
                dist[v_to] = dist_cand
                heappush(heap, (dist[v_to], v_to))
    return dist


k = int(input())

graph = [[] for _ in range(k)]
for i in range(1, k):
    graph[i - 1].append((1, i + 1))
    graph[i].append((0, 10 * i % k))

dist = dijkstra(graph, k, 1)
ans = dist[0]
print(ans)


# Fixed version:
from heapq import heappush, heappop


def dijkstra(graph: list, n: int, v_s: int, INF: int = float('inf')) -> list:
    # graph[v_from] = [(cost, v_to), ...]
    dist = [INF] * n

    dist[v_s] = 0
    heap = [(0, v_s)]  # heap = [(dist[v], v), ...]
    while heap:
        dist2v, v_from = heappop(heap)
        if dist[v_from] < dist2v:
            continue
        for cost, v_to in graph[v_from]:
            dist_cand = dist2v + cost
            if dist_cand < dist[v_to]:
                dist[v_to] = dist_cand
                heappush(heap, (dist[v_to], v_to))
    return dist


k = int(input())

graph = [[] for _ in range(k)]
for i in range(1, k):
    graph[i].append((1, (i + 1) % k))
    graph[i].append((0, 10 * i % k))

dist = dijkstra(graph, k, 1)
ans = dist[0] + 1
print(ans)

# Buggy version:
import heapq
def main():
    k=int(input())
    brdict = {}
    for i in range(1,k):
        if i==k-1:brdict[i] = [(0,1)]
        else:
            brdict[i] = [((i*10)%k,0)]
            if (i*10)%k!=i+1:brdict[i] += [(i+1,1)]
    start=1
    cost=[i for i in range(k)]
    cost[0]=k
    fixed=[False]*k
    fixed[start]=True
    akouho = [(1,start)]
    heapq.heapify(akouho)
    while not all(fixed):
        (fnode,fcost) = heapq.heappop(akouho)
        if fnode==0:break
        fixed[fnode]=True
        for br in brdict[fnode]:
            if not fixed[br[0]]:
                if cost[br[0]] > fcost + br[1]:
                    cost[br[0]] = fcost + br[1]
                    heapq.heappush(akouho,(br[0],cost[br[0]]))
    print(cost[0])
if __name__ == '__main__':
    main()

# Fixed version:
import heapq
def main():
    k= int(input())
    brdict = {}
    for i in range(1,k):
        if i==k-1:
            if (10*(k-1))%k==0:
                brdict[i]= [(0,0)]
            else:
                brdict[i] = [(0,1)]
        else:
            if i!=(i*10)%k:
                brdict[i] = [((i*10)%k,0)]
            if (i*10)%k!=i+1:
                brdict[i] = brdict.get(i,[])+[(i+1,1)]
    start=1
    cost=[i for i in range(k)]
    cost[0]=k
    fixed=[False]*k
    fixed[start]=True
    akouho = [(1,start)]
    heapq.heapify(akouho)
    while not all(fixed):
        (fcost,fnode) = heapq.heappop(akouho)
        if fnode==0:break
        fixed[fnode]=True
        for br in brdict[fnode]:
            if not fixed[br[0]]:
                if cost[br[0]] > fcost + br[1]:
                    cost[br[0]] = fcost + br[1]
                    heapq.heappush(akouho,(cost[br[0]],br[0]))
        if akouho==[]:break
    print(cost[0])
if __name__ == '__main__':
    main()

# Buggy version:
import collections

N = int(input())

q = collections.deque([(1, 0)])
v = {1}

while len(q) > 0:
    c, p = deque.popleft()
    v.add(c)
    if c == 0:
        break

    n1, n10 = (c + 1) % N, (c * 10) % n
    if not n1 in v:
        q.append((n1, p + 1))
    if not n10 in v:
        q.appendleft((n10, p))

print(p + 1)


# Fixed version:
import collections

K = int(input())

q = collections.deque([(1, 0)])
v = {1}

while len(q) > 0:
    c, p = q.popleft()
    v.add(c)
    if c == 0:
        break

    n1, n10 = (c + 1) % K, (c * 10) % K
    if not n1 in v:
        q.append((n1, p + 1))
    if not n10 in v:
        q.appendleft((n10, p))

print(p + 1)

# Buggy version:
import heapq


def small_multiple(k):
    # kの倍数で各桁の総数が最も小さくなるような、各桁の総数
    dist = [float("inf")] * k
    q = []
    # kで割った余りが1になるような最小の数は1
    heapq.heappush(q, (1, 1))

    while q:
        n, c = heapq.heappop(q)
        if dist[n] <= c:
            continue
        print(n, c)
        dist[n] = c

        if dist[(n+1) % k] > c + 1:
            heapq.heappush(q, ((n + 1) % k, c + 1))
        if dist[(n*10) % k] > c:
            heapq.heappush(q, ((n * 10) % k, c))

    print(dist[0])


def main():
	k = int(input())
    small_multiple(k)


if __name__ == "__main__":
    main()

# Fixed version:
import heapq


def small_multiple(k):
    # kの倍数で各桁の総数が最も小さくなるような、各桁の総数
    dist = [float("inf")] * k
    q = []
    # kで割った余りが1になるような最小の数は1
    heapq.heappush(q, (1, 1))

    while q:
        n, c = heapq.heappop(q)
        if dist[n] <= c:
            continue
        dist[n] = c

        if dist[(n + 1) % k] > c + 1:
            heapq.heappush(q, ((n + 1) % k, c + 1))
        if dist[(n * 10) % k] > c:
            heapq.heappush(q, ((n * 10) % k, c))

    print(dist[0])


def main():
    k = int(input())
    small_multiple(k)


if __name__ == "__main__":
    main()

# Buggy version:
from collections import deque
K = int(input())
E = [[(i*10)%K,(i+1)%K] for i in range(K)] # E[i][0] がcost 0, E[i][1]がcost 1
V = [-1] * (K)
V[1] = 1
q = deque([1])
i = 1
while V[0] == -1:
    i = q.popleft()
    q.appendleft(E[i][0])
    V[E[i][0]] = V[i]
    if V[E[i][1]] == -1:
        q.append(E[i][1])
        V[E[i][1]] = V[i]+1
print(V[0])

# Fixed version:
from collections import deque
K = int(input())
E = [[(i*10)%K,(i+1)%K] for i in range(K)] # E[i][0] がcost 0, E[i][1]がcost 1
INF = K*10
V = [INF] * (K)
V[1] = 1
q = deque([1])
i = 1
while q:
    i = q.popleft()
    if V[E[i][0]] > V[i]:
        q.appendleft(E[i][0])
        V[E[i][0]] = V[i]
    if V[E[i][1]] > V[i]+1:
        q.append(E[i][1])
        V[E[i][1]] = V[i]+1
print(V[0])

# Buggy version:
from collections import deque
#q = deque()
q= []
m = {}
K = int(input())
q.append((1,1))
while len(q):
  #n,s = q.popleft()
  n,s = q.pop(0)
  if n in m:
    continue
  m[n] = s
  #q.appendleft((n*10%K,s))
  q.insert(0,(n*10%K,s))
  #q.append(((n+1)%K,s+1))
  q.append(((n+1)%K,s+1))
print(m[0])
print(m)

# Fixed version:
from collections import deque
q = deque()
K = int(input())
q.append((1, 1))
m = {}
while len(q):
 n, s = q.popleft()
 if n in m:continue
 m[n] = s
 q.appendleft((n * 10 % K, s))
 q.append(((n + 1) % K, s + 1))
print(m[0])

# Buggy version:
import math
from collections import defaultdict,deque

INF=10**12+1
a=input()
a=int(a)
graph = defaultdict(list)
for i in range(a):
    graph[i].append((1,(i+1)%a))
    graph[i].append((0,(i*10)%a))
costs=[INF]*a
costs[1]=1
que = [] #後ろの方が優先度高いqueue
que.append((0,1))
while len(que)!=0:
    temp=que.pop()
    now=temp[1]
    v_now=graph[now]
    for c,n in v_now: #v nowから出てる総てのノードについて操作する c→ノードのコスト n→行き先
        if costs[n]>c+costs[now]: #改善したのでそのvは走査の余地あり
            costs[n]=c+costs[now]
            que.append((c,n))
    que=sorted(que,reverse=True)
print(costs[0])
        

# Fixed version:
import math
from collections import defaultdict,deque
import numpy as np
import heapq
INF=10**12+1
a=input()
a=int(a)
graph = defaultdict(list)
for i in range(a):
    graph[i].append((1,(i+1)%a))
    graph[i].append((0,(i*10)%a))
costs=[INF]*a
costs[1]=1
que = [] #後ろの方が優先度高いqueue
que.append((0,1))
while len(que)!=0:
    temp=heapq.heappop(que)
    now=temp[1]
    v_now=graph[now]
    for c,n in v_now: #v nowから出てる総てのノードについて操作する c→ノードのコスト n→行き先
        if costs[n]>c+costs[now]: #改善したのでそのvは走査の余地あり
            costs[n]=c+costs[now]
            heapq.heappush(que,(c,n))
            
print(costs[0])