#include <iostream>
#include <set>
#include <queue>
#include <cstring>
using namespace std;

typedef pair<string,int> P;

int n;
set<string> closed;

bool check(string s){
  for(int i = 0; i < n - 1; i++){
    if(s[i] != s[i + 1]){
      return false;
    }
  }
  return true;
}

void solve(string s){
  queue<P> open;
  open.push(P(s, 0));
  closed.clear();
  closed.insert(s);

  while(!open.empty()){
    P p = open.front(); open.pop();
    s = p.first;

    if(check(s)){
      cout << p.second << endl;
      return;
    }

    for(int i = 0; i < n - 1; i++){
      if(s[i] != s[i + 1]){
        string next = s;
        if(s[i] != 'r' && s[i + 1] != 'r') next[i] = next[i + 1] = 'r';
        if(s[i] != 'g' && s[i + 1] != 'g') next[i] = next[i + 1] = 'g';
        if(s[i] != 'b' && s[i + 1] != 'b') next[i] = next[i + 1] = 'b';

        if(closed.find(next) == closed.end()){
          closed.insert(next);
          open.push(P(next, p.second + 1));
        }
      }
    }
  }

  cout << "NA\n";
}

int main(){
  string s;

  while(cin >> s, s != "0"){
    n = s.length();
    solve(s);
  }
}