#include <cstdio>
#include <string>
#include <utility>
#include <queue>
#include <set>
using namespace std;

typedef pair<int, string> P;

int main()
{
    char worm[32];

    while (scanf("%s", worm), *worm != '0'){
        queue<P> que;
        set<string> exist;

        exist.insert(string(worm));
        for (que.push(P(0, string(worm))); que.size(); que.pop()){
            P tmp = que.front();
            string s = tmp.second;
            bool flag = true;

            for (int i = 0; i < s.length() - 1; i++){
                string t = s;

                if (t[i] != t[i + 1]){
                    flag = false;
                    if (t[i] != 'r' && t[i + 1] != 'r'){
                        t[i] = t[i + 1] = 'r';
                    }
                    else if (t[i] != 'g' && t[i + 1] != 'g'){
                        t[i] = t[i + 1] = 'g';
                    }
                    else {
                        t[i] = t[i + 1] = 'b';
                    }
                    if (exist.find(t) == exist.end()){
                        exist.insert(t);
                        que.push(P(tmp.first + 1, t));
                    }
                }
            }
            if (flag){
                printf("%d\n", tmp.first);
                break;
            }
        }
        if (que.empty()){
            puts("NA");
        }
    }

    return 0;
}