var fs = require('fs'),
    length = fs.fstatSync(process.stdin.fd).size,
    buffer = new Buffer(length),
    bytesRead = fs.readSync(process.stdin.fd, buffer, 0, length, 0),
    input = buffer.toString('utf8', 0, bytesRead).split('\n');
main();
function main(){
  var i = 0;
  while(true){
    var fieldInfo = input[i].split(' ');
    var width = fieldInfo[0] - 0;//toInt
    var height = fieldInfo[1] - 0;//toInt
                
    // 0 0が入力された
    if(!width && !height){
      break;
    }
                
    var start,
    goal,
    elem = [[],[],[],[],[]],
    c;
    
    for(var y = height; y--;){
      i++;//1行進める
      var ni = input[i];
      for(var x = width; x--;){
        c = ni[x];
        
        if(c == '.'){
          // ドットが一番多いはずなので、先にひっかけてしまう
        }else if(c == '1' || c == '2' || c == '3' || c == '4' || c == '5'){
          // 次に多いはずの属性を潰す
          var e = elem[c - 1];
          e[e.length] = pos(x, y);
        }else if(c == 'S'){
          start = pos(x, y);
        }else{
          goal = pos(x, y);
        }
                
      }
    }
                
    var INF = 1 << 30;
    var bestElement = -1;
    var distance = INF;
                    
    // 属性ごとにループ
    for (var startElem = 0; startElem < 5; startElem++) {
      // DPの初期化 DP[属性][同じ属性内の連番]
      var dp = [[],[],[],[],[]];
      for(var h = 5; h--;){
        var hLength = elem[h].length;
        dp[h].length = hLength;
        var initdph = dp[h];
        for(; hLength--;){
          initdph[hLength] = INF;
        }
      }

      // 変数宣言をまとめると安い（らしい）
      var first = (startElem + 1) % 5,
      idxf,
      firstLength = elem[first].length,
      dpf = dp[first],
      elemf = elem[first],
      now = first,
      dpnw = dpf,
      elnw = elemf,
      nowLength = elnw.length,
      next,
      dpnx,
      elnx,
      nextLength,
      dpnwj,
      elnwj,
      idxj,
      idxk,
      inxl,
      tdist;

      // 最初に選んだパチモンから次に捕まえられる属性番号
      for (idxf = firstLength; idxf--;) {
        // s→e1を計算
        dpf[idxf] = dist(start, elemf[idxf]);
      }
                      
      // s->1->2->3->4->g
      // なので、ループは間の-> * 3分まわす
      for (var e = 0; e < 3; e++) {
        // ex->e(x+1)
        next = (now + 1) % 5;
        dpnx = dp[next];
        elnx = elem[next];
        nextLength = elnx.length;
        for (idxj = 0; idxj < nowLength; idxj++) {
          dpnwj = dpnw[idxj];
          elnwj = elnw[idxj];
          for (idxk = 0; idxk < nextLength; idxk++) {
            dpnx[idxk] = min(
              dpnx[idxk],
              dpnwj + 
                  dist(elnwj, elnx[idxk]));
          }
        }
        now = next;
        dpnw = dpnx;
        elnw = elnx;
        nowLength = elnw.length;
      }
 
      for (idxl = 0; idxl < nextLength; idxl++) {
        // e4->g
        tdist = dpnx[idxl] + dist(elnx[idxl], goal);
                        
        if (tdist < distance) {
          distance = tdist;
          bestElement = startElem + 1;
        }
        
      }
      
    }
    
    if (distance == INF) {
      console.log("NA");
    } else {
      console.log(bestElement + " " + distance);
    }
                
    i++;
  }
  
}
 
function dist(from, to){
  // x座標とy座標の切り出し(11ビット右シフトでxを、2047(下位10ビット1)とのAND演算でyを取り出す)
  return abs((from >> 11) - (to >> 11)) + abs((from & 2047) - (to & 2047));
}
                
function min(a, b){
  // ビット演算で高速化
 return b + ((a - b) & ((a - b) >> 31));// 変数宣言よりもこっちの方が高速
}
                
function pos(a, b){
  // 2^10 = 1024なので、下位10ビットでy座標を、上位ビットでx座標を表す
  return (a << 11) + b;
}
               
function abs(a){
 // ビット演算で高速化
 return (a ^ (a >> 31)) - (a >> 31);// これも
}

Measured memory_bytes: 41052