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 <vector>
#include <map>
#include <climits>
using namespace std;

map<int,bool> isre;
map<int,int> dp;

int code(string s){
    int ret=0,kake=1;
    for(int i=0;i<s.size();i++){
        if(s[i]=='r') ret+=kake*1;
        else if(s[i]=='g') ret+=kake*2;
        else ret+=kake*3;
        kake*=4;
    }
    return ret;
}

int solve(string st){
    int co=code(st);
    if(dp.find(co)==dp.end()){
        bool is=true;
        for(int i=0;i<st.size();i++){
            if(st[i]!=st[0]) is=false;
        }
        if(is) return dp[co]=0;
        if(isre[co]) return dp[co]=-1;
        isre[co]=true;
        int miti=INT_MAX;
        for(int i=1;i<st.size();i++){
            string ne=st;
            if(st[i-1]!=st[i]){
                if(st[i-1]!='r'&&st[i]!='r'){
                    ne[i-1]=ne[i]='r';
                }else if(st[i-1]!='g'&&st[i]!='g'){
                    ne[i-1]=ne[i]='g';
                }else{
                    ne[i-1]=ne[i]='b';
                }
            }
            int ret=solve(ne);
            if(ret!=-1) miti=min(miti,ret+1);
        }
        isre[co]=false;
        if(miti==INT_MAX) return dp[co]=-1;
        else return dp[co]=miti;
    }else return dp[co];
}

int main(){
    string in;
    for(;;){
        cin>>in;
        if(in=="0") break;
        isre=map<int,bool>();
        dp=map<int,int>();
        int ret=solve(in);
        if(ret==-1) cout<<"NA"<<endl;
        else cout<<ret<<endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
struct Data{
    string s;
    int t;
    Data(string _s,int _t){
        s =_s,t =_t;
    }
};
map<string,bool> M;
int main(){
    string str;
    while(cin>>str,str!="0"){
        queue<Data> Q;
        Q.push(Data(str,0));
        while(!Q.empty()){
            Data E = Q.front();
            //cout<<E.s<<endl;
            Q.pop();
            if(M[E.s])continue;
            M[E.s] = true;
            bool flg = true;
            for(int i = 1;i < E.s.length();i++){
                if(E.s[i] != E.s[0])flg = false;
            }
            if(flg){
                cout<<E.t<<endl;
                goto e;
            }
            for(int i = 0;i < E.s.length()-1;i++){
                Data D = E;
                if(D.s[i]!=D.s[i+1]){
                    bool flg[3]={};
                    string w = "rgb";
                    if(D.s[i]=='r'||D.s[i+1]=='r')flg[0] = true;
                    if(D.s[i]=='g'||D.s[i+1]=='g')flg[1] = true;
                    if(D.s[i]=='b'||D.s[i+1]=='b')flg[2] = true;
                    for(int j = 0;j < 3;j++){
                        if(!flg[j])D.s[i] = D.s[i+1] = w[j];
                    }
                    D.t++;
                    //cout<<D.s<<endl;
                    Q.push(D);
                }
            }
        }
        cout<<"NA"<<endl;
        e:;
    }
}

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

#define mkp(x, y) (make_pair(x, y))

int main() {
	string in;

	while(cin >> in, in != "0") {
		set<string> appeared;
		queue<pair<string, int> > q;
		int loop_flag = -1;

		appeared.insert(in);
		q.push(mkp(in, 0));

		while(!q.empty()) {
			string worm;
			int count;
			pair<string, int> p;
			bool complete_flag = true;
			p = q.front(); q.pop();
			worm = p.first;
			count = p.second;

			for(int i = 1; i < worm.size(); i++) {
				if(worm[i - 1] != worm[i]) {
					string tmp(worm);
					tmp[i - 1] = tmp[i] = 'r' + 'g' + 'b' - worm[i - 1] - worm[i];
					if(appeared.find(tmp) == appeared.end()) {
						q.push(mkp(tmp, count + 1));
						appeared.insert(tmp);
					}
					complete_flag = false;
				}
			}

			if(complete_flag) {
				loop_flag = count;
				break;
			}
		}

		if(loop_flag != -1)
			cout << loop_flag << endl;
		else
			cout << "NA" << endl;
	}

	return 0;
}