stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

#define rep(i,n) for(int i = 0 ; i < n ; i++)

struct P{int x,y;P(){}P(int a,int b){x = a , y = b;}};

int operator -(P a, P b){

	return abs(a.x - b.x) + abs(a.y - b.y);

}

int dp[1<<5][5][1000];

#define INF (1<<21)

int main(){	

	int W,H;

	while(cin >> W >> H , W){

		P s , g ;

		vector<P> v[6];

		rep(i,H)rep(j,W){

			char c; cin >> c;

			if(c == 'S') s = P(j,i);

			else if(c == 'G') g = P(j,i);

			else if(c != '.') v[c-'1'].push_back(P(j,i));

		}

		rep(i,1<<5)rep(k,5)rep(j,1000)dp[i][k][j] = INF;

		rep(i,5)rep(j,v[(i+1)%5].size()){

			dp[(1<<i) | (1<<((i+1)%5))][(i+1)%5][j] = s - v[(i+1)%5][j];

		}

		rep(bit,(1<<5)-1)rep(x,5){

			int y = (x+1)%5;

			rep(i,v[x].size()){

				if( dp[bit][x][i] == INF ) break; // <- ツ催適ツ可サ

				rep(j,v[y].size()){

					dp[bit|(1<<y)][y][j] = min( dp[bit|(1<<y)][y][j] , dp[bit][x][i] + (v[x][i]-v[y][j]) );

				}

			}

		}

		int ans = INF , cho = -1;

		rep(i,5) rep( j , v[i].size() ){

			int cst = dp[(1<<5)-1][i][j] + (g - v[i][j]);

			int w = (i+1)%5;

			if(ans >= cst){

				if(ans == cst){

					cho = min(cho, w );

				}else{

					cho = w;

					ans = cst;

				}

			}

		}

		if(ans == INF) cout << "NA" << endl;

		else cout << cho+1 << " " << ans << endl;

		

		

	}

	

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <algorithm>

#include <vector>

#include <queue>

#include <map>

#include <cstring>

#include <cstdio>

#define rep(i,n) for(int i = 0 ; i < n ; i++)

using namespace std;





int W,H;

int sx,sy,gx,gy;

vector< pair<int,int> > e[5];



int memo[1000][1000][5] = {};



int dfs(int x,int y,int c,int r){

	if( memo[x][y][c] != -1 ) return memo[x][y][c];

	if( r == 0 ) return abs(gx-x)+abs(gy-y);

	int g = (c+1)%5;

	int ans = 1e8;

	for(int i = 0 ; i < e[g].size() ; i++){

		ans = min( ans , dfs(e[g][i].first,e[g][i].second,g,r-1) + abs(e[g][i].first-x)+abs(e[g][i].second-y) );

	}

	return memo[x][y][c] = ans;

}

int main(){

	ios_base::sync_with_stdio(false);

	while(cin >> W >> H && W){

		rep(i,5)e[i].clear();

		sx = sy = gx = gy = 1e7;

		

		vector< pair<int,int> > list;

		rep(i,H){

			rep(j,W){

				char c;

				cin >> c;

				if( c == 'S' ) sx = j , sy = i;

				else if( c == 'G' ) gx = j , gy = i;

				else if( c != '.' ) e[c-'1'].push_back(make_pair(j,i)), list.push_back(make_pair(j,i));

			}

		}

		list.push_back(make_pair(sx,sy));

		list.push_back(make_pair(gx,gy));

		int F = !e[0].size() + !e[1].size() + !e[2].size() + !e[3].size() + !e[4].size();

		if( F >= 2){

			cout << "NA" << endl;

			continue;

		}

		pair<int,int> ans = make_pair(1e8,1e8);

		rep(i,5){

			rep(j,list.size()) rep(k,5) memo[list[j].first][list[j].second][k] = -1;

			ans = min(ans,make_pair(dfs(sx,sy,i,4),i));

		}

		if( 1e6 > ans.first ){

			cout << ans.second+1 << " " << ans.first << endl;

		}else{

			cout << "NA" << endl;

		}

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cstring>

#include<queue>

#include<vector>

#include<queue>

#define INF 99999999

using namespace std;

typedef pair<int, int> P;

vector<P> point[7];

int x,y;

int dp[6][1001][6];

char mas[1001][1001];

int cx[6],ans;

int sp,ans1;

int solve(int i , int j, int z);

int main(){

  while(1){

    for(int i = 0; i < 7; i++){

      point[i].clear();

    }

    memset(cx, 0, sizeof(cx));

    ans1 = INF;

    sp = INF;

    cin >> x >> y;

    if(x == 0 && y == 0) break;

    for(int i = 0; i < y; i++) cin >> mas[i];

    for(int i = 0; i < y; i++){ 

      for(int j = 0; j < x; j++){

    if(mas[i][j] == 'S'){

      point[0].push_back(P(j,i));

    }

    if(mas[i][j] == 'G'){

      point[6].push_back(P(j,i));

    }

    if(mas[i][j] >= '1' && mas[i][j] <= '5'){

      point[mas[i][j] - '0'].push_back(P(j,i));

      cx[mas[i][j] - '0']++;

    }

      }

    }

    for(int i = 1; i <= 5; i++){

      rotate(point + 1, point + 2, point + 6);

      rotate(cx + 1, cx + 2, cx + 6);

      memset(dp, -1, sizeof(dp));

      ans = solve(0, 0, 0);

      if(ans < ans1){

    ans1 = ans;

    sp = i;

      }

    }

    if(ans1 == INF) cout << "NA" << endl;

    else cout << sp << " " << ans1 << endl;

  }

}

int solve(int i, int j,int z){

  if(dp[i][j][z] >= 0) return dp[i][j][z];

  if(i == 4){

    return abs(point[6][0].first - point[z][j].first) + abs(point[6][0].second - point[z][j].second);

  }

  int res = INF;

  for(int k = 0; k < cx[z + 1]; k++){

    res = min(abs(point[z + 1][k].first - point[z][j].first) + abs(point[z + 1][k].second - point[z][j].second) + solve(i + 1, k, z + 1),res);

  }

  return dp[i][j][z] = res;

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstring>

#include<cstdio>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

int dx[5][1111], dy[5][1111], dn[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<dn[d];i++){

    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);

    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    scanf("%d %d", &w, &h);

    if(!w && !h) break;



    memset(dn, 0, sizeof(dn));



    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	scanf(" %c", &c);

	if(c == 'S') sx = j, sy = i;

	else if(c == 'G') gx = j, gy = i;

	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) printf("NA\n");

    else printf("%d %d\n", a+1, b);



  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

#define F first

#define S second

using namespace std;



int sx, sy, gx, gy;

typedef pair < int, int > P;

vector < P > p[5];

int memo[5][5][1111];



int dist(int k, P a, P b){

  if(!k) return abs(sx-p[b.F][b.S].F) + abs(sy-p[b.F][b.S].S);

  return abs(p[a.F][a.S].F-p[b.F][b.S].F) + abs(p[a.F][a.S].S-p[b.F][b.S].S);

}



int solve(int k, P q){



  if(k == 4) return abs(p[q.F][q.S].F-gx) + abs(p[q.F][q.S].S-gy);

  if(memo[k][q.F][q.S]) return memo[k][q.F][q.S];



  int ans = INF, d = (q.F+1)%5;

  for(int i=0;i<p[d].size();i++) ans = min(ans, solve(k+1, P(d,i)) + dist(k, P(q.F,q.S), P(d,i)));



  return memo[k][q.F][q.S] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    cin >> w >> h;

    if(!w && !h) break;



    for(int i=0;i<5;i++) p[i].clear();

    

    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	cin >> c;

	if(c == 'S') sx = j, sy = i;

	if(c == 'G') gx = j, gy = i;

	else if(c != '.') p[c-'1'].push_back(P(j,i));

      }

    }

    

    P q = P(0,INF);

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(q.S > solve(0,P(i,0))) q.F = i+1, q.S = solve(0,P(i,0));

    }



    if(q.S == INF) cout << "NA\n";

    else cout << q.F << " " << q.S << endl;



  }

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstring>

