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<stdio.h>
#define M 1000000000
int l;
int d[110][60000]={0};
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 zMIN(int a,int b){
  if(b==0)return a;
  if(a==0||a>b)return b;
  return a;
}
int main(){
  int i,j,k,n,a[20];
  char s[20];
  while(scanf("%s",s),s[1]){
    for(i=0;i<110;i++){
      for(j=0;j<60000;j++)d[i][j]=0;
    }
    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;
    }
    d[0][n]=1;
    for(i=n=0;i<l;i++)n=n*3+2;//printf("%d\n",n);
    for(i=0;i<100;i++){
      for(j=0;j<n;j++){
	if(d[i][j]==0)continue;
	d[i+1][j]=zMIN(d[i+1][j],d[i][j]);
	for(k=1;k<l;k++){
	  f(j,a);
	  a[k]=a[k-1]=(6-a[k]-a[k-1])%3;
	  d[i+1][g(a)]=zMIN(d[i+1][g(a)],d[i][j]+1);
	}
      }
    }
    d[100][0]=zMIN(d[100][0],zMIN(d[100][n/2],d[100][n]));
    if(d[100][0])printf("%d\n",d[100][0]-1);
    else         printf("NA\n");
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>

using namespace std;

int solve(string s)
{
    map<string, bool> mem;
    queue<pair<string, int> > q;

    q.push(pair<string, int>(s, 0));
    mem[s] = true;

    while (!q.empty()) {
        pair<string, int> tmp;
        bool flag = true;

        tmp = q.front();
        q.pop();

        // Check
        for (int i = 1; i < tmp.first.size(); i++)
            if (tmp.first[0] != tmp.first[i]) {
                flag = false;
                break;
            }
        if (flag) 
            return tmp.second;

        for (int i = 0; i < tmp.first.size() - 1; i++) {
            if (tmp.first[i] != tmp.first[i + 1]) {
                string str = tmp.first;
                int num = (int)(tmp.first[i] + tmp.first[i + 1]);

                if (num == 217) 
                    str[i] = str[i + 1] = 'b';
                else if (num == 212) 
                    str[i] = str[i + 1] = 'g';
                else if (num == 201) 
                    str[i] = str[i + 1] = 'r';

                if (mem.count(str) == 0) {
                    q.push(pair<string, int>(str, tmp.second + 1));
                    mem[str] = true;
                }
            }
        }
    }
    return -1;
}

int main()
{
    string inp;

    while (cin >> inp, inp != "0") {
        int num = solve(inp);

        if (num >= 0)
            cout << num << endl;
        else 
            cout << "NA" << endl;
    }

    return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define EPS 1e-8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define MAX_v 100
typedef long long ll;
typedef unsigned long long ull;
typedef pair<string,int> P;
int main(){
	string s;
	while(cin >> s , s!="0"){
		int len = s.size();
		int check = -1;
		queue<P> Q;
		Q.push(P(s,0));
		vector<string> vis;
		while( Q.size() ){
			P p = Q.front();Q.pop();
			string tmp = p.first;
			string k = p.first;
			//cout << "D:" << tmp << endl;
			bool f = false;
			rep(i,len-1){
				if(tmp[i] != tmp[i+1]){f = true;break;}
			}
			if(!f){check = p.second;break;}
			if(find(vis.begin(),vis.end(),tmp) != vis.end())continue;
			vis.push_back(tmp);
			rep(i,len-1){
				if(tmp[i] < tmp[i+1]){
					swap(k[i],k[i+1]);
				}
				if(k[i]=='r'&&k[i+1]=='g'){k[i]=k[i+1]='b';Q.push(P(k,p.second+1));}
				else if(k[i]=='r'&&k[i+1]=='b'){k[i]=k[i+1]='g';Q.push(P(k,p.second+1));}
				else if(k[i]=='g'&&k[i+1]=='b'){k[i]=k[i+1]='r';Q.push(P(k,p.second+1));}
				k = p.first;
			}
		}
		(check != -1) ? printf("%d\n",check) : puts("NA");
	}
	return 0;
}