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<queue>
#include<cstdio>
#include<string>
#include<set>

#define R 114
#define G 103
#define B 98

using namespace std;

class DATA{
	public:
		DATA(string temp){			//ÌzñÆzñÌ·³ðnµÄ °Ä­¾³¢
			step = 0;
			arr = temp;
		}
		
		DATA(string temp, int step) :arr(temp), step(step) {}
		
		//I¹»èH
		int check(void){
			char t = arr[0];
			for(int i = 0; i < arr.size(); i++){
				if(arr[i] != t){
					return 0;
				}
			}
			return 1;
		}
		
		int step;		//Xebv(JÚ)
		string arr;	//rgbÌÀÑ
};

set<string> table;

//¦
int ans = -1;

int main()
{
	string rgb;
	string temp;
	int i,j, len;
	int temp3;
	char g;
	
	
	for(;;){
		cin >> rgb;
		if(rgb == "0"){
			break;
		}
		len = rgb.size();
		ans = -1;
		queue<DATA> Q;
		
		DATA start(rgb,0);
		
		Q.push(start);
		while(!Q.empty()){
			//`FbN·éf[^ðæèo·
			DATA d = Q.front();
			Q.pop();
			
			if(table.count(d.arr)){
				continue;
			}
			
			table.insert(d.arr);
			
			//I¹
			if(d.check() == 1){
				ans = d.step;
				break;
			}
			
			//·×ÄÌp^[ÉÎµÄóÔJÚµAL[Éi[
			for(i = 0; i < len-1; i++){
				temp = d.arr;
				if(d.arr[i] != d.arr[i+1]){
					switch(d.arr[i]+d.arr[i+1]){
						case R+B:
							temp[i] = temp[i+1]= G;
							break;
						case R+G:
							temp[i] = temp[i+1] = B;
							break;
						case G+B:
							temp[i] = temp[i+1] = R;
					}
					
					DATA next(temp,d.step+1);
					Q.push(next);
				}
			}
		}
		if(ans == -1){
			cout << "NA" << endl;
		}
		else{
			cout << ans << endl;
		}
	}
}

C++: // 2011/08/21 Tazoe

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

struct state{
	string seg;
	int tim;
};

int main()
{
	while(true){
		struct state wrm;
		cin >> wrm.seg;
		if(wrm.seg=="0")
			break;
		wrm.tim = 0;

		queue<struct state> que;
		que.push(wrm);

		map<string, bool> memo;

		while(true){
			if(que.empty()){
				cout << "NA" << endl;
				break;
			}

			wrm = que.front();
			que.pop();

			if(memo[wrm.seg])
				continue;
			else
				memo[wrm.seg] = true;

			bool flg = true;
			for(int i=1; i<wrm.seg.size(); i++)
				if(wrm.seg[i]!=wrm.seg[0]){
					flg = false;
					break;
				}
			if(flg){
				cout << wrm.tim << endl;
				break;
			}

			struct state tmp;
			for(int i=0; i<wrm.seg.size()-1; i++)
				if(wrm.seg[i]==wrm.seg[i+1])
					continue;
				else if(wrm.seg[i]!='r'&&wrm.seg[i+1]!='r'){
					tmp.seg = wrm.seg;
					tmp.seg[i] = tmp.seg[i+1] = 'r';
					tmp.tim = wrm.tim+1;
					que.push(tmp);
				}
				else if(wrm.seg[i]!='g'&&wrm.seg[i+1]!='g'){
					tmp.seg = wrm.seg;
					tmp.seg[i] = tmp.seg[i+1] = 'g';
					tmp.tim = wrm.tim+1;
					que.push(tmp);
				}
				else if(wrm.seg[i]!='b'&&wrm.seg[i+1]!='b'){
					tmp.seg = wrm.seg;
					tmp.seg[i] = tmp.seg[i+1] = 'b';
					tmp.tim = wrm.tim+1;
					que.push(tmp);
				}
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <string>
#include <utility>
#include <queue>
#include <set>
using namespace std;

typedef pair<int, string> P;

int main()
{
    char worm[32];

    while (scanf("%s", worm), *worm != '0'){
        queue<P> que;
        set<string> exist;

        exist.insert(string(worm));
        for (que.push(P(0, string(worm))); que.size(); que.pop()){
            P tmp = que.front();
            string s = tmp.second;
            bool flag = true;

            for (int i = 0; i < s.length() - 1; i++){
                string t = s;

                if (t[i] != t[i + 1]){
                    flag = false;
                    if (t[i] != 'r' && t[i + 1] != 'r'){
                        t[i] = t[i + 1] = 'r';
                    }
                    else if (t[i] != 'g' && t[i + 1] != 'g'){
                        t[i] = t[i + 1] = 'g';
                    }
                    else {
                        t[i] = t[i + 1] = 'b';
                    }
                    if (exist.find(t) == exist.end()){
                        exist.insert(t);
                        que.push(P(tmp.first + 1, t));
                    }
                }
            }
            if (flag){
                printf("%d\n", tmp.first);
                break;
            }
        }
        if (que.empty()){
            puts("NA");
        }
    }

    return 0;
}