#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int,pair<int,int> > NODE;
int abs(int a){
	return a<0?-a:a;
}
#define DIST first
#define NEXT data[(i+1)%5]
int main(){
	int w,h,r,check;
	char t;
	int ret,ele;
	pair<int,int> s,g;
	while(cin >> w >> h && (w || h) ){
		vector< pair<int,int> >data[5];
		check = 0;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> t;
				if(t=='S')s = make_pair(i,j);
				else if(t=='G')g = make_pair(i,j);
				else if(t!='.'){
					data[t-'1'].push_back(make_pair(i,j));
					check = check | 1<<(t-'1');
				}
			}
		} 
		if(check != 0x1f){
			cout << "NA" << endl;
		}else{
			for(int E=0;E<5;E++){
				vector<NODE> dp(data[E].size(),make_pair(0,make_pair(s.first,s.second)));
				for(int i=0;i<4;i++){
					for(size_t k=0;k<dp.size();k++){
						cout << dp[k].first << " ";
						r = dp[k].first;
						dp[k].first = r + abs(dp[k].second.first - NEXT[0].first) + abs(dp[k].second.second - NEXT[0].second);
						for(size_t j=0;j<data[(E+i+1)%5].size();j++){
							if(dp[k].first > r+abs(dp[k].second.first-NEXT[j].first)+abs(dp[k].second.second-NEXT[j].second) ){
								dp[k].first =  r+abs(dp[k].second.first-NEXT[j].first)+abs(dp[k].second.second-NEXT[j].second);
								dp[k].second = NEXT[j]; 
							}
						}
					}
					cout << endl;
				}
				ret = (1<<21);
				for(size_t k=0;k<dp.size();k++){
					if(ret>dp[k].first + abs(dp[k].second.first-g.first)+abs(dp[k].second.second-g.second)){
						ret = dp[k].first + abs(dp[k].second.first-g.first)+abs(dp[k].second.second-g.second);
						ele = E+1;
					}
				}
			}
			cout << ele << " " << ret << endl;
		}
	}
}
	