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 <utility>
#include <string>
#include <queue>
#include <set>

using namespace std;

static int
validate (
  const char * s
  )
{
  while ( *( ++s ) != '\0' )
  {
    if ( s[ -1 ] != s[ 0 ] )  return ( 0 );
  }

  return ( 1 );
}

/** Application main entry point. */
int
main (
  int     argc,
  char  * argv[ ]
  )
{
  int i;

  for ( ; ; )
  {
    typedef pair<string,int> Point;
    queue<Point> lis;
    set<string> h;
    char si[ 11 ];

    scanf ( " %s", si );
    if ( *si == '0' ) break ;

    lis.push ( make_pair ( si, 0 ) );
    h.insert ( si );
    while ( !lis.empty ( ) )
    {
      const Point s = lis.front ( );
      lis.pop ( );

      if ( validate ( s.first.c_str ( ) ) )
      {
        printf ( "%d\n", s.second );
        goto ESC;
      }

      for ( i = 1; i < s.first.size ( ); ++i )
      {
        if ( s.first[ i - 1 ] != s.first[ i ] )
        {
          string g = s.first;

          if ( ( g[ i - 1 ] == 'r' && g[ i ] == 'g' )
            || ( g[ i - 1 ] == 'g' && g[ i ] == 'r' ) )
          {
            g.replace ( i - 1, 2, "bb" );
          }
          if ( ( g[ i - 1 ] == 'g' && g[ i ] == 'b' )
            || ( g[ i - 1 ] == 'b' && g[ i ] == 'g' ) )
          {
            g.replace ( i - 1, 2, "rr" );
          }
          if ( ( g[ i - 1 ] == 'b' && g[ i ] == 'r' )
            || ( g[ i - 1 ] == 'r' && g[ i ] == 'b' ) )
          {
            g.replace ( i - 1, 2, "gg" );
          }

          if ( h.find ( g ) == h.end ( ) )
          {
            lis.push ( make_pair ( g, s.second + 1 ) );
            h.insert ( g );
          }
        }
      }
    }
    puts ( "NA" );

ESC: ;
  }

  return ( 0 );
}

C++: #include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

#define reps(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reps(i, 0, n) 

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main()
{
	char str[16];
	while(scanf("%s", str), str[0]!='0'){
		int n = strlen(str);
		int ans = -1;
		set<string> visited;
		queue<pair<string, int> > Q;
		Q.push(make_pair(str, 0));
		while(!Q.empty()){
			pair<string, int> data = Q.front();
			Q.pop();
			rep(i, n-1){
				char a = min(data.first[i], data.first[i+1]);
				char b = max(data.first[i], data.first[i+1]);
				if(a == b)
					continue;
				
				char nc;
				if(a == 'b')
					nc = b=='g' ? 'r' : 'g';
				else
					nc = 'b';
				
				string next = data.first;
				next[i] = next[i+1] = nc;
				if(count(next.begin(), next.end(), nc) == n){
					ans = data.second + 1;
					goto end;
				}
				if(!visited.count(next)){
					visited.insert(next);
					Q.push(make_pair(next, data.second+1));
				}
			}
		}
end:
		if(ans == -1)
			puts("NA");
		else
			printf("%d\n", ans);
	}
	return 0;
}

C++: #include<stdio.h>
#define M 1000000000
int l;
void f(int n,int *a){//kakunou
  int i;
  for(i=0;i<l;n/=3)a[i++]=n%3;
}
int g(int *a){
  int n=0,i;
  for(i=l;i;i--)n=n*3+a[i-1];
}
int main(){
  char s[20];
  int i,j,k,mi,n,r,a[12],c[60000],h[1000000]={-M};
  while(scanf("%s",s),s[0]-'0'){
    for(i=0;i<60000;i++)c[i]=M;
    for(i=r=1;i<1000000;i++)h[i]=M;
    for(l=n=0;s[l];l++){
      if(s[l]=='r')n=n*3+0;
      if(s[l]=='g')n=n*3+1;
      if(s[l]=='b')n=n*3+2;
    }
    c[mi=n]=0;
    for(i=n=0;i<l;i++)n=n*3+1;//printf("%d\n",n);
    if(mi==0||mi==n||mi==n*2){
      printf("0\n");
      continue;
    }
     while(1){
      for(i=1;i<l;i++){//printf("%d\n",mi);
	f(mi,a);
	a[i-1]=a[i]=(6-a[i-1]-a[i])%3;
		j=g(a);//printf("%d\n",j);
	
	if(c[j]>c[mi]+1){
	  c[j]=c[mi]+1;//printf("%d\n",c[j]);
	  k=c[j]*60000+j;//printf("k%d\n",k);
	  for(j=r++;h[j/2]>k;j/=2)h[j]=h[j/2];
	  h[j]=k;//printf("%d\n",j);
	}
      }
      mi=h[i=1]%60000;//printf("%d\n",mi);
      if(mi==0||mi==n||mi==n*2)break;
      k=h[--r];
      for(h[r]=M;k>h[i];i=j){
	h[i]=h[j=i*2];
	if(h[i]>h[j+1])h[i]=h[j=j+1];
      }//printf("mi%d\n",mi);
      if(r==1)break;
      h[i/2]=k;
    }
    if(r-1||k==-1)printf("%d\n",c[mi]);
    else          printf("NA\n");
  }
  return 0;
}