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 <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <map>
#include <queue>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
string s;
string rbg = "rbg";
char calc(char a, char b)
{
    for(int i = 0; i < rbg.size(); i++) if(rbg[i] != a and rbg[i] != b) return rbg[i];
}
bool check(string s)
{
    char ch = s[0];
    for(int i = 0; i < s.size(); i++) if(ch != s[i]) return false;
    return true;
}
int solve()
{
    map<string, int> mp;
    queue<pair<string, int>> que;
    que.push(make_pair(s, 0));
    while(que.size())
    {
        pair<string, int> p = que.front(); que.pop();
        string target = p.first;
        int cnt = p.second;
        if(mp.count(target)) continue;
        if(check(target)) return cnt;
        mp[target] = cnt;
        int pos = 0;
        while(pos < target.size() - 1)
        {
            string next_target = target;
            char a = next_target[pos], b = next_target[pos + 1];
            if(a != b)
            {
                next_target[pos] = next_target[pos + 1] = calc(a, b);
                que.push(make_pair(next_target, cnt + 1));
            }
            pos++;
        }
    }
    return -1;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> s, s != "0")
    {
        int ans = solve();
        if(ans == -1) cout << "NA" << endl;
        else cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
using namespace std;

bool is_goal( string s )
{
	bool ok = true;
	for ( int i = 0; i + 1 < s.size(); i++ ) if ( s[i] != s[i+1] ) ok = false;
	return ok;
}

char change_color( char a, char b )
{
	if ( a == 'r' )
		return ( b == 'g' ) ? 'b' : 'g';

	else if ( a == 'g' )
		return ( b == 'b' ) ? 'r' : 'b';

	else if ( a == 'b' )
		return ( b == 'r' ) ? 'g' : 'r';

	return ' ';
}

int main( void )
{
	string in;
	while ( cin >> in )
	{
		if ( in == "0" )
			break;

		map <string, int>	V;
		queue <string>		Q;

		V[in] = 0;
		Q.push( in );

		int kotae = -1;
		while ( Q.empty() == false )
		{
			string now = Q.front();
			Q.pop();

			if ( is_goal( now ) )
			{
				kotae = V[now];
				break;
			}

			for ( int i = 0; i + 1 < now.size(); i++ )
			{
				string tmp = now;
				if ( tmp[i] != tmp[i+1] )
				{
					tmp[i] = tmp[i+1] = change_color( tmp[i], tmp[i+1] );
				}

				if ( V.find( tmp ) == V.end() )
				{
					V[tmp] = V[now] + 1;
					Q.push( tmp );
				}

			}

		}

		if ( kotae == -1 )
			cout << "NA" << endl;
		else
			cout << kotae << endl;

	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define FOR(i, n, m) for(int i = (n);i < (m);i++)
#define REP(i, n) for(int i = 0;i < (n);i++)
#define PII pair<int, int>

using namespace std;

string worm;

int all(string a){
    char b = a[0];
    int cnt = 0;
    for(int i = 1;a[i] != '\0';i++){
        if(a[i] != b)
            cnt++;
    }
    return cnt;
}

int solve(){
    queue< pair<string, int> > que;
    map<string, int> m;
    que.push(pair<string, int>(worm, 0));

    if(all(worm) == 0){
        return 0;
    }

    while(que.size()){
        string worm1 = que.front().first;
        int num = que.front().second;
        que.pop();

        if(m[worm1]){
            continue;
        }
        if(all(worm1) == 0){
            return num;
        }

        m[worm1] = num;

        REP(i, worm1.size()-1){
            string tmp = worm1;
            if(tmp[i] != tmp[i+1]){
                if(tmp[i] != 'r' && tmp[i+1] != 'r') tmp[i] = tmp[i+1] = 'r';
                else if(tmp[i] != 'g' && tmp[i+1] != 'g') tmp[i] = tmp[i+1] = 'g';
                else tmp[i] = tmp[i+1] = 'b';

                que.push(pair<string, int> (tmp, num+1));
           }
        }
    }

    return -1;
}

int main(void){
    cin >> worm;
    while(worm[0] != '0'){
        int result = solve();
        if(result >= 0){
            cout << result << endl;
        }else{
            cout << "NA" << endl;
        }
        cin >> worm;
    }

    return 0;
}