#include <iostream>
#include <vector>
#include <climits>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;

typedef pair<int,int> P;

int w, h, sx, sy, gx, gy;
int dp[7][1002];
char t[1002][1002];

vector<P> v[5];

int main(){
  while(cin >> w >> h, w || h){
    for(int i = 0; i < 5; i++){
      v[i].clear();
    }

    for(int i = 0; i < h; i++){
      cin >> t[i];

      for(int j = 0; j < w; j++){
        if(t[i][j] == 'S'){
          t[i][j] = '.';
          sx = j;
          sy = i;
        }
        else if(t[i][j] == 'G'){
          t[i][j] = '.';
          gx = j;
          gy = i;
        }
        else if(isdigit(t[i][j])){
          v[t[i][j] - '1'].push_back(P(j, i));
        }
      }
    }

    int ansCost = INT_MAX;
    int ans = -1;

    for(int start = 0; start < 5; start++){
      memset(dp, -1, sizeof(dp));
      int S = (start + 1) % 5;

      for(int i = 0; i < v[S].size(); i++){
        int dist = abs(sx - v[S][i].first) + abs(sy - v[S][i].second);
        dp[0][i] = dist;
      }

      for(int I = 0; I < 3; I++){
        int NI = I + 1;
        int i = (S + I) % 5;
        int ni = (i + 1) % 5;

        for(int j = 0; j < v[i].size(); j++){
          for(int k = 0; k < v[ni].size(); k++){
            if(dp[I][j] == -1) continue;

            int dist = abs(v[i][j].first - v[ni][k].first) + abs(v[i][j].second - v[ni][k].second);
            int cost = dist + dp[I][j];

            if(dp[NI][k] == -1 || dp[NI][k] > cost){
              dp[NI][k] = cost;
            }
          }
        }
      }

      int idx = (S + 3) % 5;

      for(int i = 0; i < v[idx].size(); i++){
        if(dp[3][i] == -1) continue;
        int cost = dp[3][i] + abs(gx - v[idx][i].first) + abs(gy - v[idx][i].second);
        if(ansCost > cost){
          ansCost = cost;
          ans = start;
        }
      }
    }

    if(ans == -1){
      cout << "NA\n";
    }
    else{
      cout << ans + 1 << " " << ansCost << endl;
    }
  }
}