#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using namespace std;

const int INF = 1e+9;
typedef pair<int, int> P;

int ans = 0;
int s_size;

string change(string s, int a, int b){
	if((s[a] == 'r' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'r'))
		s[b] = s[a] = 'b';
	else if((s[a] == 'b' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'b'))
		s[b] = s[a] = 'r';
	else
		s[b] = s[a] = 'g';
	
	return s;
}

void bfs(string s, int res){
	if(res >= ans)
		return;
	for(int i = 0; i < s_size - 1; ++i){
		if(s[i] != s[i + 1])
			break;
		if(i == s_size - 2){
			ans = min(res, ans);
			return;
		}
	}
	if(res >= s_size)
		return;
	for(int i = 0; i < s_size - 1; ++i){
		if(s[i] != s[i + 1]){
			string str = change(s, i, i + 1);
			bfs(str, res + 1);
		}
	}
}

int main() {
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	string s;
	while(cin >> s){
		if(s == "0")
			break;
		s_size = s.size();
		ans = INF;
		bfs(s, 0);
		if(ans == INF)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
	return 0;
}