#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int a[11];
int main()
{
	ios_base::sync_with_stdio(0);
	string s;
	while (cin >> s, s != "0") {
		int n = s.size();
		rep(i, n) switch (s[i]) {
			case 'r': a[i] = 0; break;
			case 'g': a[i] = 1; break;
			case 'b': a[i] = 2; break;
		}
		int x = 0;
		rep(i, n) x = x * 3 + a[n-1-i];

		queue<int> q;
		map<int, int> m;
		q.push(x);
		m[x] = 0;
		int res = -1;
		while (!q.empty()) {
			int p = q.front(); q.pop();
			int t = p, d = p % 3;
			bool ok = true;
			rep(i, n) { a[i] = t % 3; t /= 3; ok = (ok && (a[i] == d)); }
			if (ok) { res = m[p]; break; }

			rep(i, n-1) if (a[i] != a[i+1]) {
				int t1 = a[i], t2 = a[i+1];
				a[i] = a[i+1] = 3 - t1 - t2;
				int y = 0;
				rep(j, n) y = y * 3 + a[n-1-j];
				if (!contains(m, y)) { q.push(y); m[y] = m[p] + 1; }
				a[i] = t1, a[i+1] = t2;
			}
		}
		if (res >= 0) cout << res << endl;
		else cout << "NA" << endl;
	}
	return 0;
}