#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
struct data{
	string str;
	int cnt;
	data(){}
	data(string str,int cnt):str(str),cnt(cnt){}
};
string func(string str,int i) {
	int r,g,b;
	char change;
	if(str[i] == 'r') {
		if(str[i+1] == 'g') change='b';
		else if(str[i+1] == 'b') change = 'g';
		else return str;
	}
	else if(str[i] == 'g') {
		if(str[i+1] == 'r') change='b';
		else if(str[i+1] == 'b') change = 'r';
		else return str;
	}
	else {
		if(str[i+1] == 'g') change = 'r';
		else if(str[i+1] == 'r') change = 'g';
		else return str;
	}
	str[i] = change;
	str[i+1] = change;
	return str;
}
bool check(string str) {
	char c = str[0];
	for(int i = 1; i < str.size(); i++) {
		if(str[i] != c) return false;
	}
	return true;
}
int main() {
	string str;
	while(1) {
		cin >> str;
		if(str == "0") break;
		int ans = -1;
		map<string,bool> memo;
		queue<data> Q;
		Q.push(data(str,0));
		while(!Q.empty()) {
			data d = Q.front();Q.pop();
			if(memo[d.str]) continue;
			memo[d.str] = true;
			if(check(d.str)) {
				ans = d.cnt;
				break;
			}
			for(int i = 0; i < d.str.size() - 1; i++) {
				string nstr = func(d.str,i);
				if(nstr == d.str) continue;
				Q.push(data(nstr,d.cnt+1));
			}
		}
		if(ans == -1) puts("NA");
		else printf("%d\n",ans);
	}
	return 0;
}