import java.util.*;
import java.io.*;
class Main {
// 1~2단계: 노드 정의 및 정렬 기준 설정
static class Node implements Comparable<Node> {
int rem;
long cost;
Node(int rem, long cost) {
this.rem = rem;
this.cost = cost;
}
@Override
public int compareTo(Node other) {
return Long.compare(this.cost, other.cost);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
// 6단계: 거리 장부(dist) 준비
long[] dist = new long[m];
Arrays.fill(dist, Long.MAX_VALUE);
// 7단계: 시작점 (현재 n의 나머지에서 시작)
dist[n % m] = 0;
// 8단계: 우선순위 큐 가동
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(n % m, 0));
while(!pq.isEmpty()) {
Node cur = pq.poll();
// 이미 더 저렴한 경로를 찾았다면 패스
if (cur.cost > dist[cur.rem]) continue;
// 9단계: 1~9까지 숫자 붙여보기
for (int y = 1; y <= 9; y++) {
// 찬우님 분석대로 m이 20만이라 int 범위 안에서 안전합니다!
int nextRem = (cur.rem * 10 + y) % m;
long nextCost = cur.cost + y;
if (dist[nextRem] > nextCost) {
dist[nextRem] = nextCost;
pq.add(new Node(nextRem, nextCost));
}
}
}
// 10단계: 정답 출력
if (dist[r] == Long.MAX_VALUE) System.out.println("-1");
else System.out.println(dist[r]);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: