#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define MOD 1000000007
#define ALL(a) begin((a)),end((a))
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
typedef vector<edge> edges;
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main()
{
	string s;
	while (cin >> s)
	{
		if (s == "0") break;
		queue<string> que;
		que.push(s);
		map<string,int> t;
		t[s] = 0;
		while (!que.empty())
		{
			string tmp = que.front();
			que.pop();
			bool f = true;
			REP(i, tmp.size())
			{
				if (tmp[i] != tmp[0]) f = false;
			}
			if (f)
			{
				cout << t[tmp] << endl;
				goto end;
			}
			REP(i, tmp.size() - 1)
			{
				if (tmp[i] != tmp[i + 1])
				{
					char a = 'r' + 'g' + 'b' - tmp[i] - tmp[i + 1];
					string tmps = tmp;
					tmps[i] = tmps[i + 1] = a;
					if (t.find(tmps) == t.end())
					{
						t[tmps] = t[tmp] + 1;
						que.push(tmps);
					}
				}
			}
		}
		cout << "NA" << endl;
	end:;
	}
	return 0;
}