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++: //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;
    cout << issame("asfd")<<endl;
    cout << issame("aaaa")<<endl;

    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<queue>
using namespace std;
char conv(char c1,char c2){
  if(c1=='r'){
    if(c2=='g')return 'b';
    if(c2=='b')return 'g';
  }else if(c1=='g'){
    if(c2=='r')return 'b';
    if(c2=='b')return 'r';
  }else if(c1=='b'){
    if(c2=='r')return 'g';
    if(c2=='g')return 'r';
  }
}
int bfs(string s){
  queue< pair<int, string> > qs;
  qs.push( make_pair(0,s) );
  while(!qs.empty()){
    bool bFound=false;
    int ret = qs.front().first;
    string now = qs.front().second;
    if(ret>s.length())break;
    qs.pop();
    for(int i = 0; i < now.length()-1; ++i){
      if(now[i]!=now[i+1]){
	bFound = true;
	string next = now;
	char nc = conv(now[i],now[i+1]);
	next[i] = next[i+1] = nc;
	qs.push(make_pair(ret+1,next));
      }
    }
    if(!bFound){
      //cout << now << endl;
      return ret;
    }
  }
  return -1;
}
int main(){
  while(true){
    string s;
    cin >> s;
    if( s == "0" )  break;
    int ans = bfs(s);
    if( ans < 0 )
      cout << "NA" << endl;
    else
      cout << ans << endl;    
  }
  return 0;
}

C++: #include <cstdio>
#include <utility>
#include <queue>
#include <map>
using namespace std;

#define F(X,Y) ((X)>>((Y-1)*2)&3)

typedef pair<int,int> P;

int main() {
	char st[10];
	while(scanf("%s",st),st[0]!='0') {
		queue<P> que;
		map<int,bool> mp;
		int x=0,d;
		for(d=0;st[d]!='\0';d++) {
			x<<=2;
			x|=st[d]=='r'?0:st[d]=='b'?1:2;
		}
		mp[x]=true;
		que.push(P(x,0));
		int ans=-1;
		while(!que.empty()) {
			P p=que.front();
			que.pop();
			x=p.first;
			bool b=true;
			for(int i=d;i>1;i--) {
				int t1=F(x,i),t2=F(x,i-1),t3=3-(t1+t2);
				if(t1==t2) continue;
				else b=false;
				int y=0;
				for(int j=d;j>0;j--) {
					y<<=2;
					y|=(j==i||j==(i-1))?t3:F(x,j);
				}
				if(!mp[y]) {
					mp[y]=true;
					que.push(P(y,p.second+1));
				}
			}
			if(b) {ans=p.second; break;}
		}
		printf(ans==-1?"NA\n":"%d\n",ans);
	}
}