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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	stdout     *bufio.Writer
)

func init() {
	readString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readInt()
	}
	return b
}

func readLengthAndSlice() []int {
	n := readInt()
	return readIntSlice(n)
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minUint(a, b uint) uint {
	if a < b {
		return a
	}
	return b
}

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minUint64(a, b uint64) uint64 {
	if a < b {
		return a
	}
	return b
}

func maxInt(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxUint(a, b uint) uint {
	if a < b {
		return b
	}
	return a
}
func maxInt64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxUint64(a, b uint64) uint64 {
	if a < b {
		return b
	}
	return a
}

func absInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absInt64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// readString() string
// readInt() int
// readIntSlice(n int) []int
// readLengthAndSlice() []int

type pair struct {
	val, cost int
}

func (e pair) LessThan(f pair) bool {
	return e.cost < f.cost
}

type priorityQueue []pair

func newPriorityQueue() priorityQueue {
	var a []pair
	a = append(a, pair{})
	return a
}

func (pq priorityQueue) Len() int {
	return len(pq) - 1
}

func (pq *priorityQueue) Push(e pair) {
	a := *pq
	n := len(a)
	a = append(a, e)

	for i, p := n, a.parent(n); p > 0 && a[i].LessThan(a[p]); i, p = p, a.parent(p) {
		a[i], a[p] = a[p], a[i]
	}
	*pq = a
}

func (pq *priorityQueue) Pop() pair {
	a := *pq
	res := a[1]
	n := len(a) - 1
	a[1] = a[n]
	a = a[:n]
	i := 1
	for {
		l := a.left(i)
		r := a.right(i)
		if n <= l {
			break
		}
		if n <= r {
			if a[l].cost < a[i].cost {
				a[i], a[l] = a[l], a[i]
			}
			break
		}
		if a[l].cost < a[r].cost {
			a[i], a[l] = a[l], a[i]
			i = l
		} else {
			a[i], a[r] = a[r], a[i]
			i = r
		}
	}
	*pq = a
	return res
}

func (pq priorityQueue) parent(i int) int {
	return i / 2
}

func (pq priorityQueue) left(i int) int {
	return i * 2
}
func (pq priorityQueue) right(i int) int {
	return i*2 + 1
}

func main() {
	defer stdout.Flush()

	k := readInt()

	//g := make([][]pair, k)
	//for i := range g {
	//	g[i] = make([]pair, 2)
	//	g[i][0] = edge{i, (i + 1) % k, 1}
	//	g[i][1] = edge{i, (i * 10) % k, 0}
	//}

	pq := newPriorityQueue()
	push := func(v, c int) {
		pq.Push(pair{v, c})
	}
	pop := func() (int, int) {
		e := pq.Pop()
		return e.val, e.cost
	}

	ans := make([]int, k)
	for i := range ans {
		ans[i] = math.MaxInt32
	}
	ans[1] = 0
	push(2%k, 1)
	push(10%k, 0)

	for pq.Len() != 0 {
		v, cost := pop()
		if ans[v] <= cost {
			continue
		}
		ans[v] = cost
		push((v+1)%k, cost+1)
		push((v*10)%k, cost)
	}

	println(ans[0] + 1)

}