#include<cstdio>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

int dx[5][1111], dy[5][1111], dn[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<dn[d];i++){

    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);

    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    scanf("%d %d", &w, &h);

    if(!w && !h) break;



    memset(dn, 0, sizeof(dn));



    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	scanf(" %c", &c);

	if(c == 'S') sx = j, sy = i;

	else if(c == 'G') gx = j, gy = i;

	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) printf("NA\n");

    else printf("%d %d\n", a+1, b);



  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

#define F first

#define S second

using namespace std;



int sx, sy, gx, gy;

typedef pair < int, int > P;

vector < P > p[5];

int memo[5][5][1111];



int solve(int k, P q){



  if(k == 4) return abs(p[q.F][q.S].F-gx) + abs(p[q.F][q.S].S-gy);

  if(memo[k][q.F][q.S]) return memo[k][q.F][q.S];



  int ans = INF, d = (q.F+1)%5, c;

  for(int i=0;i<p[d].size();i++){

    if(!k) c = abs(sx-p[d][i].F) + abs(sy-p[d][i].S);

    else c = abs(p[q.F][q.S].F-p[d][i].F) + abs(p[q.F][q.S].S-p[d][i].S);

    ans = min(ans, solve(k+1, P(d,i))+c);

  }



  return memo[k][q.F][q.S] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    cin >> w >> h;

    if(!w && !h) break;



    for(int i=0;i<5;i++) p[i].clear();

    

    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	cin >> c;

	if(c == 'S') sx = j, sy = i;

	if(c == 'G') gx = j, gy = i;

	else if(c != '.') p[c-'1'].push_back(P(j,i));

      }

    }

    

    P q = P(0,INF);

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(q.S > solve(0,P(i,0))) q.F = i+1, q.S = solve(0,P(i,0));

    }



    if(q.S == INF) cout << "NA\n";

    else cout << q.F << " " << q.S << endl;



  }

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstring>

