Go: package main

import (
    "bufio"
    "os"
    "fmt"
)

var sc = bufio.NewScanner(os.Stdin)

/**************  Cell  *****************/
type Cell struct {
    item interface{}
    next *Cell
    prev *Cell
}

func NewCell(x interface{}, ne *Cell, pr *Cell) *Cell {
    newCp := new(Cell)
    newCp.item = x
    newCp.next = ne
    newCp.prev = pr
    return newCp
}
/************  End Cell  ***************/

/**************  Queue  ****************/
type Queue struct {
    size int
    front *Cell
    rear *Cell
}

func NewQueue() *Queue {
    return new(Queue)
}

func (q *Queue) Push(x interface{}) {
    newNode := NewCell(x, nil, nil)
    if q.size == 0 {
        q.front = newNode
        q.rear = newNode
    } else {
        newNode.prev = q.rear
        q.rear.next = newNode
        q.rear = newNode
    }
    q.size++
}

func (q *Queue) PopLeft() interface{} {
    if q.size == 0 {
        return nil
    } else {
        x := q.front.item
        if q.size == 1 {
            q.front = nil
            q.rear = nil
        } else {
            q.front = q.front.next
            q.front.prev = nil
        }
        q.size--
        return x
    }
}

func (q *Queue) IsEmpty() bool {
    return q.size == 0
}

func (q *Queue) Length() int {
    return q.size
}

type State struct {
    color string
    cnt int
}

func main() {
    for {
        sc.Scan()
        init := sc.Text()
        if init == "0" {
            break
        }
        bfs(init)
    }
}

func bfs(init string) {
    dic := make(map[string]int)
    que := NewQueue()
    que.Push(State{init, 0})
    length := len(init)
    isSolved := false
    for !que.IsEmpty() {
        state := que.PopLeft().(State)
        _, ok := dic[state.color]
        if ok {
            continue
        } else {
            dic[state.color] = state.cnt
        }

        if isSingleColor(state.color) {
            fmt.Println(state.cnt)
            isSolved = true
            break
        }

        for i := 0;i < length - 1; i++ {
            color1, color2 := string(state.color[i]), string(state.color[i + 1])
            if color1 != color2 {
                color3 := other(color1, color2)
                if i <= length - 2 {
                    que.Push(State{state.color[:i] + color3 + state.color[i + 2:], state.cnt + 1})
                } else {
                    que.Push(State{state.color[:i] + color3, state.cnt + 1})
                }
            }
        }
    }
    if !isSolved {
        fmt.Println("NA")
    }
}

func other(color1, color2 string) string {
    if color1 == "r" {
        if color2 == "g" {
            return "bb"
        } else if color2 == "b" {
            return "gg"
        }
    } else if color1 == "g" {
        if color2 == "r" {
            return "bb"
        } else if color2 == "b" {
            return "rr"
        }
    } else if color1 == "b" {
        if color2 == "r" {
            return "gg"
        } else if color2 == "g" {
            return "rr"
        }
    }
    return ""
}

func isSingleColor(color string) bool {
    for _, c := range color {
        if string(c) != string(color[0]) {
            return false
        }
    }
    return true
}

C++: #include <iostream>
#include <set>
#include <map>
#include <queue>
#include <algorithm>

using namespace std;

int change( string target )
{
	int a = 1;
	int sum = 0;

	for( int i = 0;i < target.size();i++ ){
		if( target[i] == 'r' ) target[i] = 0;
		else if( target[i] == 'g' ) target[i] = 1;
		else if( target[i] == 'b' ) target[i] = 2;
		
		sum += target[i] * a;
		a *= 3;
	}

	return sum;
}

		

