#include<iostream>
#include<string>
#include<queue>
#include<set>
#include<utility>
using namespace std;
char C(char a,char b)
{
	if(a!='r'&&b!='r')return'r';
	if(a!='g'&&b!='g')return'g';
	return'b';
}
int F(string s)
{
	int i,b;
	string n;
	set<string> h;
	typedef pair<string,int>T;
	queue<T> q;	
	T p;
	q.push(make_pair(s,0));
	while(!q.empty())
	{
		p=q.front();q.pop();
		s=p.first;
		if(!h.insert(s).second)continue;
		for(b=1,i=0;i+1<s.size();++i)
			if(s[i]!=s[i+1])b=0;
		if(b) return p.second;

		for(i=0;i+1<s.size();++i)
		{
			if(s[i]!=s[i+1])
			{
				n=s;
				n[i]=n[i+1]=C(n[i],n[i+1]);
				q.push(make_pair(n,p.second+1));
			}
		}
	}
	return -1;
}
int main()
{
	string s;
	int n;
	while(cin>>s,s[0]-'0')
		if((n=F(s))<0) cout<<"NA\n";
		else cout<<n<<endl;
	return 0;
}