#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int qqq(int r, int g, int b)
{
	if (r == string::npos)
	{
		if (g == string::npos)return 1;
		if (b == string::npos)return 1;
	}
	if (g== string::npos)
	{
		if (b == string::npos)return 1;
	}
	return 0;

}
string ch(char a, char b){
	if (a == b)return "0";
	if (a == 'r')
	{
		if (b == 'g')return "bb";
		else return "gg";
	}
	if (a == 'g')
	{
		if (b == 'r')return "bb";
		else return "rr";
	}
	if (a == 'b')
	{
		if (b == 'r')return "gg";
		else return "rr";
	}

}
int main()
{
	while (1)
	{
		string musi;
		queue<string> now;
		map<string, int> cou;

		cin >> musi;		if (musi == "0")break;

		cou[musi] = 0;
		now.push(musi);

		while (1)
		{
			string ss = now.front();
			int r, g, b;
			r = ss.find("r", 0);
			g = ss.find("g", 0);
			b = ss.find("b", 0);
			if (qqq(r, g, b)){
				cout << cou[ss] << endl;
				cou.clear();
				while (!now.empty())now.pop();
				break;
			}
			int siz = ss.size();

			for (int i = 0; i < siz - 1; i++)
			{
				if (ch(ss.at(i), ss.at(i + 1)) == "0")continue;
				string hoge=ss;
				hoge.replace(i, 2, ch(ss.at(i), ss.at(i + 1)));
				if (!cou.count(hoge)){
					cou[hoge] = cou[ss] + 1;
					now.push(hoge);
				}
			}
			now.pop();
			if (now.empty()){ cout << "NA" << endl; break; }
		}
	}
	return 0;
}