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 Kotlin (1.0.0) (judge verdict: Accepted)

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*


fun main(args: Array<String>) {
    val sc = FastScanner(System.`in`)

    val k = sc.nextInt()

    val vs = Array(k) { Vertex(it) }

    for (x in 1 until k) {
        vs[x].adj.add(Edge(vs[(x * 10) % k], 0))
        vs[x].adj.add(Edge(vs[(x + 1) % k], 1))
    }

    vs[1].dist = 0

    val bfs = PriorityQueue<Vertex>()
    var count = 0

    bfs.add(vs[1])
    while (!bfs.isEmpty()) {
        val v = bfs.poll()
        //if (v.solved) continue


        v.solved = true
        for (edge in v.adj) {
            count += 1
            if (edge.to.dist == -1 || edge.to.dist > v.dist + edge.length) {
                edge.to.dist = v.dist + edge.length
                bfs.add(edge.to)
            }
        }
    }

    println(vs[0].dist + 1)

}


data class Vertex(val x: Int): Comparable<Vertex> {
    override fun compareTo(other: Vertex): Int {
        return this.dist - other.dist
    }

    var dist = -1
    var solved = false
    val adj = mutableListOf<Edge>()
}

class Edge(val to: Vertex, val length: Int)


class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}