#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

int bfs( string str )
{
	const int N = str.size();

	queue< pair< string, int > > que;
	que.push( MP( str, 0 ) );

	set< string > visited;
	visited.insert( str );

	set< char > chars;
	chars.insert( 'r' );
	chars.insert( 'g' );
	chars.insert( 'b' );

	while ( !que.empty() )
	{
		auto cur = que.front().fst;
		auto time = que.front().snd;
		que.pop();

		if ( set<char>( ALL( cur ) ).size() == 1 )
		{
			return time;
		}

		REP( i, 0, N - 1 )
		{
			if ( cur[i] == cur[ i + 1 ] )
			{
				continue;
			}

			auto c = chars;
			c.erase( cur[i] );
			c.erase( cur[ i + 1 ] );

			auto next = cur;
			next[i] = next[ i + 1 ] = *c.begin();

			if ( EXIST( visited, next ) )
			{
				continue;
			}

			que.push( MP( next, time + 1 ) );
			visited.insert( next );
		}
	}

	return -1;
}			

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	while ( true )
	{
		string str;
		cin >> str;

		if ( str == "0" )
		{
			break;
		}

		int res = bfs( str );

		if ( res == -1 )
		{
			cout << "NA";
		}
		else
		{
			cout << res;
		}
		cout << endl;
	}

	return 0;
}