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

using namespace std;



bool judge(string s){
  for(int i = 1; i < s.length(); ++i) if(s[0] != s[i]) return false;
  return true;
}

int main(){
  char worm[16];
  while(scanf("%s", worm) && worm[0] != '0'){
    queue<string> q;
    q.push(string(worm));
    vector<string>f;
    f.push_back(string(worm));
    if(judge(q.front())){
      puts("0");
      continue;
    }
    int res = 1;
    while(!q.empty()){
      int q_size = (int)q.size();
      for(int j = 0; j < q_size; ++j){
	string tmp = q.front(); q.pop();
	int len = (int)tmp.length();
	for(int i = 0; i < len-1; ++i){
	  string n = tmp;
	  if(n[i] != n[i+1]){
	    if(n[i] == 'r' && n[i+1] == 'b' || n[i] == 'b' && n[i+1] == 'r') n[i] = n[i+1] = 'g';
	    else if(n[i]=='r'&&n[i+1]=='g' || n[i]=='g'&&n[i+1]=='r') n[i] = n[i+1] = 'b';
	    else if(n[i]=='g'&&n[i+1]=='b' || n[i]=='b'&&n[i+1]=='g') n[i] = n[i+1] = 'r';
	  }
	  ///	  puts(n.c_str());
	  if(find(f.begin(), f.end(), n) != f.end()) continue;

	  if(judge(n)){
	    printf("%d\n", res);
	    goto end;
	  }
	  f.push_back(n);
	  q.push(n);
	}
      }
      //      printf("%d\n", res);
      //      for(int i = 0; i < f.size(); ++i) puts(f[i].c_str());
      //      queue<string>t;
      //      t = q;
      //      while(!t.empty()){ puts(t.front().c_str()); t.pop();}
      //      puts("");
      
      res++;
    }
    puts("NA");
  end:;

  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int que[60000][10],flg[1050000];
int main(void){
	int p1,p2,i,j,len,time[60000],copy[10],f,x,ans,iro;
	char ca[11];
	while(1){
		cin>>ca;
		if(ca[0]=='0') break;
		len=strlen(ca);
		for(i=0;i<len;i++){
			if(ca[i]=='r') que[0][i]=0;
			if(ca[i]=='g') que[0][i]=1;
			if(ca[i]=='b') que[0][i]=2;
		}
		for(i=0;i<=1050000;i++) flg[i]=0;
		p1=0; p2=1; time[0]=0; ans=-1;
		while(1){
			if(p1==p2){
//				cout<<p1<<endl;
				break;
			}
/*			cout<<p1<<' ';
			for(i=0;i<len;i++) cout<<que[p1][i];
			cout<<' '<<time[p1]<<endl;*/
			f=1;
			for(i=0;i<len-1;i++){
				if(que[p1][i]!=que[p1][i+1]){
					f=0;
					break;
				}
			}
			if(f==1){
				ans=time[p1];
				break;
			}
			for(i=0;i<len-1;i++){
				for(j=0;j<len;j++) copy[j]=que[p1][j];
				if(copy[i]!=copy[i+1]){
					iro=copy[i]+copy[i+1];
					if(iro==3){
						copy[i]=0; copy[i+1]=0;
					}
					if(iro==2){
						copy[i]=1; copy[i+1]=1;
					}
					if(iro==1){
						copy[i]=2; copy[i+1]=2;
					}
				}
				x=copy[0];
				for(j=1;j<len;j++) x=x*4+copy[j];
				if(flg[x]==0){
					flg[x]=1; time[p2]=time[p1]+1;
					for(j=0;j<len;j++) que[p2][j]=copy[j];
					p2++;
				}
			}
			p1++;
		}
		if(ans==-1) cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define FOR(i, n, m) for(int i = (n);i < (m);i++)
#define REP(i, n) for(int i = 0;i < (n);i++)
#define PII pair<int, int>

using namespace std;

string worm;

int all(string a){
    char b = a[0];
    int cnt = 0;
    for(int i = 1;a[i] != '\0';i++){
        if(a[i] != b)
            cnt++;
    }
    return cnt;
}

int solve(){
    queue< pair<string, int> > que;
    que.push(pair<string, int>(worm, 0));

    if(all(worm) == 0){
        return 0;
    }

    while(que.size()){
        string worm1 = que.front().first;
        int num = que.front().second;
        que.pop();

        if(all(worm1) == 1){
            continue;
        }else if(all(worm1) == 0){
            return num;
        }
        for(int i = 0;i < strlen(worm1.c_str())-1;i++){
            string tmp = worm1;
            if(worm1[i] != worm1[i+1]){
                if((worm1[i] == 'b' && worm1[i+1] == 'g') || (worm1[i] == 'g' && worm1[i+1] == 'b')){
                    tmp[i] = tmp[i+1] = 'r';
                    que.push(pair<string, int>(tmp, num+1));
                }else if((worm1[i] == 'b' && worm1[i+1] == 'r') || (worm1[i] == 'r' && worm1[i+1] == 'b')){
                    tmp[i] = tmp[i+1] = 'g';
                    que.push(pair<string, int>(tmp, num+1));
                }else{
                    tmp[i] = tmp[i+1] = 'b';
                    que.push(pair<string, int> (tmp, num+1));
                }
            }
        }
        if(num > 10000)
            return -1;
    }

    return -1;
}

int main(void){
    cin >> worm;
    while(worm[0] != '0'){
        int result = solve();
        if(result >= 0){
            cout << result << endl;
        }else{
            cout << "NA" << endl;
        }
        cin >> worm;
    }

    return 0;
}