#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <set>
#include <climits>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> P;
static const int INF=1<<24;

string s;
int qf;

int foo1(int D[]){
	int res=0;
	for(int i=0;i<s.size();i++){
		res*=10;
		res+=D[i];
	}
	return res;
}
void foo2(int D[],int m){
	rep(j,s.size()){
		D[j]=0;
	}
	for(int i=s.size()-1;i>=0;--i){
		D[i]=m%10;
		m/=10;
		rep(j,s.size()){
		}
	}
}


int a[30];
int tmp[30];
set<unsigned int> vis;

bool check(){
	int t=a[0];
	rep(i,s.size()){
		if(t!=a[i]) return false;
	}
	return true;
}

int main(){
	
	while(cin>>s,s[0]!='0'){
		vis.clear();
		int ans=0;
		queue<int> qu;
		rep(i,s.size()){
			if(s[i]=='r') a[i]=0;
			else if(s[i]=='g') a[i]=1;
			else if(s[i]=='b') a[i]=2;
		}
		if(check()){
			cout<<"0"<<endl;
			continue;
		}
		
		qu.push(foo1(a));
		vis.insert(foo1(a));
		bool f=false;
		bool f1=false;
		int c=0;
		while(!qu.empty()&&!f1){
			c++;
			if(c>50) break;
			f=false;
			ans++;
			int qs=qu.size();
			rep(i,qs){
				qf=qu.front();
				qu.pop();
				foo2(a,qf);
				if(check()){
					cout<<ans-1<<endl;
					f1=true;
					break;
				}
				rep(j,s.size()-1){
					if(a[j]!=a[j+1]){
						rep(k,s.size()){
							tmp[k]=a[k];
						}
						if(a[j]*a[j+1]==2){
							tmp[j]=0;
							tmp[j+1]=0;
						}
						else if(a[j]==2||a[j+1]==2){
							tmp[j]=1;
							tmp[j+1]=1;
						}
						else{
							tmp[j]=2;
							tmp[j+1]=2;
						}
						if(vis.find(foo1(tmp))==vis.end()){
							qu.push(foo1(tmp));
							vis.insert(foo1(tmp));
							f=true;
						}
					}
				}
				if(!f) break;
			}
			if(f1) break;
		}
		if(f1) continue;
		if(!f&&!f1) cout<<"NA"<<endl;
		
	}
	
	return 0;
}