#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

using namespace std;

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long ll;

int main()
{
    map<string, int> worm;
    queue<string> que;
    string chars = "rgb";
    REP(i, 2, 11){
        for (char c : chars) {
            worm[string(i,c)] = 0;
            que.push(string(i,c));
            while (!que.empty()) {
                string s = que.front(); que.pop();
                rep(j, s.length()-1){
                    if (s[j] != s[j+1])
                        continue;
                    rep(k, 3){
                        if (s[j] == chars[k]) {
                            REP(l, k+1, k+3){
                                REP(m, k+1, k+3){
                                    if (l != m) {
                                        string p = s;
                                        p[j] = chars[l%3];
                                        p[j+1] = chars[m%3];
                                        if(!EXIST(worm, p) || worm[p] > worm[s]+1){
                                            que.push(p);
                                            worm[p] = worm[s] + 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    string input;
    while (cin >> input, input != "0") {
        if (EXIST(worm, input))
            cout << worm[input] << endl;
        else
            cout << "NA" << endl;
    }
    return 0;
}