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 <iostream>
#include <queue>
#include <map>
using namespace std;

char get(char a,char b){
	bool c[128]={0};
	c[a] = c[b] = true;
	if(!c['r'])return 'r';
	if(!c['g'])return 'g';
	if(!c['b'])return 'b';
}
bool same(string t){
	for(int i=0;i<t.size();i++)
		if(t[i] != t[0])return false;
	return true;
}
int main(){
	string str;
	while(cin >> str , str != "0"){
		queue<string> Q;
		map<string,int> done;
		done[str] = 0;
		Q.push(str);
		int ans = -1;
		while(Q.size()){
			string q = Q.front(); Q.pop();
			if(same(q)){ans = done[q];break;};
			for(int i=0;i<q.size()-1;i++){
				if(q[i] != q[i+1]){
					string t = q;
					t[i] = t[i+1] = get(q[i],q[i+1]);
					if(done.find(t) == done.end()){
						done[t] = done[q]+1;
						Q.push(t);
					}
				}
			}
		}
		if(~ans)cout << ans << endl;
		else cout << "NA" << endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef pair<int,int> pii;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999
int dp[60000];



int main(){
    string s;
    while(cin>>s){
        if(s=="0")break;
        int l=s.size();
        rep(i,60000)dp[i]=INF;
        bool ff=true;
        rep(i,s.size()-1){
            if(s[i]!=s[i+1])ff=false;
        }
        if(ff)cout<<"0"<<endl;
        if(ff)continue;
        int num=0;
        rep(i,s.size()){
            num*=3;
            int p=0;
            if(s[i]=='g')p=1;
            if(s[i]=='b')p=2;
            num+=p;
        }
        dp[num]=0;
        
        queue<pii> que;
        que.push(pii(num,0));
        while(que.size()){
//            cout<<que.size()<<endl;
            pii q=que.front();
            que.pop();
            vector<int> vec;
            rep(i,l){
                vec.pb(q.first%3);
                q.first/=3;
            }
            reverse(all(vec));
            reverse(all(vec));
            rep(i,l-1){
                if(vec[i]!=vec[i+1]){
                    int a=vec[i],b=vec[i+1];
                    if(a>b)swap(a,b);
                    vector<int> v=vec;
                    if(a==0&&b==1)v[i]=v[i+1]=2;
                    if(a==1&&b==2)v[i]=v[i+1]=0;
                    if(a==0&&b==2)v[i]=v[i+1]=1;
                    bool f=true;
                    rep(i,vec.size()-1){
                        if(v[i]!=v[i+1])f=false;
                    }
                    if(f){
                        cout<<q.second+1<<endl;
                        goto next;
                    }
                    int x=0;
                    rep(j,v.size()){
                        x*=3;
                        x+=v[j];
                    }
                    if(dp[x]==INF){
                        dp[x]=q.second+1;
                        que.push(pii(x,q.second+1));
                    }
                }
            }
        }
        cout<<"NA"<<endl;
    next:;
    }
}