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++: // AOJ 0179
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<functional>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

typedef long long int integer;

bool end(string s) {
  char c = s[0];
  int len = s.size();
  REP(i, len) {
    if (c != s[i]) {
      return false;
    }
  }
  return true;
}

vector<string> gonext(string worm) {
  vector<string> v;
  for (int i = 1, len = worm.length(); i < len; i++) {
    if (worm[i] == worm[i-1]) continue;
    string s = worm;
    char c = 'r' + 'g' + 'b' - worm[i] - worm[i-1];
    s[i] = s[i-1] = c;
    v.push_back(s);
  }
  return v;
}

typedef pair<integer,string> P;

integer solve(string worm) {
  priority_queue<P,vector<P>,greater<P> > q;
  set<string> s;
  q.push(make_pair(0,worm));
  while (!q.empty()) {
    P w = q.top();
    q.pop();
    if (end(w.second)) return w.first;
    vector<string> nxts = gonext(w.second);
    for (auto nxt : nxts) {
      if (s.find(nxt) == s.end()) {
        q.push(make_pair(w.first + 1, nxt));
        s.insert(nxt);
      }
    }
  }
  return -1;
}

int main() {
  string s;
  while (cin >> s and s != "0") {
    integer ans = solve(s);
    if (ans == -1) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
queue <pair<string,int> > worm;
map <string,int> M;
int s1,n;
string str,strrr,sample[3]={"gggggggggg","bbbbbbbbbb","rrrrrrrrrr"},worms[3];
bool flag=false;

int main(){
    while(1){
        cin >> str;
        if(str=="0")break;
            s1=str.size();
            for(int i=0;i<3;i++)worms[i]=sample[i].substr(0,s1);
            worm.push(make_pair(str,0));


        while(!worm.empty()){
            str=worm.front().first;
            n=worm.front().second;
            worm.pop();
            if(M[str]>0);
            else{
            //cout << n << endl;
            if(s1*2<n)break;
            M[str]++;
            if(str==worms[0]||str==worms[1]||str==worms[2]){
                    flag=true;
                cout  << n << endl;
                while(!worm.empty())worm.pop();
                break;
            }else{
                for(int i=1;i<s1;i++){
                    if(str[i]!=str[i-1]){
                        if(str[i]!='r'&&str[i-1]!='r'){
                            strrr=str;
                            str[i]='r',str[i-1]='r';
                            worm.push(make_pair(str,n+1));
                            str=strrr;
                        }
                        if(str[i]!='b'&&str[i-1]!='b'){
                            strrr=str;
                            str[i]='b',str[i-1]='b';
                            worm.push(make_pair(str,n+1));
                            str=strrr;
                        }
                        if(str[i]!='g'&&str[i-1]!='g'){
                            strrr=str;
                            str[i]='g',str[i-1]='g';
                            worm.push(make_pair(str,n+1));
                            str=strrr;
                        }
                    }
                }

            }
        }

    //end:
    }

        if(flag==false)cout << "NA" << endl;
        flag=false;
        while(!worm.empty())worm.pop();
        n=0;
        M.clear();

    }

return 0;
}

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

using namespace std;

int main()
{
	char s[11];
	while (~scanf("%s", 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) {
					printf("%d\n", cnt);
					goto nxt;
				}
			}
			while (r.size()) {
				string t = r.front(); r.pop();
				q.push(t);
			}
			cnt++;
		}

		printf("NA\n");
nxt:;
	}
}