#include<cstdio>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

int dx[5][1111], dy[5][1111], dn[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<dn[d];i++){

    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);

    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    scanf("%d %d", &w, &h);

    if(!w && !h) break;



    memset(dn, 0, sizeof(dn));



    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	scanf(" %c", &c);

	if(c == 'S') sx = j, sy = i;

	else if(c == 'G') gx = j, gy = i;

	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) printf("NA\n");

    else printf("%d %d\n", a+1, b);



  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

#define F first

#define S second

using namespace std;



int sx, sy, gx, gy;

typedef pair < int, int > P;

vector < P > p[5];

int memo[5][5][1111];



int solve(int k, int t, int q){



  if(k == 4) return abs(p[t][q].F-gx) + abs(p[t][q].S-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, c;

  for(int i=0;i<p[d].size();i++){

    if(!k) c = abs(sx-p[d][i].F) + abs(sy-p[d][i].S);

    else c = abs(p[t][q].F-p[d][i].F) + abs(p[t][q].S-p[d][i].S);

    ans = min(ans, solve(k+1, d, i) + c);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    cin >> w >> h;

    if(!w && !h) break;



    for(int i=0;i<5;i++) p[i].clear();

    

    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	cin >> c;

	if(c == 'S') sx = j, sy = i;

	if(c == 'G') gx = j, gy = i;

	else if(c != '.') p[c-'1'].push_back(P(j,i));

      }

    }

    

    P q = P(0,INF);

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(q.S > solve(0,i,0)) q.F = i+1, q.S = solve(0,i,0);

    }



    if(q.S == INF) cout << "NA\n";

    else cout << q.F << " " << q.S << endl;



  }

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstring>

#include<cstdio>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

int dx[5][1111], dy[5][1111], dn[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<dn[d];i++){

    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);

    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    scanf("%d %d", &w, &h);

    if(!w && !h) break;



    memset(dn, 0, sizeof(dn));



    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	scanf(" %c", &c);

	if(c == 'S') sx = j, sy = i;

	else if(c == 'G') gx = j, gy = i;

	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) printf("NA\n");

    else printf("%d %d\n", a+1, b);



  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

#define F first

#define S second

using namespace std;



int sx, sy, gx, gy;

typedef pair < int, int > P;

vector < P > p[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(p[t][q].F-gx) + abs(p[t][q].S-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<p[d].size();i++){

    if(!k) s = abs(sx-p[d][i].F) + abs(sy-p[d][i].S);

    else s = abs(p[t][q].F-p[d][i].F) + abs(p[t][q].S-p[d][i].S);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    cin >> w >> h;

    if(!w && !h) break;



    for(int i=0;i<5;i++) p[i].clear();

    

    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	cin >> c;

	if(c == 'S') sx = j, sy = i;

	if(c == 'G') gx = j, gy = i;

	else if(c != '.') p[c-'1'].push_back(P(j,i));

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) cout << "NA\n";

    else cout << a+1 << " " << b << endl;



  }

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstring>

