stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#include<cstdio>

#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){

    scanf("%d %d", &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++){

	scanf(" %c", &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) printf("NA\n");

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



  }

}

// Faster C++ solution for the same problem (measured speedup: 2.755449863555774x):
#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);



  }

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.142950153629858x):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

#define F first

#define S second

using namespace std;

 



typedef pair < int, int > P;

vector < P > p[5];

int sx, sy, gx, gy;

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++){

    c = k?abs(p[t][q].F-p[d][i].F) + abs(p[t][q].S-p[d][i].S):abs(sx-p[d][i].F) + abs(sy-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(cin >> w >> h, w){

 

    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 == '.') continue;

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

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

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

      }

    }

    

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

    P q = P(0, INF);

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

      

      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;

    

  }

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.1507051237486097x):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

#include<cstring>

#define INF 1<<28

#define F first

#define S second

using namespace std;

 



typedef pair < int, int > P;

vector < P > v[5];

int sx, sy, gx, gy;

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



int dist(P a, P b){

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

}



int solve(int cnt, int now, int idx){

  

  if(cnt == 4) return dist(v[now][idx], P(gx, gy));

  if(~memo[cnt][now][idx]) return memo[cnt][now][idx];

  

  int ans = INF, next = (now+1)%5, cost;

  for(int i=0;i<v[next].size();i++){

    cost = cnt?dist(v[now][idx], v[next][i]):dist(P(sx, sy), v[next][i]);

    ans = min(ans, solve(cnt+1, next, i) + cost);

  }

 

  return memo[cnt][now][idx] = ans;

}

 

int main(){

 

  int w, h;

  char c;

 

  while(cin >> w >> h, w){

 

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

     

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

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

	cin >> c;

	if(c == '.') continue;

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

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

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

      }

    }

    

    int a, b = INF;

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

    for(int i=0;i<5;i++) if(b > solve(0,i,0)) a = i+1, b = solve(0,i,0);

    

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

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

    

  }

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.1296677603112164x):
#include<cstdio>

#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(scanf("%d %d", &w, &h), w){

 

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

     

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

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

	scanf(" %c", &c);

	if(c == '.') continue;

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

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

	else 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) puts("NA");

    else printf("%d %d\n", q.F, q.S);

 

  }

}
// Slower C++ solution:
#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;



  }

}

// Faster C++ solution for the same problem (measured speedup: 1.1705675369745385x):
#include<cstdio>

#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 > v[5];

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

 

int dist(P a, P b){

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

}

 

 

int solve(int cnt, int now, int p){

  

  if(cnt == 4) return dist(v[now][p], P(gx, gy));

  if(~memo[cnt][now][p]) return memo[cnt][now][p];

  

  int ans = INF, next = (now+1)%5, cost;

  for(int i=0;i<v[next].size();i++){

    if(!cnt) cost = dist(P(sx, sy), v[next][i]);

    else cost = dist(v[now][p], v[next][i]);

    ans = min(ans, solve(cnt+1, next, i) + cost);

  }

  

  return memo[cnt][now][p] = ans;

}

  

int main(){

  

  int w, h;

  char c;

  

  while(scanf("%d %d", &w, &h), w){

     

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

     

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

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

    scanf(" %c", &c);

    if(c == '.') continue;

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

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

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

      }

    }

     

    int a, b = INF;

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

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

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

    }

  

    b == INF?puts("NA"):printf("%d %d\n", a, b);

  

  }

}
// Slower C++ solution:
#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;



  }

}

// Faster C++ solution for the same problem (measured speedup: 1.1699958672898114x):
#include<cstdio>

#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 > v[5];

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

 

int dist(P a, P b){

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

}

 

 

int solve(int cnt, int now, int p){

  

  if(cnt == 4) return dist(v[now][p], P(gx, gy));

  if(~memo[cnt][now][p]) return memo[cnt][now][p];

  

  int ans = INF, next = (now+1)%5, cost;

  for(int i=0;i<v[next].size();i++){

    if(!cnt) cost = dist(P(sx, sy), v[next][i]);

    else cost = dist(v[now][p], v[next][i]);

    ans = min(ans, solve(cnt+1, next, i) + cost);

  }

  

  return memo[cnt][now][p] = ans;

}

  

int main(){

  

  int w, h;

  char c;

  

  while(scanf("%d %d", &w, &h), w){

     

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

     

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

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

    scanf(" %c", &c);

    if(c == '.') continue;

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

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

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

      }

    }

     

    int a, b = INF;

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

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

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

    }

  

    b == INF?puts("NA"):printf("%d %d\n", a, b);

  

  }

}
// Slower C++ solution:
#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;



  }

}

// Faster C++ solution for the same problem (measured speedup: 1.1699446544274363x):
#include<cstdio>

#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 > v[5];

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

 

int dist(P a, P b){

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

}

 

 

int solve(int cnt, int now, int p){

  

  if(cnt == 4) return dist(v[now][p], P(gx, gy));

  if(~memo[cnt][now][p]) return memo[cnt][now][p];

  

  int ans = INF, next = (now+1)%5, cost;

  for(int i=0;i<v[next].size();i++){

    if(!cnt) cost = dist(P(sx, sy), v[next][i]);

    else cost = dist(v[now][p], v[next][i]);

    ans = min(ans, solve(cnt+1, next, i) + cost);

  }

  

  return memo[cnt][now][p] = ans;

}

  

int main(){

  

  int w, h;

  char c;

  

  while(scanf("%d %d", &w, &h), w){

     

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

     

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

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

    scanf(" %c", &c);

    if(c == '.') continue;

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

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

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

      }

    }

     

    int a, b = INF;

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

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

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

    }

  

    b == INF?puts("NA"):printf("%d %d\n", a, b);

  

  }

}
// Slower C++ solution:
#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;



  }

}

// Faster C++ solution for the same problem (measured speedup: 1.1694459502082628x):
#include<cstdio>

#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 > v[5];

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

 

int dist(P a, P b){

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

}

 

 

int solve(int cnt, int now, int p){

  

  if(cnt == 4) return dist(v[now][p], P(gx, gy));

  if(~memo[cnt][now][p]) return memo[cnt][now][p];

  

  int ans = INF, next = (now+1)%5, cost;

  for(int i=0;i<v[next].size();i++){

    if(!cnt) cost = dist(P(sx, sy), v[next][i]);

    else cost = dist(v[now][p], v[next][i]);

    ans = min(ans, solve(cnt+1, next, i) + cost);

  }

  

  return memo[cnt][now][p] = ans;

}

  

int main(){

  

  int w, h;

  char c;

  

  while(scanf("%d %d", &w, &h), w){

     

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

     

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

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

    scanf(" %c", &c);

    if(c == '.') continue;

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

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

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

      }

    }

     

    int a, b = INF;

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

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

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

    }

  

    b == INF?puts("NA"):printf("%d %d\n", a, b);

  

  }

}