#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;
#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define rep(i,N) FOR(i, 0, N)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).reeal(), (b).real) && EQ((a).imag(), (b).imag()))
#define NIL -1

ll g;
ll w;
ll d[100000];
char table[] = {'r','g','b'};

string myitos(ll num){
    string res = "";
    rep(i,w){
        res += table[num % 3];
        num /= 3;
    }
    reverse(ALL(res));
    return res;
}

ll mystoi(string s){
    ll res = 0;
    rep(i,s.size()){
        res *= 3;
        if(s[i] == 'g')res++;
        else if(s[i] == 'b')res += 2;
    }
    return res;
}

void dijkstra(ll start){
    fill(d,d+100000,INF);
    d[start] = 0;

    priority_queue<P,vector<P>,greater<P>> que;
    que.push(P(0,start));

    while(!que.empty()){
        P p = que.top();
        que.pop();
        ll v = p.second;
        ll cost = p.first;
        if(d[v] < cost)continue;
        // d[v] = cost;
        if(v == 0 || v == g || v == 2*g)continue;

        string now = myitos(v);

        rep(i,now.size() - 1){
            char c1 = now[i];
            char c2 = now[i+1];
            string t = now;
            if((c1 == 'r' && c2 == 'g') || (c1 == 'g' && c2 == 'r')){
                t[i] = 'b';
                t[i+1] = 'b';
            }
            if((c1 == 'g' && c2 == 'b') || (c1 == 'b' && c2 == 'g')){
                t[i] = 'r';
                t[i+1] = 'r';
            }
            if((c1 == 'b' && c2 == 'r') || (c1 == 'r' && c2 == 'b')){
                t[i] = 'g';
                t[i+1] = 'g';
            }
            ll to = mystoi(t);
            // cout << t << endl;
            if(d[to] > cost+1){
                d[to] = cost + 1;
                que.push(P(cost+1,to));
            }
        }

    }
}

string s;

int main(){
    while(cin >> s && s != "0"){
        int start = 0;
        w = s.size();
        g = 0;
        rep(i,s.size()){
            g *= 3;
            g++;
            int num = 0;
            if(s[i] == 'g')num++;
            if(s[i] == 'b')num += 2;
            start = 3*start + num;
        }
        // string t = myitos(start);
        // cout << t << endl;
        // cout << mystoi(t) << endl;
        // continue;
        dijkstra(start);
        ll ans = min(d[0],min(d[g],d[2*g]));
        if(ans == INF)cout << "NA" << endl;
        else cout << ans << endl;
    }
}
