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 <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int worm[4][4][4][4][4][4][4][4][4][4];

int main() {
    fill_n((int *)worm, 4*4*4*4*4*4*4*4*4*4, -1);
    FOR(len, 2, 11) {
        queue<vi> que;
        FOR(color, 1, 4) {
            vi v(10, 0);
            REP(i, len) v[i] = color;
            que.push(v);
        }
        que.push({});
        int cnt = 0;
        while (1) {
            vi v = que.front(); que.pop();
            if (v.empty()) {
                if (que.empty()) break;
                que.push({});
                cnt++;
                continue;
            }
            if (worm[v[9]][v[8]][v[7]][v[6]][v[5]][v[4]][v[3]][v[2]][v[1]][v[0]] != -1) continue;
            worm[v[9]][v[8]][v[7]][v[6]][v[5]][v[4]][v[3]][v[2]][v[1]][v[0]] = cnt;
            REP(i, len-1) {
                if (v[i+1] != 0 && v[i] == v[i+1]) {
                    int c1 = (v[i]) % 3 + 1, c2 = (v[i]+1) % 3 + 1;
                    vi v1 = v;
                    v1[i] = c1, v1[i+1] = c2;
                    que.push(v1);
                    vi v2 = v;
                    v2[i] = c2, v2[i+1] = c1;
                    que.push(v2);
                }
            }
        }
    }

    string line;
    while (cin >> line, line != "0") {
        vi v(10, 0);
        REP(i, line.size()) v[i] = line[i] % 3 + 1; // rgb -> 123
        int res = worm[v[9]][v[8]][v[7]][v[6]][v[5]][v[4]][v[3]][v[2]][v[1]][v[0]];
        if (res == -1) {
            cout << "NA" << endl;
        } else {
            cout << res << endl;
        }
    }
}

C++: #include <iostream>
#include <map>
#include <deque>
#include <string>

using namespace std;

int main(void){
	string str;
	
	while(cin >> str && str != "0"){
		int ret = -1;
		map<string,bool> m;
		deque<pair<string,int> > q;
		string curstr,tmpstr;
		int curtime;
		
		q.push_back(make_pair(str,0));
		m[str] = true;
		while(!q.empty()){
			curstr = q.front().first;
			curtime = q.front().second;
			q.pop_front();
			
			//cout << curstr << endl;
			
			bool isended = true;
			
			for(int i=0;i<curstr.size()-1;i++){
				if(curstr[i] != curstr[i+1]) isended = false;
			}
			
			if(isended){
				ret = curtime;
				break;
			}
			
			tmpstr = curstr;
			
			for(int i=0;i<curstr.size()-1;i++){
				if((tmpstr[i] == 'b' && tmpstr[i+1] == 'g') ||
				(tmpstr[i] == 'g' && tmpstr[i+1] == 'b')){
					tmpstr[i] = 'r'; tmpstr[i+1] = 'r';
					//cout << tmpstr << endl;
					if(m[tmpstr] == false){
						m[tmpstr] = true;
						q.push_back(make_pair(tmpstr, curtime+1));
					}
					tmpstr = curstr;
				}else if((tmpstr[i] == 'r' && tmpstr[i+1] == 'g') ||
				(tmpstr[i] == 'g' && tmpstr[i+1] == 'r')){
					tmpstr[i] = 'b'; tmpstr[i+1] = 'b';
					if(m[tmpstr] == false){
						m[tmpstr] = true;
						q.push_back(make_pair(tmpstr, curtime+1));
					}
					tmpstr = curstr;
				}else if((tmpstr[i] == 'b' && tmpstr[i+1] == 'r') ||
				(tmpstr[i] == 'r' && tmpstr[i+1] == 'b')){
					tmpstr[i] = 'g'; tmpstr[i+1] = 'g';
					if(m[tmpstr] == false){
						m[tmpstr] = true;
						q.push_back(make_pair(tmpstr, curtime+1));
					}
					tmpstr = curstr;
				}
			}
		}
		if(ret == -1){
			cout << "NA" << endl;
		}else{
			cout << ret << endl;
		}
	}
}