int bfs( string target )
{
	set<int> history;
	queue<string> data;
	map<string,int> dist;

	data.push( target );
	dist[target] = 0;

	while( !data.empty() ){
		const string u = data.front();
		if( count( u.begin(), u.end(), 'r' ) == u.size() || count( u.begin(), u.end(), 'g' ) == u.size() || count( u.begin(), u.end(), 'b' ) == u.size() ) return dist[u];

		for( int i = 0;i < u.size() - 1;i++ ){
			if( u[i] != u[i + 1] ){
				string hoge = u;
				hoge[i] = hoge[i + 1] = ( 'r' + 'g' + 'b' ) - ( hoge[i] + hoge[i + 1] );
				int piyo = change( hoge );
				if( history.count( piyo ) ) continue;
				history.insert( piyo );
				data.push( hoge );
				dist[hoge] = dist[u] + 1;
			}
		}

		data.pop();
	}
	return -1;

}

int main(int argc, char const* argv[])
{
	string target;

	while( cin >> target && target != "0" ){
		int answer = bfs( target );
		if( answer != -1 ) cout << answer << endl;
		else cout << "NA" << endl;
	}
}

C++: #include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int qqq(int r, int g, int b)
{
	if (r == string::npos)
	{
		if (g == string::npos)return 1;
		if (b == string::npos)return 1;
	}
	if (g== string::npos)
	{
		if (b == string::npos)return 1;
	}
	return 0;

}
string ch(char a, char b){
	if (a == b)return "0";
	if (a == 'r')
	{
		if (b == 'g')return "bb";
		else return "gg";
	}
	if (a == 'g')
	{
		if (b == 'r')return "bb";
		else return "rr";
	}
	if (a == 'b')
	{
		if (b == 'r')return "gg";
		else return "rr";
	}

}
int main()
{
	while (1)
	{
		string musi;
		queue<string> now;
		map<string, int> cou;

		cin >> musi;		if (musi == "0")break;

		cou[musi] = 0;
		now.push(musi);

		while (1)
		{
			string ss = now.front();
			int r, g, b;
			r = ss.find("r", 0);
			g = ss.find("g", 0);
			b = ss.find("b", 0);
			if (qqq(r, g, b)){
				cout << cou[ss] << endl;
				cou.clear();
				while (!now.empty())now.pop();
				break;
			}
			int siz = ss.size();

			for (int i = 0; i < siz - 1; i++)
			{
				if (ch(ss.at(i), ss.at(i + 1)) == "0")continue;
				string hoge=ss;
				hoge.replace(i, 2, ch(ss.at(i), ss.at(i + 1)));
				if (!cou.count(hoge)){
					cou[hoge] = cou[ss] + 1;
					now.push(hoge);
				}
			}
			now.pop();
			if (now.empty()){ cout << "NA" << endl; break; }
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
using namespace std;

typedef pair<string, int> P;

int bfs(string worm){
	queue<P> que;
	que.push(P(worm, 0));

	map<string, int> d;
	
	while(!que.empty()){
		P p = que.front();
		que.pop();

		//重複パターンはcontinue
		if(d[p.first]) continue;

		string str = p.first;

		//全色同じかどうかのチェック
		bool flag = false;
		for(int i=0;i<str.size()-1;i++){
			if(str[i] != str[i+1]){
				flag = true;
				break;
			}
		}
		//同じならreturn
		if(!flag) return p.second;

		//重複パターン判別用
		d[p.first] = p.second;

		for(int i=0;i<str.size()-1;i++){
			//隣り合わせで色が異なるとき
			if(str[i] != str[i+1]){
				char temp;
				string next;

				//tempに含まれない一色を代入
				if(str[i] != 'b' && str[i+1] != 'b'){
					temp = 'b';
				}else if(str[i] != 'g' && str[i+1] != 'g'){
					temp = 'g';
				}else{
					temp = 'r';
				}

				//変化後の順番をnextに詰め込む
				next = str;
				next[i] = next[i+1] = temp;

				//キューにプッシュ
				que.push(P(next, p.second+1));
			}
		}
	}
	return -1;
}

int main(){
	string worm;
	while(cin >> worm && worm != "0"){
		int ret = bfs(worm);
		if(ret>=0) cout << ret << endl;
		else cout << "NA" << endl;
	}
}