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 <iostream>
#include <vector>
#include <map>
#include <climits>
#include <queue>
using namespace std;

map<int,bool> isre;
map<int,int> dp;

int code(string s){
    int ret=0,kake=1;
    for(int i=0;i<s.size();i++){
        if(s[i]=='r') ret+=kake*1;
        else if(s[i]=='g') ret+=kake*2;
        else ret+=kake*3;
        kake*=4;
    }
    return ret;
}

int main(){
    string in;
    for(;;){
        cin>>in;
        if(in=="0") break;
        map<int,bool> isre;
        queue<string> nq;
        nq.push(in);
        int ret=-1;
        int cnt=0;
        while(!nq.empty()){
            int sz=nq.size();
            for(int i=0;i<sz;i++){
                string now=nq.front();
                nq.pop();
                int co=code(now);
                if(isre.find(co)!=isre.end() && isre[co]) continue;
                isre[co]=true;
                bool is=true;
                for(int j=0;j<now.size();j++){
                    if(now[j]!=now[0]) is=false;
                }
                if(is){
                    ret=cnt;
                    goto EXIT;
                }
                for(int j=1;j<now.size();j++){
                    string ne=now;
                    if(now[j-1]!=now[j]){
                        if(now[j-1]!='r'&&now[j]!='r'){
                            ne[j-1]=ne[j]='r';
                        }else if(now[j-1]!='g'&&now[j]!='g'){
                            ne[j-1]=ne[j]='g';
                        }else{
                            ne[j-1]=ne[j]='b';
                        }
                        nq.push(ne);
                    }
                }
            }
            cnt++;
        }
EXIT:   ;
        if(ret==-1) cout<<"NA"<<endl;
        else cout<<ret<<endl;
    }
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <cstring>
using namespace std;

typedef pair<string, int> P;

int bfs(string worm){
  queue<P> que;
  que.push(P(worm, 0));

  map<string, int>  d;

  while(!que.empty()){
    P p = que.front();
    que.pop();

    if(d[p.first])  continue;

    string str = p.first;

    bool flag = true;
    for(int i = 0; i < str.size()-1; i++){
      if(str[i] != str[i+1]){
        flag = false;
        break;
      }
    }

    if(flag) return p.second;

    d[p.first] = p.second;

    for(int i = 0; i < str.size()-1; i++){
      if(str[i] != str[i+1]){
        string next;
        char tmp;
        if(str[i] != 'r' && str[i+1] != 'r'){
          tmp = 'r';
        }else if(str[i] != 'b' && str[i+1] != 'r'){
          tmp = 'b';
        }else{
          tmp = 'g';
        }

        next = str;
        next[i] = next[i+1] = tmp;
        que.push(P(next, p.second+1));
      }
    }
  }
  return -1;
}

int main(void){
  string worm;
  while(cin >> worm && worm != "0"){
    int ans = bfs(worm);
    if(ans >= 0)  cout << ans << endl;
    else cout << "NA" << endl;
  }

  return 0;
}

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

using namespace std;

int main()
{
    string s;
    while (cin >> s, s[0] != '0') {
		map<string, bool> m;

		queue<string> q, r;

		q.push(s);

		int cnt = 0;

		while (q.size()) {
			while (q.size()) {
				string t = q.front(); q.pop();
				if (m[t]) continue;
				m[t] = true;
				bool f = true;
				for (int i = 0; i < t.size() - 1; ++i) {
					if ((t[i] == 'r' && t[i + 1] == 'b') || (t[i] == 'b' && t[i + 1] == 'r')) {
						char c = t[i], d = t[i + 1];
						t[i] = t[i + 1] = 'g';
						r.push(t);
						t[i] = c, t[i + 1] = d;
						f = false;
					} else if ((t[i] == 'b' && t[i + 1] == 'g') || (t[i] == 'g' && t[i + 1] == 'b')) {
						char c = t[i], d = t[i + 1];
						t[i] = t[i + 1] = 'r';
						r.push(t);
						t[i] = c, t[i + 1] = d;
						f = false;
					} else if ((t[i] == 'r' && t[i + 1] == 'g') || (t[i] == 'g' && t[i + 1] == 'r')) {
						char c = t[i], d = t[i + 1];
						t[i] = t[i + 1] = 'b';
						r.push(t);
						t[i] = c, t[i + 1] = d;
						f = false;
					}
				}
				if (f) {
                    cout << cnt << endl;
					goto nxt;
				}
			}
			while (r.size()) {
				string t = r.front(); r.pop();
				q.push(t);
			}
			cnt++;
		}

        cout << "NA" << endl;
nxt:;
	}
}