(h=Hash[q=[[$_.chop,0]]]
puts (while q[0]
s,c=q.shift
break c if s=~/^(r+|g+|b+)$/
(0..s.size-2).map{|i|s[i]!=s[i+1]&&(t=s.dup;t[i,2]="rgb".delete(t[i,2])*2;h[t]||=(q<<[t,c+1];0))}end)||:NA)while gets>?a