#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int,int> pii;

bool isAns(string s){
  int len = s.length();
  FOR(i,1,len){
    if(s[0] != s[i]) return false;
  }
  return true;
}

char diffColor(char a, char b){
  if((a == 'r' && b == 'g') || (a == 'g' && b == 'r')){
    return 'b';
  }else if((a == 'g' && b == 'b') || (a == 'b' && b == 'g')){
    return 'r';
  }else{
    return 'g';
  }
}

int main(){

  while(true){
    string s;
    cin >> s;
    if(s == "0") break;

    map<string, int> mp;
    mp[s] = 1;
    queue<string> que;

    que.push(s);
    bool found = false;
    while(!que.empty()){
      s = que.front();
      que.pop();

      if(isAns(s)){
        found = true;
        cout << mp[s] - 1 << "\n";
        break;
      }

      int len = s.length();
      REP(i,len-1){
        if(s[i] != s[i+1]){
          string v = s;
          char tmp = diffColor(s[i], s[i+1]);
          v[i] = tmp; v[i+1] = tmp;
          if(mp[v] == 0){
            mp[v] = mp[s] + 1;
            que.push(v);
          }
        }
      }
    }
    if(!found){
      cout << "NA\n";
    }
  }
  return 0;
}

