### 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 = []
  nbrs = 5.times.map{[]}
  start = nil
  goal = nil
  nid = 2

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

  min_dist = MAX_INT
  min_attr = nil

  for sattr in (0..4)
    gattr = (sattr + 4) % 5
    q = [start]
    start[2] = sattr
    visited = {}
    visited[start[3]] = 1

    dists = nid.times.map{MAX_INT}
    dists[0] = 0

    while ! q.empty?
      u = q.min{|a, b| dists[a[3]] <=> dists[b[3]]}
      q.delete u

      if u[2] == gattr
        gd = dists[u[3]] + dist(u, goal)
        if dists[goal[3]] > gd
          dists[goal[3]] = gd
        end
      else
        for v in nbrs[u[2]]
          d = dists[u[3]] + dist(u, v)
          if dists[v[3]] > d
            dists[v[3]] = d
            if visited[v[3]].nil?
              visited[v[3]] = 1
              q << v
            end
          end
        end
      end
    end

    if min_dist > dists[goal[3]]
      min_dist = dists[goal[3]]
      min_attr = sattr
    end
  end

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