#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef pair<string, int> bug;
typedef map<string, int> closeMap;

int isOK(string str) {
    // cout << "str:" << str << " " << str.size() << endl;
    for ( int i = 0; i < str.size(); i++ ) {
        if ( str[0] != str[i] ) return 0;
    }
    return 1;
}

char cc(char a, char b) {
    // 並び替え
    if ( b < a ) {
        char tmp = a;
        a = b;
        b = tmp;
    }

    if ( a == 'b' && b == 'g' ) {
        return 'r';
    } else if ( a == 'b' && b == 'r' ) {
        return 'g';
    } else {
        return 'b';
    }
}

void wfs(string str) {
    int cnt = -1;
    string first = str;
    queue<bug> open;
    closeMap close;

    // 初期状態をプッシュ
    open.push( make_pair(str, 0) );

    close.insert( closeMap::value_type(str, 1) );
    // cout << str << " key:" << close[str] << endl;

    while(!open.empty()) {
        bug b = open.front(); open.pop();
        string cs = b.first;

        if ( isOK(cs) ) {
            cnt = b.second;
            break;
        }

        int c = 0;
        for ( int i = 1; i < cs.size(); i++ ) {
            if ( cs[i-1] != cs[i] ) {
                string tmp = cs;
                char nc = cc(cs[i-1], cs[i]);
                tmp[i-1] = nc;
                tmp[i] = nc;

                if ( close[tmp] )continue;
                open.push( make_pair(tmp, b.second+1) );
                close[tmp] = 1;
                c++;
            }
        }
    }

    if ( cnt == -1 ) {
        cout << "NA" << endl;
    } else {
        cout << cnt << endl;
    }


}

int main() {
    string n;

    while ( cin >> n, n != "0" ) {
        wfs(n);
    }

    return 0;
}