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<queue>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j,k;
  int n,p;
  bool b[3][3][3][3][3][3][3][3][3][3];
  string s;
  queue<pair<string,int> > c;
  while(cin>>s&&s[0]!='0'){
    n=s.length();
    for(i=0;i<10-n;i++)
      s+=(char)0;
    memset(b,0,59049);
    while(c.empty()==0)
      c.pop();
    c.push(make_pair(s,0));
    while(c.empty()==0){
      s=c.front().first;
      p=c.front().second;
      for(i=0;i<n;i++)
	s[i]+=3;
      j=0;
      for(i=0;i<n;i++){
	if(s[i]>2){
	  for(k=i+1;k<n;k++){
	    if(s[k]==s[i])
	      s[k]=j;
	  }
	  s[i]=j;
	  j++;
	}
      }
      for(i=1;i<n&&s[i-1]==s[i];i++);
      if(i==n)
	break;
      c.pop();
      bool* q=&b[(int)s[0]][(int)s[1]][(int)s[2]][(int)s[3]][(int)s[4]][(int)s[5]][(int)s[6]][(int)s[7]][(int)s[8]][(int)s[9]];
      if(*q==0){
	*q=1;
	for(i=1;i<n;i++){
	  if(0){
	  }else if(s[i-1]<s[i]){
	    if(0){
	    }else if(s[i]-s[i-1]==1){
	      s[i-1]=(s[i-1]+2)%3;
	      s[i]=(s[i]+1)%3;
	      c.push(make_pair(s,p+1));
	      s[i-1]=s[i-1]-2+(s[i-1]-2<0?3:0);
	      s[i]=s[i]-1+(s[i]-1<0?3:0);
	    }else if(s[i]-s[i-1]==2){
	      s[i-1]=(s[i-1]+1)%3;
	      s[i]=(s[i]+2)%3;
	      c.push(make_pair(s,p+1));
	      s[i-1]=s[i-1]-1+(s[i-1]-1<0?3:0);
	      s[i]=s[i]-2+(s[i]-2<0?3:0);
	    }
	  }else if(s[i-1]>s[i]){
	    if(0){
	    }else if(s[i-1]-s[i]==1){
	      s[i]=(s[i]+2)%3;
	      s[i-1]=(s[i-1]+1)%3;
	      c.push(make_pair(s,p+1));
	      s[i]=s[i]-2+(s[i]-2<0?3:0);
	      s[i-1]=s[i-1]-1+(s[i-1]-1<0?3:0);
	    }else if(s[i-1]-s[i]==2){
	      s[i]=(s[i]+1)%3;
	      s[i-1]=(s[i-1]+2)%3;
	      c.push(make_pair(s,p+1));
	      s[i]=s[i]-1+(s[i]-1<0?3:0);
	      s[i-1]=s[i-1]-2+(s[i-1]-2<0?3:0);
	    }
	  }	    
	}
      }
    }
    if(c.empty())
      cout<<"NA"<<endl;
    else
      cout<<p<<endl;
  }
  return 0;
}

C++: //2013-5

#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <time.h>
#define mp make_pair
#define fs first
#define sc second
#define rep(x,n) for(int x = 0;x < n;x ++)
#define Rep(x,n) for(int x = 1;x <= n;x ++)
using namespace std;

char retcol(char c1,char c2){
    if((c1 == 'g' && c2== 'r')||(c1 == 'r' && c2== 'g'))
        return 'b';
    else if((c1 == 'b' && c2 == 'r')||(c1 == 'r' && c2 == 'b'))
        return 'g';
    else
        return 'r';
}

int n;

bool issame(string xx){
    int nu = xx.size();
    rep(i,nu-1)
        if(xx[i]!=xx[i+1])
            return 0;
    return 1;
}

int main(){

    bool tf = 0;
    string str,next;
    while(cin >> str&&str != "0"){
        queue<string> que;
        que.push(str);
        tf = 0;
        n = str.size();
        //この時点で判定
        if(issame(str)){
            printf("0\n");
            continue;
        }
        map<string,int> flag;
        flag.insert(make_pair(str,0));
        //探索
        while(!que.empty()&& tf == 0){
            string str = que.front();
            que.pop();
            if(flag.find(str)==flag.end())
                continue;
            int num = flag.find(str)->second;
            rep(i,n-1){
                if(str[i]!=str[i+1]){
                    next = str;
                    next[i]=retcol(str[i],str[i+1]);
                    next[i+1]=next[i];
                    if(issame(next)){
                        tf = 1;
                        cout << num + 1 << endl;
                        break;
                    }
                    if(flag.find(next)==flag.end()){
                        que.push(next);
                        flag.insert(make_pair(next,num+1));
                    }
                    
                }
            }
        }
        if(tf == 0){
            printf("NA\n");
        }
    }
}

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

int main() {
	string sin,ans;
	while(cin >> sin && sin != "0"){
		queue<string> Q;
		map<string,int> B;
		ans = "";
		Q.push(sin);
		B[sin] = 0;
		while(!Q.empty()){
			string s = Q.front();Q.pop();
			char t = s[0];
			bool allF = true;
			for(int i = 0;i<s.length();i++){
				if(s[i] != t){
					allF = false;
					break;
				}
			}
			if(allF){
				ans = s;
				break;
			}
			for(int i = 1;i<s.length();i++){
				string ss = s;
				if(s[i] != s[i-1]){
					char c = 'r' + 'g' + 'b' - s[i] - s[i-1];
					ss[i] = c;
					ss[i-1] = c;
					if(B[ss] == 0){
						B[ss] = B[s] + 1;
						Q.push(ss);
					}
				}
			}
		}
		if(!ans.empty()){
			cout << B[ans] << endl;
		}
		else{
			cout << "NA" << endl;
		}
	}
	return 0;
}