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<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	map<string,string>q;
	q["bg"]=q["gb"]="rr";
	q["gr"]=q["rg"]="bb";
	q["rb"]=q["br"]="gg";
	string s;
	while(cin>>s,s!="0"){
		char qqq=s[0];
		bool h=true;
		rep(i,s.size())if(s[i]!=qqq)h=false;
		if(h){cout<<0<<endl;continue;}
		vector<string>in;
		map<string,bool>tmp;
		in.pb(s);
		int co=0;
		while(1){
			vector<string>out;
			bool h=false;
			rep(i,in.size()){
				string s=in[i];
				tmp[s]=true;
				//h=true;
				//char qq=in[i][0];
				//rep(j,s.size())if(qq!=in[i][j])h=false;
				//if(h)break;
				rep(j,s.size()-1)if(s[j]!=s[j+1]){
					string www=s.substr(j,2);
					string w=q[www];
					string ww=s;
					ww[j]=w[0];ww[j+1]=w[1];
					h=true;
					char qq=ww[0];
					rep(k,ww.size())if(qq!=ww[k])h=false;
					if(h)break;
					if(!tmp[ww]){out.pb(ww);tmp[ww]=true;}
				}
				if(h)break;
			}
			//cout<<endl;
			//rep(i,out.size())cout<<out[i]<<endl;
			//cout<<tmp.size()<<endl;
			//cout<<out.size()<<endl;
			if(h)break;
			co++;
			in=out;
			//cout<<" "<<in.size()<<endl;
			if(co==100)break;
		}
		if(co==100)cout<<"NA"<<endl;
		else cout<<co+1<<endl;
	}
}

C++: #include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

#define reps(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reps(i, 0, n) 

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main()
{
	char str[16];
	while(scanf("%s", str), str[0]!='0'){
		int n = strlen(str);
		int ans = -1;
		set<string> visited;
		queue<pair<string, int> > Q;
		Q.push(make_pair(str, 0));
		while(!Q.empty()){
			pair<string, int> data = Q.front();
			Q.pop();
			if(visited.count(data.first))
				continue;
			if(count(data.first.begin(), data.first.end(), data.first[0]) == n){
				ans = data.second;
				break;
			}
			visited.insert(data.first);
			rep(i, n-1){
				char a = min(data.first[i], data.first[i+1]);
				char b = max(data.first[i], data.first[i+1]);
				if(a == b)
					continue;
				char nc;
				if(a == 'b')
					nc = b=='g' ? 'r' : 'g';
				else
					nc = 'b';
				string next = data.first;
				next[i] = next[i+1] = nc;
				Q.push(make_pair(next, data.second+1));
			}
		}
		if(ans == -1)
			puts("NA");
		else
			printf("%d\n", ans);
	}
	return 0;
}