#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

typedef pair<int,string> pis;

int solve(string s)
{
	set<string> memo;
	priority_queue<pis,vector<pis>,greater<pis> > pq;
	pq.push(mp(0,s));
	while(!pq.empty()){
		pis cur=pq.top(); pq.pop();
		int curf=cur.first;
		string curs=cur.second;
		if(count(allof(curs),curs[0])==curs.size())
			return curf;
		if(memo.find(curs)!=memo.end())
			continue;
		memo.insert(curs);
		
		rep(i,curs.size()-1){
			char a=curs[i],b=curs[i+1];
			if(a==b)
				continue;
			if(a=='r' && b=='g' || a=='g' && b=='r') curs[i]=curs[i+1]='b';
			if(a=='g' && b=='b' || a=='b' && b=='g') curs[i]=curs[i+1]='r';
			if(a=='b' && b=='r' || a=='r' && b=='b') curs[i]=curs[i+1]='g';
			pq.push(mp(curf+1,curs));
			curs[i]=a;
			curs[i+1]=b;
		}
	}
	return -1;
}

int main()
{
	for(string s;cin>>s,s[0]!='0';){
		int res=solve(s);
		if(res==-1)
			puts("NA");
		else
			cout<<res<<endl;
	}
	
	return 0;
}