#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <set>
#include <queue>
#include <stack>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<string, int>;
using edge = struct
{
    int to;
    int cost;
};

int wfs(string worm)
{
    queue<P> que;
    que.push(P(worm, 0));

    map<string, int> mp;

    while (!que.empty())
    {
        P p = que.front();
        que.pop();

        string tmp = p.first;

        if(mp[p.first]) continue;

        bool flag = true;

        for (int i = 0; i < tmp.size() - 1; i++)
        {
            if (tmp[i] != tmp[i + 1])
            {
                flag = false;
                break;
            }
        }

        if (flag) return p.second;

        mp[p.first] = p.second;

        for(int i = 0; i < tmp.size() - 1; i++)
        {
            if(tmp[i] == tmp[i + 1]) continue;

            string nworm = tmp;

            if(tmp[i] != 'g' && tmp[i + 1] != 'g')
            {
                nworm[i] = 'g';
                nworm[i + 1] = 'g';
            }
            else if(tmp[i] != 'b' && tmp[i + 1] != 'b')
            {
                nworm[i] = 'b';
                nworm[i + 1] = 'b';
            }
            else
            {
                nworm[i] = 'r';
                nworm[i + 1] = 'r';
            }

            que.push(P(nworm, p.second + 1));

        }        
    }
    return -1;
}

int main()
{
    string input;

    while(cin >> input && input != "0")
    {
        cout << (wfs(input) != - 1 ? to_string(wfs(input)) : "NA") << endl;
    }

    return 0;
}
