#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

int W, H;
string maps[1000];
int d[1000][1000];

void solve(char st) {
	queue<pii> que;
	REP(i, H) REP(j, W) if (maps[i][j] == st) {
		que.push(pii(i, j));
	}
	
	while (!que.empty()) {
		int y = que.front().fi, x = que.front().se;
		que.pop();
		
		int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
		REP(i, 4) {
			int nx = x + dx[i], ny = y + dy[i];
			if (!(nx >= 0 && nx < W && ny >= 0 && ny < H)) continue;
			if (chmin(d[ny][nx], d[y][x] + 1)) que.push(pii(ny, nx));
		}
	}
}

int main() {
	while (cin >> W >> H, W || H) {
		REP(i, H) cin >> maps[i];
		
		int sx, sy, gx, gy;
		REP(i, H) REP(j, W) {
			if (maps[i][j] == 'S') sx = j, sy = i;
			if (maps[i][j] == 'G') gx = j, gy = i;
		}
		
		pii ans(INF, -1);
		REP(s, 5) {
			fill(d[0], d[H], INF);
			d[sy][sx] = 0;
			solve('S');
			
			REP(t, 4) {
				int next = (s + t + 1) % 5;
				REP(i, H) REP(j, W) if (maps[i][j] != next + '1') d[i][j] = INF;
				solve(next + '1');
			}
			
			chmin(ans, pii(d[gy][gx], s));
		}
		
		if (ans.se == -1) puts("NA");
		else printf("%d %d\n", ans.se + 1, ans.fi);
	}
	return 0;
}