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, 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)){
                                            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 <iostream>
#include <string>
#include <queue>
#include <map>

using namespace std;
typedef pair<string, int> P;

int main()
{
	string str;
	while(cin >> str){
		if(str[0] == '0')
			break;
		int count = -1;
		map<string,bool> mp;
		queue<P> que;
		P p;p.first = str;p.second = 0;
		que.push(p);
		while(que.size()){
			P s = que.front();que.pop();
			if(mp.find(s.first)!=mp.end())
				continue;
			else
				mp[s.first] = true;

			if(s.second > 10)
				break;

			bool is_all = true;
			for(unsigned i=1;i<s.first.size();i++){
				if(s.first[0] != s.first[i]){
					is_all = false;
					break;
				}
			}
			if(is_all){
				count = s.second;
			}

			int a=0;
			for(unsigned i=0;i<s.first.size()-1;i++){
				P ss = s;
				if((ss.first[i] != ss.first[i+1])){
					if((ss.first[i] == 'r' && ss.first[i+1] == 'g') || (ss.first[i] == 'g' && ss.first[i+1] == 'r')){
						ss.first[i] = 'b';ss.first[i+1] = 'b';
					}
					if((ss.first[i] == 'r' && ss.first[i+1] == 'b') || (ss.first[i] == 'b' && ss.first[i+1] == 'r')){
						ss.first[i] = 'g';ss.first[i+1] = 'g';
					}
					if((ss.first[i] == 'b' && ss.first[i+1] == 'g') || (ss.first[i] == 'g' && ss.first[i+1] == 'b')){
						ss.first[i] = 'r';ss.first[i+1] = 'r';
					}
					ss.second = s.second+1;
					que.push(ss);
				}
			}
		}
		if(count == -1)
			cout << "NA" << endl;
		else
			cout << count << endl;
	}
	return 0;
}