#include <cstdio>
#include <vector>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int H, W; char c[1009][1009];
int main() {
	while (scanf("%d%d", &W, &H), W) {
		for (int i = 0; i < H; i++) scanf("%s", &c[i]);
		vector<vector<pair<int, int> > > v(7);
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (c[i][j] == '.') continue;
				if (c[i][j] == 'S') v[0].push_back(make_pair(i, j));
				else if (c[i][j] == 'G') v[6].push_back(make_pair(i, j));
				else v[c[i][j] - 48].push_back(make_pair(i, j));
			}
		}
		int ret = 999999999, p = -1;
		for (int i = 0; i < 5; i++) {
			vector<int> r = { 0 };
			for (int j = 1; j < 5; j++) r.push_back((i + j) % 5 + 1);
			r.push_back(6);
			vector<vector<int> > dp(6); dp[0] = { 0 };
			for (int j = 1; j < 6; j++) {
				dp[j].resize(v[r[j]].size());
				for (int k = 0; k < dp[j].size(); k++) {
					dp[j][k] = 999999999;
					for (int l = 0; l < dp[j - 1].size(); l++) {
						int dist = abs(v[r[j]][k].first - v[r[j - 1]][l].first) + abs(v[r[j]][k].second - v[r[j - 1]][l].second);
						dp[j][k] = min(dp[j][k], dp[j - 1][l] + dist);
					}
				}
			}
			if (ret > dp[5][0]) {
				ret = dp[5][0];
				p = i + 1;
			}
		}
		if (ret == 999999999) puts("NA");
		else printf("%d %d\n", p, ret);
	}
	return 0;
}