#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef pair<int, string> PS;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

char color(char s, char l){
	if(s + l == 'r' + 'g') return 'b';
	if(s + l == 'r' + 'b') return 'g';
	if(s + l == 'b' + 'g') return 'r';
	return 0;
}

int main(void){
	
	string S;
	while(cin >> S){
		if(S == "0")break;
		int str_size = S.size();
		string R, G, B;
		rep(i, str_size){
			R += 'r';
			G += 'g';
			B += 'b';
		}
		bool flag = 0;
		queue<PS> que;
		que.push(PS(0, S));
		map<string, bool> state;
		while(que.size()){
			PS p = que.front(); que.pop();
			string str = p.second;
			
			if(str == R || str == G || str == B){
				flag = 1;
				cout << p.first << endl;
				break;
			}

			if(state[str]){
				continue;
			}
			state[str] = 1;

			rep(i, str.size()-1){
				if(str[i] != str[i+1]){
					char L = color(str[i], str[i+1]);
					string SS = str;
					SS[i] = L;
					SS[i+1] = L;
					que.push(PS(p.first+1,SS));
				}
			}
		}
		if(!flag) cout << "NA" << endl;
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}