while(x,y=gets.split.map &:to_i;x>0)
l=(0..6).map{[]}
y.times{|j|i=0;gets.each_char{|c|n="S12345G".index(c);n&&l[n]<<[i,j];i+=1}}
r=1..5
puts(l[r].count{|a|a[0]}<4?:NA:
  r.map{|k|m=l[r].rotate(k)[0,4]<< l[6]
    h={}
    q=[l[0][0]+[0,0]]
    min=1e9
    while q[0]&&(t,u,n,c=q.shift;n<5)
      next if h[[t,u]]
      h[[t,u]]=0
      q.reject!{|i,j|i==t&&j==u}
      z=m[n].reject{|i|h[i]}.map{|i,j|[i,j,n+1,c+(i-t).abs+(j-u).abs]}
      if n>3
        if min>w=z[0][3]
          min=w
          q.select!{|i|i[3]<w}
        else
          z=[]
        end
      end
      q+=z.reject{|i|i[3]>min}
      q.sort_by!{|i|i[3]}
    end
    [k,n>4?c:1e9]
  }.min_by{|i,j|[j,i]}*" ")
end