#include<cstdio>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

int dx[5][1111], dy[5][1111], dn[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<dn[d];i++){

    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);

    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    scanf("%d %d", &w, &h);

    if(!w && !h) break;



    memset(dn, 0, sizeof(dn));



    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	scanf(" %c", &c);

	if(c == 'S') sx = j, sy = i;

	else if(c == 'G') gx = j, gy = i;

	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) printf("NA\n");

    else printf("%d %d\n", a+1, b);



  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

#define F first

#define S second

using namespace std;



int sx, sy, gx, gy;

typedef pair < int, int > P;

vector < P > p[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(memo[k][t][q]) return memo[k][t][q];

  if(k == 4) return abs(p[t][q].F-gx) + abs(p[t][q].S-gy);



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<p[d].size();i++){

    if(!k) s = abs(sx-p[d][i].F) + abs(sy-p[d][i].S);

    else s = abs(p[t][q].F-p[d][i].F) + abs(p[t][q].S-p[d][i].S);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    cin >> w >> h;

    if(!w && !h) break;



    for(int i=0;i<5;i++) p[i].clear();

    

    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	cin >> c;

	if(c == 'S') sx = j, sy = i;

	if(c == 'G') gx = j, gy = i;

	else if(c != '.') p[c-'1'].push_back(P(j,i));

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) cout << "NA\n";

    else cout << a+1 << " " << b << endl;



  }

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstring>

#include<cstdio>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

int dx[5][1111], dy[5][1111], dn[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<dn[d];i++){

    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);

    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    scanf("%d %d", &w, &h);

    if(!w && !h) break;



    memset(dn, 0, sizeof(dn));



    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	scanf(" %c", &c);

	if(c == 'S') sx = j, sy = i;

	else if(c == 'G') gx = j, gy = i;

	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) printf("NA\n");

    else printf("%d %d\n", a+1, b);



  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

typedef pair < int, int > P;

vector < P > p[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(p[t][q].first-gx) + abs(p[t][q].second-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<p[d].size();i++){

    if(!k) s = abs(sx-p[d][i].first) + abs(sy-p[d][i].second);

    else s = abs(p[t][q].first-p[d][i].first) + abs(p[t][q].second-p[d][i].second);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    cin >> w >> h;

    if(!w && !h) break;



    for(int i=0;i<5;i++) p[i].clear();

    

    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	cin >> c;

	if(c == 'S') sx = j, sy = i;

	if(c == 'G') gx = j, gy = i;

	else if(c != '.') p[c-'1'].push_back(P(j,i));

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) cout << "NA\n";

    else cout << a+1 << " " << b << endl;



  }

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstring>

#include<cstdio>

#define INF 1<<28

using namespace std;



int sx, sy, gx, gy;

int dx[5][1111], dy[5][1111], dn[5];

int memo[5][5][1111];

int a, b;



int solve(int k, int t, int q){



  if(k == 4) return abs(dx[t][q]-gx) + abs(dy[t][q]-gy);

  if(memo[k][t][q]) return memo[k][t][q];



  int ans = INF, d = (t+1)%5, s;

  for(int i=0;i<dn[d];i++){

    if(!k) s = abs(sx-dx[d][i]) + abs(sy-dy[d][i]);

    else s = abs(dx[t][q]-dx[d][i]) + abs(dy[t][q]-dy[d][i]);

    ans = min(ans, solve(k+1, d, i) + s);

  }



  return memo[k][t][q] = ans;

}



int main(){



  int w,h;

  char c;



  while(true){

    scanf("%d %d", &w, &h);

    if(!w && !h) break;



    memset(dn, 0, sizeof(dn));



    for(int i=0;i<h;i++){

      for(int j=0;j<w;j++){

	scanf(" %c", &c);

	if(c == 'S') sx = j, sy = i;

	else if(c == 'G') gx = j, gy = i;

	else if(c != '.') dx[c-'1'][dn[c-'1']] = j, dy[c-'1'][dn[c-'1']] = i, dn[c-'1']++;

      }

    }

    

    b = INF;

    for(int i=0;i<5;i++){

      memset(memo, 0, sizeof(memo));

      if(b > solve(0,i,0)) a = i, b = solve(0,i,0);

    }



    if(b == INF) printf("NA\n");

    else printf("%d %d\n", a+1, b);



  }

}

// Fastest known solution for this problem: