#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 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 complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
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; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///


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

	return 0;
}