#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

struct Node { int x, y; };

int H, W, sx, sy, gx, gy;
string grid[1000];
vector< Node > nodes[5];
vector< int > dst[5];

int solve(int init_c) {
	for_(i,0,5) dst[i].assign(nodes[i].size(), (int)1e9);
	
	int sc = (init_c + 1) % 5;
	
	for_(i,0,(int)nodes[sc].size()) {
		dst[sc][i] = abs(sx - nodes[sc][i].x) + abs(sy - nodes[sc][i].y);
	}
		
	for_(i,1,4) {
		int c = (init_c + i) % 5;
		int nc = (c + 1) % 5;
		
		int A = nodes[c].size(), B = nodes[nc].size();
		
		for_(j,0,A) {
			int x1 = nodes[c][j].x;
			int y1 = nodes[c][j].y;
			
			for_(k,0,B) {
				int x2 = nodes[nc][k].x;
				int y2 = nodes[nc][k].y;
				
				dst[nc][k] = min(dst[nc][k], dst[c][j] + abs(x1 - x2) + abs(y1 - y2));
			}
		}
	}
	
	int gc = (init_c + 4) % 5;
	int res = (int)1e9, M = nodes[gc].size();
	
	for_(i,0,M) res = min(res, dst[gc][i] + abs(gx - nodes[gc][i].x) + abs(gy - nodes[gc][i].y));
	
	return res;
}

int main() {
	while (cin >> W >> H, W) {
		for_(i,0,5) nodes[i].clear();
		
		for_(i,0,H) {
			cin >> grid[i];
			
			for_(j,0,W) {
				if (grid[i][j] == 'S') { sx = j; sy = i; }
				else if (grid[i][j] == 'G') { gx = j; gy = i; }
				else if (grid[i][j] != '.') nodes[grid[i][j] - '1'].push_back(Node{j, i});
			}
		}
		
		int ansc = 0, mini = (int)1e9;
		
		for_(i,0,5) {
			int res = solve(i);
			
			if (mini > res) {
				mini = res;
				ansc = i;
			}
		}
		
		if (mini < (int)1e9) cout << ansc + 1 << " " << mini << endl;
		else cout << "NA" << endl;
	}
}