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

using namespace std;

int main(){
  string worm;
  cin>>worm;
  
  while(worm != "0"){
    queue<pair<string,int> >que;
    que.push(make_pair(worm,0));
    map<string,bool> done_str;
    
    int ans = -1;
    
    while(!que.empty()){
      string s = que.front().first;
      int count = que.front().second;
      que.pop();

        
      string cmp_strR,cmp_strG,cmp_strB;

      cmp_strR.append(worm.size(), 'r');
      cmp_strG.append(worm.size(), 'g');
      cmp_strB.append(worm.size(), 'b');
      
      if(s == cmp_strR || s == cmp_strG || s == cmp_strB ){
	ans = count;
	break;
      }else if(done_str.find(s) != done_str.end()){
	continue;
      }else{
	done_str.insert(make_pair(s,true));
      }

      
      for(int i=0;i<s.size()-1;i++){
	if(s.at(i) != s.at(i+1)){
	  if(s.at(i) != 'r' && s.at(i+1)!='r'){
	    string tmp = s;
	    que.push(make_pair(tmp.replace(i,2,2,'r'),count+1));

	  }else if(s.at(i) != 'g' && s.at(i+1)!='g'){
	    string tmp = s;
	    que.push(make_pair(tmp.replace(i,2,2,'g'),count+1));

	  }else if(s.at(i) != 'b' && s.at(i+1)!='b'){
	    string tmp = s;
	    que.push(make_pair(tmp.replace(i,2,2,'b'),count+1));
	  }
	}
      }
    }

    if(ans < 0){
      cout<<"NA"<<endl;
    }else{
      cout<<ans<<endl;
    }
    
    cin>>worm;
  }

  return 0;
}

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

int main(){
	string s;
	while(1){
		map<string,bool> used;
		cin >> s;
		if(s == "0") break;
		queue<string> que;
		que.push(s);
		for(int i = 0;;i++){
			int siz = que.size();
			if(!siz){
				cout << "NA" << endl;
				break;
			}
			for(int j = 0;j < siz;j++){
				s = que.front();que.pop();
				if(used[s]) continue;
				used[s] = true;
				char c = s[0];
				bool flag = true;
				for(int k = 1;k < s.length();k++){
					if(s[k] != c) flag = false;
				}
				if(flag){
					cout << i << endl;
					goto end;
				}
				for(int k = 0;k < s.length() - 1;k++){
					if(s[k] != s[k + 1]){
						string t = s;
						if(s[k] != 'g' && s[k + 1] != 'g'){
							t[k] = 'g';
							t[k + 1] = 'g';
							que.push(t);
						}
						else if(s[k] != 'r' && s[k + 1] != 'r'){
							t[k] = 'r';
							t[k + 1] = 'r';
							que.push(t);
						}
						else{
							t[k] = 'b';
							t[k + 1] = 'b';
							que.push(t);
						}
					}
				}
			}
		}
		end:
		continue;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
class Data{
public:
    string bc;
    int t;
    Data(string bc,int t):bc(bc),t(t){}
    Data(){}
};
bool judge(string s,int l){
    for(int i=0;i<l-1;++i){
        if(s[i]!=s[i+1])return false;
    }
    return true;
}
char cj(char a,char b){
    if(a=='b'&&b=='g')return 'r';
    if(a=='b'&&b=='r')return 'g';
    if(a=='g'&&b=='r')return 'b';
    swap(a,b);
    if(a=='b'&&b=='g')return 'r';
    if(a=='b'&&b=='r')return 'g';
    if(a=='g'&&b=='r')return 'b';
}
int main()
{
    string s;
    while(cin>>s,s!="0")
    {
        int l=s.length();
        int res=-1;
        queue<Data> Q;
        set<string> visited;
        Q.push(Data(s,0));
        while(!Q.empty())
        {
            Data t=Q.front();
            Q.pop();
            if(visited.count(t.bc))continue;
            if(judge(t.bc,l)){
                res=t.t;
                break;
            }
            visited.insert(t.bc);
            for(int i=0;i<l-1;++i){
                if(t.bc[i]==t.bc[i+1])continue;
                char nc=cj(t.bc[i],t.bc[i+1]);
                string nbc=t.bc;
                nbc[i]=nbc[i+1]=nc;
                Q.push(Data(nbc,t.t+1));
            }
        }
        if(res==-1)
                cout<<"NA"<<endl;
        else
                cout<<res<<endl;
    }
    return 0;
}