#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;

typedef pair<string,int> psi;

char st[200][200]={0};

int main(void){
	
	A:;
	string str;
	cin>>str;
	
	if(str=="0")return 0;
	
	queue<psi> que;
	que.push(psi(str,0));
	
	map<string,int> memo;
	
	st['r']['b']='g';
	st['b']['r']='g';
	st['r']['g']='b';
	st['g']['r']='b';
	st['g']['b']='r';
	st['b']['g']='r';
	
	int ans = -1;
	while(1){
		if(que.empty()){
			break;
		}
		
		psi u = que.front();
		que.pop();
		
		bool flg = true;
		rep(i,u.first.size()-1)if(u.first[i]!=u.first[i+1])flg=false;
		
		if(flg){
			ans = u.second;
			break;
		}
		
		if(memo[u.first]==1)continue;
		memo[u.first]=1;
		
		rep(i,u.first.size()-1){
			if(u.first[i] == u.first[i+1])continue;
			string copy = u.first;
			char c3 = st[u.first[i]][u.first[i+1]];
			copy[i] = copy[i+1] = c3;
			que.push(psi(copy,u.second+1));
		}
	}
	
	if(ans==-1)printf("NA\n");
	else printf("%d\n",ans);
	
	goto A;
}