### constant

MAX_INT = (1 << 31) - 1

### subroutines

def dist(nd0, nd1)
  (nd0[0] - nd1[0]).abs + (nd0[1] - nd1[1]).abs
end

### main

while true
  w, h = gets.strip.split(' ').map{|s| s.to_i}
  break if w == 0 && h == 0

  nodes = 5.times.map{[]}
  start = nil
  goal = nil

  for y in (0...h)
    hl = gets.strip.split('')
    for x in (0...w)
      case hl[x]
      when 'S'
        start = [x, y, -1]
      when 'G'
        goal = [x, y, -1]
      when '1'..'5'
        attr = hl[x].to_i - 1
        nodes[attr] << [x, y, attr]
      end
    end
  end
  #p nodes

  min_dist = MAX_INT
  min_attr = nil

  for sattr in (0..4)
    prev_nodes = [start]
    prev_dists = [0]

    for k in (1..4)
      attr0 = (sattr + k) % 5
      nodes0 = nodes[attr0]
      dists0 = []

      for nd in nodes0
        min_dist0 = MAX_INT
        pnlen = prev_nodes.length
        for pi in (0...pnlen)
          d = prev_dists[pi] + dist(prev_nodes[pi], nd)
          min_dist0 = d if min_dist0 > d
        end
        dists0 << min_dist0
      end

      prev_nodes = nodes0
      prev_dists = dists0
    end

    min_gdist = MAX_INT
    pnlen = prev_nodes.length

    for pi in (0...pnlen)
      d = prev_dists[pi] + dist(prev_nodes[pi], goal)
      min_gdist = d if min_gdist > d
    end

    if min_dist > min_gdist
      min_dist = min_gdist
      min_attr = sattr
    end
  end

  if min_attr.nil?
    puts 'NA'
  else
    puts [min_attr + 1, min_dist].join(' ')
  end
end