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

// c1, c2 ª'r', 'g', 'b', ©Â(c1 != c2)ÌÆ«
// m[c1+c2] Å c1, c2 ÅÈ¢'r', 'g', 'b'ªÔÁÄ­é
map<int,char> m;

// S¯¶F©Ç¤©
bool isSameColor(const string& s){
	char c[3] = {'r','g','b'};
	for(int i = 0 ; i < 3 ; i++ ){
		int cnt = 0;
		for(int j=0 ; j < s.size() ; j++ ){
			if( s[j] == c[i] )
				cnt++;
		}
		if( cnt == s.size() )
			return true;
	}
	return false;
}

int solve(string s_ ){
	// memo[s] = true ÈçTõÏÝ
	map<string,bool> memo;
	// q.front().first Í ÌóÔ(string) q.front().second Íè(int)
	queue< pair<string,int> > q;
	
	// úóÔðL[Éüêé
	pair<string,int> ini( s_ , 0  );
	q.push( ini );
	memo[s_] = true;
	
	// L[ªóÉÈéÜÅ[v
	while( !q.empty() ){
		// stirng s; ÍÌóÔ, int cnt; Íè
		string s = q.front().first;
		int cnt = q.front().second;
		q.pop();
		
		// S¯¶FÈçTõI¹
		if( isSameColor(s) ){
			return cnt;
		}
		
		// Ü¾TõµÄ¢È¢ÌóÔðL[Éüêé
		for(int i=1 ; i < s.size() ; i++ ){
			if( s[i-1] != s[i] ){
				char next_color = m[s[i-1]+s[i]];
				char c1 = s[i-1];
				char c2 = s[i];
				s[i-1] = s[i] = next_color;
				if( memo[s] != true ){
					memo[s] = true;
					pair<string,int> next;
					next.first = s;
					next.second = cnt + 1;
					q.push( next );
				}
				s[i-1] = c1;
				s[i] = c2;
			}
		}
	}
	return -1;
}

int main(){
	string s;
	m['r'+'g'] = 'b';
	m['g'+'b'] = 'r';
	m['b'+'r'] = 'g';
	
	while( cin >> s , s != "0" ){
		int ans = solve( s );
		if( ans == -1 )
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
}

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

typedef pair<string, int> P;

int bfs(string worm){
	queue<P> que;
	que.push(P(worm, 0));

	set<string> d;
	
	while(!que.empty()){
		P p = que.front();
		que.pop();
		if(d.count(p.first)) continue;

		string str = p.first;
		
		bool flag = false;
		for(int i=0;i<str.size()-1;i++){
			if(str[i] != str[i+1]){
				flag = true;
				break;
			}
		}
		if(!flag) return p.second;
		d.insert(p.first);

		for(int i=0;i<str.size()-1;i++){
			if(str[i] != str[i+1]){
				char temp;
				string next;

				if(str[i] != 'b' && str[i+1] != 'b'){
					temp = 'b';
				}else if(str[i] != 'g' && str[i+1] != 'g'){
					temp = 'g';
				}else{
					temp = 'r';
				}

				next = str;
				next[i] = next[i+1] = temp;

				que.push(P(next, p.second+1));
			}
		}
	}
	return -1;
}

int main(){
	string worm;
	while(cin >> worm && worm != "0"){
		int ret = bfs(worm);
		if(ret>=0) cout << ret << endl;
		else cout << "NA" << endl;
	}
}

C++: 
#include<iostream>
#include<string>
#include<queue>
#include<map>
using namespace std;
struct Data{string s;int n;};
int main()
{
  map<int,int> change;
  change['r'+'g']='b';
  change['g'+'b']='r';
  change['b'+'r']='g';
  map<string,int> changed;
  queue<Data> q;
  string s;
  Data d;
  int i,n;
  bool flag;
  int t=0;
  while(cin>>s,s!="0"){
    changed.clear();
    while(!q.empty())q.pop();
    d.s=s;
    d.n=0;
    q.push(d);
    changed[d.s]=1;
    while(!q.empty()){
      d=q.front();
      s=d.s;
      n=d.n;
      q.pop();
      flag=true;
      for(i=1;i<s.size();i++){
	if(s[i-1]==s[i])continue;
	flag=false;
	d.s=s;
	d.s[i-1]=d.s[i]=change[d.s[i-1]+d.s[i]];
	d.n=n+1;
	if(changed.find(d.s)!=changed.end())continue;
	changed[d.s]=1;
	q.push(d);
      }
      if(flag)break;
    }
    if(flag)cout<<n<<endl;
    else puts("NA");
  }
}