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: Wrong Answer)

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	k := getNextInt(scanner)
	ans := solve(k)
	fmt.Fprintln(writer, ans)

	writer.Flush()
}

func solve(k int) int {
	edges := make([][2]Edge, k)
	visited := make([]int, k)
	queue := make([][2]int, k)
	for i := 0; i < k; i++ {
		edges[i] = [2]Edge{
			Edge{
				cost: 1,
				i:    (i + 1) % k,
			},
			Edge{
				cost: 0,
				i:    (i * 10) % k,
			},
		}
	}

	l := k/2 - 1
	r := l + 1
	queue[l] = [2]int{
		1, 1,
	}
	visited[1] = 1
	for l != r {
		p := queue[l]
		l = (l + 1 + k) % k
		if p[0] == 0 {
			return p[1]
		}

		for _, edge := range edges[p[0]] {
			if visited[edge.i] == 1 {
				continue
			}
			visited[edge.i] = 1
			if edge.cost == 0 {
				l = (l - 1 + k) % k
				queue[l] = [2]int{
					edge.i,
					p[1],
				}
				continue
			}
			queue[r] = [2]int{
				edge.i,
				p[1] + edge.cost,
			}
			r = (r + 1 + k) % k
		}
	}

	return 0
}

type Edge struct {
	cost, i int
}