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

using namespace std;

int main()
{
	char str[256];
	char col[3] = "rgb";
	
	while (scanf("%s", str) && str[0] !='0'){
		map<string, int> minTime;
		queue<string> q;
		string s = str;
		
		minTime[s] = 0;
		
		bool ng = true;
		for (q.push(s); !q.empty(); q.pop()){
			string look = q.front();
			
			bool flag = true;
			for (int i = 0; i < look.size() - 1; i++){
				if (look[i + 1] != look[i]) flag = false;
			}
			
			if (flag){
				printf("%d\n", minTime[look]);
				ng = false;
				break;
			}
			
			for (int c = 0; c < 3; c++){
				for (int i = 0; i < look.size() - 1; i++){
					if (look[i + 1] != col[c] && look[i] != col[c] && look[i + 1] != look[i]){
						string next = look;
						next[i + 1] = next[i] = col[c];
						if (minTime.count(next) > 0) continue;
						minTime[next] = minTime[look] + 1;
						q.push(next);
					}
				}
			}
		}
		if (ng) printf("NA\n");
	}
	
	return (0);
}

C++: #include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define QUEUE_NUM (10192)

struct Data{
	char color[15];
	int cnt;
};

typedef struct {
	int top,bottom;
	Data data[QUEUE_NUM];
}Queue;

//QÉdataðÇÁ
void push(Queue* Q, Data data){
	Q->data[Q->bottom] = data;
	Q->bottom = (Q->bottom +1);
}

//QÌæªvfðæ¾
Data front(Queue* Q)
{
	return Q->data[Q->top];
}

//QÌæªvfðí
void pop(Queue* Q)
{
	Q->top = (Q->top + 1) %QUEUE_NUM;
}

int check(char str[],int n){
	int i,number,tem=1,temp;
	for(i=0;i<n;i++){
		if(str[i]=='r'){
			str[i]='0';
		}
		else if(str[i]=='g'){
			str[i]='1';
		}
		else if(str[i]=='b'){
			str[i]='2';
		}
		else{
			str[i]='\0';
		}
	}
	
	number = atoi(str);
//	puts("\n--in--");
//	printf("%d  ",number);
	number=0;
	while(i!=-1){
		temp = (str[i]-48);
		number += tem*temp;
		tem*=3;
		i--;
	}
//	printf("%d\n",number);
//	puts("--o--");
	return number;
}

int main(void)
{
	while(1){
		int i,j,n,ans=-1;
		char col[4]={'r','g','b'},str[16];
		int temp;
		bool visited[178000]={0};
		Queue Q = {0,0,{0}};
		Data bag;
		
		scanf("%s",bag.color);
		if(bag.color[0]=='0')break;
		n = strlen(bag.color);
		bag.cnt=0;
		push(&Q,bag);
		
		while(!(Q.bottom==Q.top)){
			int flg=0,flag=0;
			Data d = front(&Q); 
			pop(&Q);
			
			
			for(i=0;i<n-1;i++){
				if(d.color[i]!=d.color[i+1])flg=1;
			}
			if(flg==0){
				ans=d.cnt;
				break;
			}
			
			for(i=0;i<n-1;i++){
				if(d.color[i] != d.color[i+1]){
					int used[4]={0};
					Data next;
					flg=0;
					strcpy(next.color ,d.color);
					
					for(j=0;j<3;j++){
						if(col[j]==next.color[i])used[j]=1;
						if(col[j]==next.color[i+1])used[j]=1;
					}
					
					for(j=0;j<3;j++){
						if(used[j]==0){
							next.color[i]=col[j];
							next.color[i+1]=col[j];
						}
					}
					
					/**/
					strcpy(str,next.color);
					temp=check(str,n);
					
					if(visited[temp]==1){
				//		printf("d¡\n");
						continue;
					}
					else {
						visited[temp]=1;
					}
					
					/**/
	//				printf("now%d == %s\n",next.cnt,next.color);
				//	Sleep(100);
					next.cnt = d.cnt+1;
					push(&Q,next);
				}
			}
		}
		if(ans==-1){
			puts("NA");
		}
		else {
			printf("%d\n",ans);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<map>
#include<string>
using namespace std;
typedef pair<string,int> P;
int bfs(string);
int main(){
    string worm;
    while(cin>>worm,worm!="0"){
        int rep=bfs(worm);
        if(rep>=0)cout<<rep<<endl;
        else cout<<"NA"<<endl;
    }
    return 0;
}

int bfs(string worm){
    queue<P>Q;
    map<string,bool>M;
    Q.push(make_pair(worm,0));

    while(Q.size()){
        P p=Q.front();
        Q.pop();
        if(M[p.first])continue;
        M[p.first]=true;
        string str=p.first;
        bool flag=false;
        for(int i=0;i<str.size()-1;i++)
            if(str[i]!=str[i+1])flag=true;

        if(!flag)return p.second;

        for(int i=0;i<str.size()-1;i++){
            string next=str;
            char temp;
            if(next[i]!=next[i+1]){
                if(next[i]!='g'&&next[i+1]!='g')temp='g';
                else if(next[i]!='r'&&next[i+1]!='r')temp='r';
                else temp='b';

                next[i]=next[i+1]=temp;
                Q.push(make_pair(next,p.second+1));
            }
        }


        }

        return -1;



}