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 <string>
#include <set>
#include <vector>
#include <queue>
using namespace std;

int main()
{
	string s;
	char c[] = { "rgb" };

	while( cin >> s && s != "0" ){
		set< string > memo;
		vector< string > sample;
		for( int i = 0; i < 3; ++i ){
			sample.push_back( string( s.size(), c[ i ] ) );
		}

		queue< string > pattern;
		pattern.push( s );
		int sec = 0;
		bool found = false;
		while( !pattern.empty() ){
			int size = pattern.size();
			while( size > 0 ){
				string p = pattern.front();
				pattern.pop();
				if( memo.find( p ) != memo.end() ){
					--size;
					continue;
				}
				memo.insert( p );
				for( int i = 0; i < 3; ++i ){
					if( p == sample[ i ] ){
						found = true;
						break;
					}
				}
				if( found ) break;

				for( int i = 0; i < (int)p.size() - 1; ++i ){
					if( p[ i ] == p[ i + 1 ] ){
						continue;
					}
					int oo = p[ i ] + p[ i + 1 ];
					char color;
					switch( oo ){
						case 'r' + 'g': color = 'b'; break;
						case 'g' + 'b': color = 'r'; break;
						case 'b' + 'r': color = 'g'; break;
					}
					string cpy = p;
					cpy[ i ] = cpy[ i + 1 ] = color;
					pattern.push( cpy );
				}
				--size;
			}
			if( found ) break;
			++sec;
		}
		if( found ){
			cout << sec << endl;
		}else{
			cout << "NA" << endl;
		}
	}

	return 0;
}

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, 2, 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) || worm[p] > worm[s]+1){
                                            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;
}