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<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

using namespace std;

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long ll;

int main()
{
    map<string, int> worm;
    queue<string> que;
    string chars = "rgb";
    REP(i, 1, 11){
        for (char c : chars) {
            worm[string(i,c)] = 0;
            que.push(string(i,c));
            while (!que.empty()) {
                string s = que.front(); que.pop();
                rep(j, s.length()-1){
                    if (s[j] != s[j+1])
                        continue;
                    rep(k, 3){
                        if (s[j] == chars[k]) {
                            REP(l, k+1, k+3){
                                REP(m, k+1, k+3){
                                    if (l != m) {
                                        string p = s;
                                        p[j] = chars[l%3];
                                        p[j+1] = chars[m%3];
                                        if(!EXIST(worm, p)){
                                            que.push(p);
                                            worm[p] = worm[s] + 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    string input;
    while (cin >> input, input != "0") {
        if (EXIST(worm, input))
            cout << worm[input] << endl;
        else
            cout << "NA" << endl;
    }
    return 0;
}

C++: #include <queue>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
int dp[60000];
int hash_(string s) {
	int ret = 0;
	for(int i = 0; i < s.size(); i++) {
		ret *= 3;
		if(s[i] == 'g') ret += 1;
		if(s[i] == 'b') ret += 2;
	}
	return ret;
}
string s; int w, w2;
int main() {
	while(cin >> s, s != "0") {
		memset(dp, -1, sizeof(dp)); w = 0, w2 = 1;
		for(int i = 0; i < s.size(); i++) w += w2, w2 *= 3;
		int h2 = hash_(s); dp[h2] = 0;
		if(h2 % w == 0) {
			printf("0\n"); continue;
		}
		queue<string> que; que.push(s); bool flag = false;
		while(!que.empty()) {
			string t = que.front(); que.pop(); int h_ = hash_(t);
			for(int i = 1; i < t.size(); i++) {
				if(t[i - 1] != t[i]) {
					string u = t;
					if(t[i - 1] != 'r' && t[i] != 'r') u[i - 1] = 'r', u[i] = 'r';
					else if(t[i - 1] != 'g' && t[i] != 'g') u[i - 1] = 'g', u[i] = 'g';
					else u[i - 1] = 'b', u[i] = 'b';
					int h = hash_(u);
					if(h % w == 0) {
						printf("%d\n", dp[h_] + 1); flag = true; break;
					}
					if(dp[h] == -1) {
						dp[h] = dp[h_] + 1; que.push(u);
					}
				}
			}
			if(flag) break;
		}
		if(!flag) printf("NA\n");
	}
}