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 <string>
using namespace std;

struct worm{
	int c;
	string s;
};
queue<worm> que;
bool str_check(string str){
	for(int i = 1;i < str.size();++i){
		if(str[0] != str[i]) return false;
	}
	return true;
}
int f(){
	while(que.size()){
		worm w = que.front(),tmp;
		que.pop();
		int n = w.c;
		if(n > 9) break;
		string str = w.s;
		if(str_check(str)){
			return n;
		}
		for(int i = 0;i < str.size() - 1;++i){
			if(str[i] != str[i+1]){
				if((str[i] == 'r' && str[i+1] == 'b') || (str[i] == 'b' && str[i+1] == 'r')){
					str[i] = str[i+1] = 'g';
				}
				else if((str[i] == 'g' && str[i+1] == 'b') || (str[i] == 'b' && str[i+1] == 'g')){
					str[i] = str[i+1] = 'r';
				}
				else {
					str[i] = str[i+1] = 'b';
				}
				tmp.s = str;
				tmp.c = n + 1;
				que.push(tmp);
				str = w.s;
			}
		}
	}
	return -1;
}
int main(){
	string str;
	worm w;
	while(cin >> str){
		if(str == "0") break;
		w.s = str;
		w.c = 0;
		que.push(w);
		int n = f();
		if(n == -1) cout << "NA\n";
		else cout << n << endl;
		while(que.size()){
			que.pop();
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
class Data{
public:
    string bc;
    int t;
    Data(string bc,int t):bc(bc),t(t){}
    Data(){}
};
bool judge(string s,int l){
    for(int i=0;i<l-1;++i){
        if(s[i]!=s[i+1])return false;
    }
    return true;
}
char cj(char a,char b){
    if(a=='b'&&b=='g')return 'r';
    if(a=='b'&&b=='r')return 'g';
    if(a=='g'&&b=='r')return 'b';
    swap(a,b);
    if(a=='b'&&b=='g')return 'r';
    if(a=='b'&&b=='r')return 'g';
    if(a=='g'&&b=='r')return 'b';
}
int main()
{
    string s;
    while(cin>>s,s!="s")
    {
        int l=s.length();
        int res=-1;
        queue<Data> Q;
        set<string> visited;
        Q.push(Data(s,0));
        while(!Q.empty())
        {
            Data t=Q.front();
            Q.pop();
            if(visited.count(t.bc))continue;
            if(judge(t.bc,l)){
                res=t.t;
                break;
            }
            visited.insert(t.bc);
            for(int i=0;i<l-1;++i){
                if(t.bc[i]==t.bc[i+1])continue;
                char nc=cj(t.bc[i],t.bc[i+1]);
                string nbc=t.bc;
                nbc[i]=nbc[i+1]=nc;
                Q.push(Data(nbc,t.t+1));
            }
        }
        if(res==-1)
                cout<<"NA"<<endl;
        else
                cout<<res<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<math.h>
#include<queue>
using namespace std;

struct worm{
	char part[11];
	int dep;
	worm(){
		dep = 0;
	}
};

int main(){
	worm input;
	while (cin >> input.part, input.part[0] != '0'){
		int len;
		for (len = 0; input.part[len] != '\0'; len++){
			if (input.part[len] == 'r') input.part[len] = 0;
			else if (input.part[len] == 'g') input.part[len] = 1;
			else input.part[len] = 2;
		}
		queue<worm> q;
		q.push(input);
		worm q_c;
		bool memo[1000000] = {};
		while (1){
			bool color[3] = {};
			int sum1 = 0, sum2 = 0;
			int cnt_clr = 0;

			if (q.empty()){
				printf("NA\n");
				break;
			}

			q_c = q.front();

			for (int i = 0; i < len; i++){
				color[q_c.part[i]] = true;
			}
			for (int i = 0; i < 3; i++) if (color[i]) cnt_clr++;
			if (cnt_clr == 1){
				printf("%d\n", q_c.dep);
				break;
			}

			/*for (int i = 0; i < len; i++){
				printf("%d", q_c.part[i]);
				}*/
			for (int i = 0; i < len; i++){
				sum1 += q_c.part[i] * pow(3, i + 1);
				sum2 += q_c.part[i] * pow(3, len - i);
				//printf(" %d*%d^%d", q_c.part[i], 3, i + 1);
			}
			//printf(" =%d\n", sum1);
			if (memo[sum1]){
				q.pop();
				continue;
			}
			memo[sum1] = true;
			memo[sum2] = true;

			for (int i = 1; i < len; i++){
				if (q_c.part[i - 1] != q_c.part[i]){
					for (int j = 0; j < 3; j++) color[j] = false;

					color[q_c.part[i - 1]] = true;
					color[q_c.part[i]] = true;

					for (int j = 0; j < 3; j++){
						if (!color[j]){
							q_c.part[i - 1] = j;
							q_c.part[i] = j;
							q_c.dep++;
							q.push(q_c);
							q_c = q.front();
						}
					}
				}
			}
			q.pop();
		}
	}
	return 0;
}