Language: C++


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	private void run() throws IOException {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			int w = scanner.nextInt();
			int h = scanner.nextInt();
			if ((w | h) == 0)
				break;
			list = new ArrayList<List<Pair>>();
			for (int i = 0; i < 6; i++) {
				list.add(new ArrayList<Pair>());
			}
			for (int i = 0; i < h; i++) {
				char[] cs = scanner.next().toCharArray();
				for (int j = 0; j < w; j++) {
					if (cs[j] == 'S') {
						sy = i;
						sx = j;
					} else if (cs[j] == 'G') {
						gy = i;
						gx = j;
					} else if (cs[j] != '.') {
						list.get(Character.getNumericValue(cs[j])).add(
								new Pair(i, j));
					}
				}
			}
			min = Integer.MAX_VALUE;
			int index = -1;
			for (int i = 1; i < 6; i++) {
				int cos = slove(i, i == 1 ? 5 : i - 1);
				if (min > cos) {
					min = cos;
					index = i;
				}
			}
			System.out.println(index == -1 ? "NA" : index + " " + min);

		}

	}

	private int slove(int k, int g) {
		PriorityQueue<Pair> pq = new PriorityQueue<>();
		int s = k == 5 ? 1 : k + 1;
		for (int i = 0; i < list.get(s).size(); i++) {
			int y = list.get(s).get(i).y;
			int x = list.get(s).get(i).x;
			int dis = Math.abs(y - sy) + Math.abs(x - sx);
			pq.add(new Pair(y, x, dis, k + 1));
		}
		while (!pq.isEmpty()) {
			Pair pair = pq.poll();
			int y = pair.y;
			int x = pair.x;
			int cost = pair.cost;
			int id = pair.id;
			int tmpid = id > 5 ? id - 5 : id;
			if(min<=cost)
				return Integer.MAX_VALUE;
			if (id == 10) {
				return cost;
			}
			if (tmpid != g) {
				for (Pair i : list.get(tmpid == 5 ? 1 : tmpid + 1)) {
					int dis = Math.abs(y - i.y) + Math.abs(x - i.x);
					pq.add(new Pair(i.y, i.x, cost + dis, id + 1));
				}
			} else {
				int dis = Math.abs(y - gy) + Math.abs(x - gx);
				pq.add(new Pair(0, 0, cost + dis, 10));
			}
		}
		return Integer.MAX_VALUE;
	}

	List<List<Pair>> list;
	int sy;
	int gy;
	int sx;
	int gx;
	int min;

	class Pair implements Comparable<Pair> {
		int y;
		int x;
		int cost;
		int id;

		@Override
		public int compareTo(Pair o) {
			if (this.cost == o.cost)
				return o.id - this.id;
			return this.cost - o.cost;
		}

		public Pair(int y, int x) {
			super();
			this.y = y;
			this.x = x;
		}

		public Pair(int y, int x, int cost, int id) {
			super();
			this.y = y;
			this.x = x;
			this.cost = cost;
			this.id = id;
		}

	}
}

Compiler output:
a.cc:102:17: error: stray '@' in program
  102 |                 @Override
      |                 ^
a.cc:2:1: error: 'import' does not name a type
    2 | import java.io.IOException;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.util.ArrayList;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:4:1: error: 'import' does not name a type
    4 | import java.util.List;
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: 'import' does not name a type
    5 | import java.util.PriorityQueue;
      | ^~~~~~
a.cc:5:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:6:1: error: 'import' does not name a type
    6 | import java.util.Scanner;
      | ^~~~~~
a.cc:6:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:8:1: error: expected unqualified-id before 'public'
    8 | public class Main {
      | ^~~~~~