#include <iostream>
#include <string>
#include <queue>
using namespace std;

string insect = "brg";
string str,str2;
int cnt;
queue <string> Q;


bool bfs( string str) {
  int l = Q.size();
  while ( !Q.empty() ) {
    
    str = Q.front();
    Q.pop();
    l--;
    //cout << "l" << " " << l << endl;
    int len = str.size();
     
    for ( int i = 0; i < len-1; i++ ) {
      str2 = str;
      for ( int j = 0; j < 3; j++ ) {
	if ( str2[i] != str2[i+1] 
	     && str2[i] != insect[j] && str2[i+1] != insect[j] ) {
	  str2[i] = insect[j];
	  str2[i+1] = insect[j];

	  Q.push( str2 );
	  int flag = 0;
	  for ( int k = 0; k < len-1; k++ ) {
	    if ( str2[k] != str2[k+1] ) flag = 1;
	  }
	  // cout << "i" << " " << i << " " << "j" << " " << j << endl;
	  // cout << "cnt" << " " << cnt << endl;
	  // cout << "str2" << " " << str2 << endl;
	  // cout << endl;
	  
	  if ( flag == 0 ) return 1;
	}
      }
    }
    if ( l == 0 ) return 0;
  }
  return 0;
}

int main() {

  while (1) {

    cin >> str;
    int length = str.size();
    int f = 0;
    for ( int i = 0; i < length-1; i++ ) {
      if ( str[i] != str[i+1] ) f = 1;
    }

    if ( str == "0" ) break;
    while( !Q.empty() ) Q.pop();
    cnt = 0;
    Q.push( str );
    while (1) {
    if ( f == 0 ){
      cout << 0 << endl;
      break;
    }
      cnt++;
      if ( cnt == 10 ){
	cout << "NA" << endl;
	break;
      }
      if ( bfs( str ) ) {
	cout <<  cnt << endl;
	break;
      }
    }

  }
  return 0;
}