#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(a,b) (a=min(a,b))
using namespace std;
typedef pair<int,int> pii;

int id[1010][1010];

inline int dis(pii a, pii b){
  return abs(a.first - b.first) + abs(a.second - b.second);
}

int main(){
  int w,h;
  while(cin >> w >> h, w){
    vector<string> g(h);
    rep(i,h)cin >> g[i];

    pii s,t;
    vector< vector<pii> > c(5);

    int cnt = 2;
    rep(i,h)rep(j,w){
      if(g[i][j] == 'S'){
	s = pii(i,j); id[i][j] = 0;
      }else if(g[i][j] == 'G'){
	t = pii(i,j); id[i][j] = 1;
      }else if(g[i][j] != '.'){
	c[g[i][j]-'1'].push_back(pii(i,j));
	id[i][j] = cnt++;
      }
    }

    pii ans = pii(1e9,1e9);
    rep(i,5){
      vector< vector<pii> > seq(6);
      seq[0].push_back(s);
      rep(j,4)seq[j+1] = c[(i+1+j)%5];
      seq[5].push_back(t);

      vector<int> d(cnt,1e8);
      d[id[s.first][s.second]] = 0;

      rep(j,5){
	for(pii a : seq[j]){
	  for(pii b : seq[j+1]){
	    int cost = d[id[a.first][a.second]] + dis(a,b);
	    chmin(d[id[b.first][b.second]], cost);
	  }
	}
      }

      ans = min(ans, pii(d[id[t.first][t.second]], i+1));
    }
    
    if(ans.first < 1e8){
      cout << ans.second << " " << ans.first << endl;
    }else{
      cout << "NA" << endl;
    }
  }

}