#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

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

vector<string> v;

bool emergence(string s){
    for(int i=0; i< v.size(); i++) if(v[i] == s) return true;
    else return false;
}

int main(){
    string s;
    while(cin >> s, s!= "0"){
        v.clear();
        queue<pair<string, int> > que;
        que.push(make_pair(s, 0));
        int ret = 9;
        while(que.size()){
            s = que.front().first; int count = que.front().second; que.pop();

            if(ret < count) continue;
            if(check(s))ret = count;
            if(emergence(s)) continue;
            else v.push_back(s);

            for(int i=0; i< s.size()-1; i++){
                if(s[i] != s[i+1]){
                    string t = s.substr(i, 2);
                    sort(t.begin(),t.end());
                    if(t == "br") t = "gg";
                    else if(t == "bg") t = "rr";
                    else if(t == "gr") t = "bb";
                    que.push(make_pair(s.substr(0, i)+t+s.substr(i+2), count+1));
                }
            }
        }
        if(ret == 9) cout << "NA" << endl;
        else cout << ret << endl;
    }
}