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