#include <iostream>
#include <string>
#include <queue>
using namespace std;
queue<int> q;
int t[59049];
int INF=100000;
int encode(vector<int> vc){
	int p=0;
	for(int i=0;i<vc.size();i++){
		p=p*3+vc[i];
	}
	return p;
}
vector<int> decode(int p,int n){
	vector<int> vc(n);
	for(int i=0;i<n;i++){
		vc[n-i-1]=p%3;
		p/=3;
	}
	return vc;
}
int main(){
	while(true){
		for(int i=0;i<59049;i++) t[i]=INF;
		string s;
		cin >> s;
		int n=s.size(),start=0,goal=1;
		for(int i=0;i<n;i++) goal*=3;
		goal-=1;
		for(int i=0;i<n;i++){
			if(s[i]=='r') start=start*3+2;
			if(s[i]=='g') start=start*3+1;
			if(s[i]=='b') start=start*3;
		}
		q.push(start);
		t[start]=0;
		int ok=-1;
		while(q.size()){
			int p=q.front();
			if(p==0 || p==goal/2 || p==goal){
				ok=p;
				while(q.size()) q.pop(); 
				break;
			}
			q.pop();
			vector<int> vc=decode(p,n);
			for(int i=0;i<n-1;i++){
				if(vc[i]==vc[i+1]) continue;
				vector<int> vc_c=vc;
				vc_c[i]=vc_c[i+1]=((vc[i]+vc[i+1])*2)%3;
				int hoge=encode(vc_c);
				if(t[hoge]!=INF) continue;
				q.push(hoge);
				t[hoge]=t[p]+1;
			}
		}
		if(ok==-1) cout << "NA" << endl;
		else cout << t[ok] << endl;
	}
	return 0;
}