#include <iostream>
#include <queue>
#include <vector>
#include <string>

using namespace std;

typedef pair<string, int> P;

char keisan(char a, char b) {
	if (a == b) {
		return a;
	} else {
		string abc = "rbg";
		int aa, bb;
		for (int i = 0; i < 3; i++) {
			if (a == abc[i]) {
				aa = i;
			} 
			if (b == abc[i]) {
				bb = i;
			}
		}
		return abc[3-(aa+bb)];
	}
}

bool hantei(string a) {
	for (int i = 1; i < a.size(); i++) {
		if (a[0] != a[i]) {
			return false;
		}
	}
	return true;
}

int main() {
	string a;
	while (cin >> a) {
		if (a[0] == '0') {
			break;
		}
		if (hantei(a)) {
			cout << 0 << endl;
		} else {
			queue<P> q;
			vector<string> hyou;
			hyou.push_back(a);
			bool hantei2 = false;
			for (int i = 0; i < a.size()-1; i++) {
				string c;
				c = a;
				c[i] = keisan(a[i], a[i+1]);
				c[i+1] = keisan(a[i], a[i+1]);
				if (hantei(c)) {
					cout << 1 << endl;
					hantei2 = true;
					break;
				} else {
					P p;
					p.first = c;
					p.second = 2;
					q.push(p);
					hyou.push_back(c);
				}
			}
			while (!q.empty()) {
				if (hantei2) {
					break;
				}
				P p = q.front();
				q.pop();
				string b = p.first;
				for (int i = 0; i < b.size()-1; i++) {
					string c;
					c = b;
					c[i] = keisan(b[i], b[i+1]);
					c[i+1] = keisan(b[i], b[i+1]);
					if (hantei(c)) {
						cout << p.second << endl;
						hantei2 = true;
						break;
					} else {
						bool hantei3 = true;
						for (int i = 0; i < hyou.size(); i++) {
							if (c == hyou[i]) {
								hantei3 = false;
								break;
							}
						}
						if (hantei3) {
							P pp;
							pp.first = c;
							pp.second = p.second+1;
							q.push(pp);
							hyou.push_back(c);
						}
					}
				}
			}
			if (!hantei2) {
				cout << "NA" << endl;
			}
		}
	}
	return 0;
}