#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct Point {
	int x, y;
	Point(int X, int Y) { x = X, y = Y; }
	Point(){}
};

int get_dis(Point a, Point b) {
	return abs(a.x - b.x) + abs(a.y - b.y);
}

int W, H;
Point start, goal;
vector<vector<Point>> pos;
int dp[4][1000];

int main() {
	while (cin >> W >> H, W + H) {
		pos.clear();
		pos.resize(5);
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				char c;
				cin >> c;
				if (c == 'S')start = Point(j, i);
				if (c == 'G')goal = Point(j, i);
				if (c >= '1' && c <= '5') {
					int type = static_cast<int>(c - '1');
					pos[type].push_back(Point(j, i));
				}
			}
		}
		int ans = 1000000000, ans_idx;
		for (int s = 0; s < 5; s++) {
			//????¨??????????????????????????????????
			for (int j = 0; j < pos[s].size(); j++) {
				dp[0][j] = get_dis(start, pos[s][j]);
			}
			for (int i = 1; i < 4; i++) {
				int now_idx = (i + s) % 5;
				for (int j = 0; j < pos[now_idx].size(); j++) {
					dp[i][j] = 1000000000;
					int before_idx = (i + s + 5 - 1) % 5;
					for (int k = 0; k < pos[before_idx].size(); k++) {
						dp[i][j] = min(dp[i][j],
							dp[i - 1][k] + get_dis(pos[before_idx][k], pos[now_idx][j]));
					}
					//cout << s << " " << i << " " << j << " " << dp[i][j] << endl;
				}
			}
			for (int j = 0; j < pos[(s + 3) % 5].size(); j++) {
				if (ans > dp[3][j] + get_dis(pos[(s + 3) % 5][j], goal)) {
					ans = dp[3][j] + get_dis(pos[(s + 3) % 5][j], goal), ans_idx = s;
					if (ans_idx == 0)ans_idx = 5;
				}
			}
		}
		if(ans != 1000000000)cout << ans_idx << " " << ans << endl;
		else cout << "NA" << endl;
	}
	return 0;
}