#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-8;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int(a.size()))
#define F first
#define S second
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

void solve(string s){
  set<string> app;

  queue<pair<int,string> > q;
  q.push(mp(0,s));
  while(!q.empty()){
    int cc=q.front().F;
    s=q.front().S;
    q.pop();
    if(app.count(s))continue;
    app.insert(s);
    bool end=true;
    rep(i,SZ(s)-1){
      if(s[i]!=s[i+1]){
        end=false;
        break;
      }
    }

    if(end){
      cout<<cc<<endl;
      return;
    }

    rep(i,SZ(s)-1){
      if(s[i]==s[i+1])continue;
      string t=s;
      char a=min(s[i],s[i+1]);
      char b=max(s[i],s[i+1]);

      if(a!='r' && b!='r')t[i]=t[i+1]='r';
      else if(a!='g' && a!='g')t[i]=t[i+1]='g';
      else t[i]=t[i+1]='b';
      if(app.count(t))continue;
      q.push(mp(cc+1,t));
    }
  }
  cout<<"NA"<<endl;
}

int main(){
  string s;
  while(cin>>s,s!="0")
    solve(s);
}