stablegradients/pie-gem5-pairs huggingface dataset:
// 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.1293476078450322x):
#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);



  }

}
// 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: 3.111860711943634x):
#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<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: 2.9554258520563668x):
#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<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: 2.953982511700759x):
#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<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: 2.9538532104749518x):
#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<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: 2.9525940918890354x):
#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<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;



  }

}

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



  }

}

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



  }

}