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 readLines = 0;
  while(true){
    var fieldInfo = input[readLines].split(' ');
    var width = fieldInfo[0] - 0;
    var height = fieldInfo[1] - 0;
     
    if(!width && !height){
      break;
    }
     
    var start;
    var goal;
    var elem = [[],[],[],[],[]]; 
    var c;
    var rowData;
    var elemCache;
    var y;
    var x;
     
    for(y = height; y--;){
      readLines++;
      rowData = input[readLines];
      for(x = width; x--;){
        c = rowData[x];
         
        if(c == '.'){
          
        }else if(c == '1' || c == '2' || c == '3' || c == '4' || c == '5'){
          
          elemCache = elem[c - 1];
          elemCache[elemCache.length] = pos(x, y);
        }else if(c == 'S'){
          start = pos(x, y);
        }else{
          goal = pos(x, y);
        }
         
      }
    }
     
    
    for(var elemInit = 5; elemInit--;){
      elem[elemInit] = new Int32Array(elem[elemInit]);
    }
     
    
    var INF = 1 << 30;
    var bestElement = -1;
    var currentDisance;
    var nearestDistance = INF;
    var dp; 
    var i; 
    var j;
    var e;
    var len;
    var dpCacheNow; 
    var dpCacheNowi; 
    var dpCacheNext; 
    var elemCacheNow;
    var elemCacheNowi; 
    var elemCacheNext;
    var now;
    var nowLength;
    var next;
    var nextLength;
     
    
    for (var selectElem = 0; selectElem < 5; selectElem++) {
      dp = new Array(5); 
       
      for(i = 5; i--;){
        len  = elem[i].length;
        dpCache = dp[i] = new Int32Array(len); 
        while(len--){
          dpCache[len] = INF;
        }
      }
       
      now = (selectElem + 1) % 5;
      nowLength = elem[now].length;
      dpCacheNow = dp[now];
      elemCacheNow= elem[now];
       
      for (i = 0; i < nowLength; i++) {
        
        dpCacheNow[i] = dist(start, elemCacheNow[i]);
      }
       
      
      
      for (e = 0; e < 3; e++) {
        
        next = (now + 1) % 5;
        dpCacheNext = dp[next];
        elemCacheNext = elem[next];
        nextLength = elemCacheNext.length;
        for (i = 0; i < nowLength; i++) {
          dpCacheNowi = dpCacheNow[i];
          elemCacheNowi = elemCacheNow[i];
           
          if(dpCacheNowi >= nearestDistance){
            
            continue;
          }
           
          for (j = 0; j < nextLength; j++) {
            dpCacheNext[j] = min(dpCacheNext[j], dpCacheNowi + dist(elemCacheNowi, elemCacheNext[j])); 
          }
        }
         
        now = next;
        dpCacheNow = dpCacheNext;
        elemCacheNow = elemCacheNext;
        nowLength = nextLength;
      }
       
      for (i = 0; i < nextLength; i++) {
        
        currentDistance = dpCacheNext[i] + dist(elemCacheNext[i], goal);
         
        if (currentDistance < nearestDistance) {
          nearestDistance = currentDistance;
          bestElement = selectElem;
        }
         
      }
       
    }
     
    if (nearestDistance == INF) {
      console.log("NA");
    } else {
      console.log((bestElement + 1) + " " + nearestDistance);
    }
     
    readLines++;
  }
   
}
 
function dist(from, to){
  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){
  return (a << 11) + b;
}
 
function abs(a){
  return (a ^ (a >> 31)) - (a >> 31);
}

Measured memory_bytes: 43988