Language: C++





//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>




using namespace std;




//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}




//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}








//container util
//------------------------------------------
#define ALL(a) &#160;(a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())




//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) &#160;FOR(i,0,n)




//IO
//------------------------------------------
#define LF(x) cout << (x) << endl;
#define LFA(a, n) cout << a[0]; FOR(itr, 1, n) {cout << " " << a[itr];} cout << endl;
#define LFP(x, w) cout << setprecision((w)); cout << setiosflags(ios::fixed); cout << (x) << endl;








//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI &#160;= acos(-1.0);
const int INF = (int)1e9;




const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};




//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))












//配列関係（二次配列のときなどに使える）
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define SYOKI_C(s,a) fill((char*)s, (char*)(s + lengthof(s)), a) &#160;
#define SYOKI_I(s,a) fill((int*)s, (int*)(s + lengthof(s)), a) &#160;
#define SYOKI_D(s,a) fill((double*)s, (double*)(s + lengthof(s)), a)&#160;
#define COUNT_I(s,a) count((int*)s, (int*)(s + lengthof(s)), a)
#define COUNT_C(s,a) count((char*)s, (char*)(s + lengthof(s)), a)




int const MAX=1000;








int kyori(pair<int ,int> a, pair<int ,int> b){




	return abs(a.first-b.first)+abs(a.second-b.second);
}




int main(int argc, char const *argv[])
{
	
	vector<int> dp[5];
	vector<pair<int ,int > > p[5];
	int w,h;
	char c;
	pair< int ,int >ss;
	pair< int ,int >gg;
	int ans;
	int ansn;




	while(1){
		cin>>w>>h;




		vector<int> dp[5];
		vector<pair<int ,int > > p[5];
		if(w+h==0) break;




		for(int i1=0;i1<h;i1++){
			for(int i2=0;i2<w;i2++){
				cin>>c;
				if('1'<=c && c<='5'){
					p[c-'1'].PB(MP(i2,i1));
					//cout<<c-'1'<<" &#160;"<<i2<<" "<<i1<<endl;
				}




				else if(c=='S'){
					ss.first=i2;
					ss.second=i1;
				}
				else if(c=='G'){
					gg.first=i2;
					gg.second=i1;
				}
			}




		}




		ans=INF;
		REP(i1,5){




			REP(i3,5){
				dp[i3].clear();
				dp[i3].resize(SZ(p[i3]));
				//cout<<dp[i3].size()<<endl;
				REP(i2,SZ(p[i3])){
					dp[i3][i2]=INF;
				}
			}




			//スタートからの距離
			/*REP(i2,SZ(p[i1])){
				dp[i1][i2]=0;
			}*/




			FOR(i3,i1+1,i1+5){
				int temp=i3%5;
				
				REP(i4,SZ(p[temp])){//目的
					if(i3==(i1+1)){
						dp[temp][i4]=kyori(ss, p[temp][i4]);
					}
					else {
						REP(i5,SZ(p[(i3-1)%5])){//出発
							dp[temp][i4]=min(dp[temp][i4],dp[(i3-1)%5][i5]+kyori(p[(i3-1)%5][i5],p[temp][i4]));
						}
					}
				}
			}




			/*for(int i5=0;i5<5;i5++){
				for(int i3=0;i3<dp[i5].size();i3++){




					cout<<i5<<":"<<dp[i5][i3]<<" &#160;";




				}
				cout<<endl;
			}*/




			int temp2=(i1+4)%5;
			REP(i3,SZ( p[temp2] )){
				if(ans>(dp[temp2][i3] +kyori(gg, p[temp2][i3]))){
					ans=dp[temp2][i3] +kyori(gg, p[temp2][i3]);
					ansn=i1+1;
				}




			}




		}
		if(ans!=INF){
			cout<<ansn<<" "<<ans<<endl;
		}
		else{
			cout<<"NA"<<endl;
		}
	}




	return 0;
}

Compiler output:
a.cc:59:14: error: '#' is not followed by a macro parameter
   59 | #define ALL(a) &#160;(a).begin(),(a).end()
      |              ^
a.cc:74:16: error: '#' is not followed by a macro parameter
   74 | #define REP(i,n) &#160;FOR(i,0,n)
      |                ^
a.cc:95:18: error: stray '#' in program
   95 | const double PI &#160;= acos(-1.0);
      |                  ^
a.cc:123:20: error: '#' is not followed by a macro parameter
  123 | #define SYOKI_C(s,a) fill((char*)s, (char*)(s + lengthof(s)), a) &#160;
      |                    ^
a.cc:124:20: error: '#' is not followed by a macro parameter
  124 | #define SYOKI_I(s,a) fill((int*)s, (int*)(s + lengthof(s)), a) &#160;
      |                    ^
a.cc:125:20: error: '#' is not followed by a macro parameter
  125 | #define SYOKI_D(s,a) fill((double*)s, (double*)(s + lengthof(s)), a)&#160;
      |                    ^
a.cc:95:17: error: expected initializer before '&' token
   95 | const double PI &#160;= acos(-1.0);
      |                 ^
a.cc:95:23: error: expected unqualified-id before '=' token
   95 | const double PI &#160;= acos(-1.0);
      |                       ^
a.cc: In function 'int main(int, const char**)':
a.cc:210:21: error: 'i1' was not declared in this scope; did you mean 'y1'?
  210 |                 REP(i1,5){
      |                     ^~
      |                     y1
a.cc:210:17: error: 'REP' was not declared in this scope
  210 |                 REP(i1,5){
      |                 ^~~