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;
    var goal;
    var elem = [[],[],[],[],[]];

    var c;
    for(var y = height; y--;){
      i++;//1行進める
      var ni = input[i];
      for(var x = width; x--;){
        c = ni[x];

        switch(c){
        case '.':// ドットが一番多いはずなので、先にひっかけてしまう
          break;
        case '1':
          var e = elem[c - 1];
          e[e.length] = pos(x, y);
          break;
        case '2':
          var e = elem[c - 1];
          e[e.length] = pos(x, y);
          break;
        case '3':
          var e = elem[c - 1];
          e[e.length] = pos(x, y);
          break;
        case '4':
          var e = elem[c - 1];
          e[e.length] = pos(x, y);
          break;
        case '5':
          var e = elem[c - 1];
          e[e.length] = pos(x, y);
          break;
        case 'S':
          start = pos(x, y);
          break;
        default:
          goal = pos(x, y);
        }
            
      }
    }
            
    var INF = 1 << 27;
    var bestElement = -1;
    var distance = INF;
                
    // 属性ごとにループ
    for (var startElem = 0; startElem < 5; startElem++) {
      // DPの初期化 DP[属性][同じ属性内の連番]
      var dp = [[],[],[],[],[]];
      var initi = 5;
      while(initi--){
        var len = elem[initi].length;
        dp[initi].length = len;
        var dpi = dp[initi];
        while(len--){
          dpi[len] = INF;
        }
      }
                  
      // 最初に選んだパチモンから次に捕まえられる属性番号
      var now = (startElem + 1) % 5;
      var elnw = elem[now];
      var nwlen = elnw.length;
      var dpnw = dp[now];
      for (var j = 0; j < nwlen; j++) {
        // s→e1を計算
        dpnw[j] = dist(start, elnw[j]);
      }
                  
      // s->1->2->3->4->g
      // なので、ループは間の-> * 3分まわす
      var next;
      var dpnx;
      var elnx;
      var nxlen;
      for (var e = 0; e < 3; e++) {
        // ex->e(x+1)
        next = (now + 1) % 5;
        dpnx = dp[next];
        elnx = elem[next];
        nxlen = elnx.length;
        for (var j = 0; j < nwlen; j++) {
          var dpnwj = dpnw[j];
          var elnwj = elnw[j];
          for (var k = 0; k < nxlen; k++) {
            dpnx[k] = min(dpnx[k], dpnwj + dist(elnwj, elnx[k]));
          }
        }
        now = next;
        dpnw = dpnx;
        elnw = elnx;
        nwlen = elnw.length;
      }

      for (var j = 0; j < nxlen; j++) {
        // e4->g
        var d = ((dpnx[j] + dist(elnx[j], goal)) << 3) + startElem;

        distance = min(d, distance);
      }

    }

    if (distance === INF) {
      console.log("NA");
    } else {
      console.log(((distance & 7) + 1) + " " + (distance >> 3));
    }

    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){
  // ビット演算で高速化
  var t = a - b;
 return b + (t & (t >> 31));
}

function pos(a, b){
  // 2^10 = 1024なので、下位10ビットでy座標を、上位ビットでx座標を表す
  return (a << 11) + b;
}

function abs(a){
 // ビット演算で高速化
 var mask = a >> 31;
 return (a ^ mask) - mask;
}

var input = '';

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
  input += chunk;
});
process.stdin.on('end', function() {
  input = input.split('\n');
  main();
});

Measured memory_bytes: 51452