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<string>
#include<iostream>

#define	mp	make_pair

using namespace std;

typedef	pair<int,int>	pii;

bool isHomochrome(int stat,int len){
	int color=stat&3;
	for(int i=0;i<len;i++,stat>>=2)	if((stat&3)!=color)	return false;
	return true;
}

int getColor(int stat,int i){
	return (stat>>(2*i))&3;
}

void setColor(int &stat,int i,int c){
	stat&=~(3<<(2*i));
	stat|=c<<(2*i);
}

int main(){
	static int memo[1<<20];
	for(string worm;cin>>worm,worm!="0";){
		int len=worm.length();
		for(int i=0;i<(1<<(2*len));i++)	memo[i]=1<<30;

		int inistat=0;
		for(int i=0;i<len;i++){
			if(worm[i]=='r')	inistat=(inistat<<2)+0;
			if(worm[i]=='g')	inistat=(inistat<<2)+1;
			if(worm[i]=='b')	inistat=(inistat<<2)+2;
		}

		int ans=-1;
		memo[inistat]=0;
		queue<pii> qu;	qu.push(mp(0,inistat));
		while(!qu.empty()){
			pii a=qu.front();	qu.pop();
			int t=a.first,stat=a.second;
			if(isHomochrome(stat,len)){ ans=t; break; }
			for(int i=0;i<len-1;i++){
				int clr[]={getColor(stat,i),getColor(stat,i+1)};
				if(clr[0]!=clr[1]){
					int nextclr;
					for(int c=0;c<=2;c++)	if(clr[0]!=c && clr[1]!=c)	nextclr=c;
					int nextstat=stat;
					setColor(nextstat,i  ,nextclr);
					setColor(nextstat,i+1,nextclr);
					if(t+1<memo[nextstat]){
						qu.push(mp(t+1,nextstat));
						memo[nextstat]=t+1;
					}
				}
			}
		}

		if(~ans)	cout<<ans<<endl;
		else		cout<<"NA"<<endl;
	}

	return 0;
}

C++: #include<iostream>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
 
using namespace std;
 
typedef pair<string,int> P;
 
bool judge(string str)
{
  for(int i=1;i<str.size();i++)
    if(str[0]!=str[i])return false;
  return true;
}
 
char getch(char c1, char c2)
{
  if(c1=='r'&&c2=='b'||c1=='b'&&c2=='r')return 'g';
  if(c1=='r'&&c2=='g'||c1=='g'&&c2=='r')return 'b';
  if(c1=='b'&&c2=='g'||c1=='g'&&c2=='b')return 'r';
}
 
int bfs(string str)
{
  queue<P> q;
  map<string,int> mat;
  q.push(P(str,0));
  while(!q.empty()){
    P p = q.front(); q.pop();
    string s=p.first;
    int n=p.second+1;
    if(mat[s])continue;
    if(judge(s)){
      return n-1;
    }
    mat[s]=n;
    for(int i=0;i<s.size()-1;i++){
      if(s[i]!=s[i+1]){
        string t=s;
        char ch=getch(t[i],t[i+1]);
        t[i]=t[i+1]=ch;
        q.push(P(t,n));
      }
    }
  }
  return -1;
}
 
int main(void)
{
  string str;
  while(cin>>str&&str!="0"){
    int res;
    res = bfs(str);
    if(~res)printf("%d\n",res);
    else puts("NA");
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

int clr_int(string str)
{
	int v = 0;
	int r = 1;
	for(int i = 0;i < str.size();i++)
	{
		switch(str[i])
		{
			case 'g':
			v += r;
			break;
			case 'b':
			v += r*2;
			break;
		}
		r *= 3;
	}
	return v;
}
string int_clr(int v,int l)
{
	string str;
	for(int i = 0;i < l;i++)
	{
		int r = v % 3;
		switch(r)
		{
			case 0:
			str += "r";
			break;
			case 1:
			str += "g";
			break;
			case 2:
			str += "b";
			break;
		}
		v /= 3;
	}
	return str;
}

vector<vector<int>> makeG(int l)
{
	auto grh = vector<vector<int>>();
	int r = 1;
	for(int i = 0;i < l;i++)
	{
		r *= 3;
	}
	for(int i = 0;i < r;i++)
	{
		auto str = int_clr(i,l);
		auto ary = vector<int>();
		for(int j = 0;j < (l-1);j++)
		{
			if(str[j] != str[j+1])
			{
				auto scp = string(str);
				switch(str[j])
				{
					case 'r':
					scp[j] = scp[j+1] = (str[j+1] == 'g')?'b':'g';
					ary.push_back(clr_int(scp));
					break;
					case 'g':
					scp[j] = scp[j+1] = (str[j+1] == 'b')?'r':'b';
					ary.push_back(clr_int(scp));
					break;
					case 'b':
					scp[j] = scp[j+1] = (str[j+1] == 'r')?'g':'r';
					ary.push_back(clr_int(scp));
					break;
				}
			}
		}
		grh.push_back(ary);
	}
	return grh;
}

int main()
{
	string str;
	cin >> str;
	while(str.size() > 1)
	{
		auto graph = makeG(str.size());
		//cout << "l:" << str.size() << "r:" << graph.size() << endl;
		queue<pair<int,int>> q;
		q.push(pair<int,int>(clr_int(str),0));
		auto table = vector<int>(graph.size(),0);
		int c = 0;
		while(q.size() > 0)
		{
			auto n = q.front();
			if(graph[n.first].size() == 0)
			{
				cout << n.second << endl;
				break;
			}
			else if(c == graph.size())
			{
				cout << "NA" << endl;
				break;
			}
			else
			{
				const int l = graph[n.first].size();
				for(int i = 0;i < l;i++)
				{
					if(table[graph[n.first][i]] == 0)
					{
						table[graph[n.first][i]] = 1;
						c++;
						q.push(pair<int,int>(graph[n.first][i],n.second+1));
					}
				}
			}
			q.pop();
		}
		if(q.size() == 0)
		{
			cout << "NA" << endl;
		}
		cin >> str;
	}
	return 0;
}