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 <string>
#include <map>
#include <queue>
#include <cstring>
using namespace std;

int ar[11], ag[11], ab[11];
bool checked[59049];
int p3[11];
int change[9] = {0, 8, 4, 8, 4, 0, 4, 0, 8};

int conv(string s) {
	int ret = 0;
	for (int i=0; i<(int)s.size(); ++i) {
		ret *= 3;
		if (s[i] == 'g') ret += 1;
		if (s[i] == 'b') ret += 2;
	}
	return ret;
}

int rotate(int x, int n) {
	int ret = 0;
	for (int i=0; i<n; ++i) {
		ret = ret * 3 + (x % 3 + 1) % 3;
		x /= 3;
	}
	return ret;
}

int main() {
	p3[0] = 1;
	for (int i=1; i<=10; ++i) {
		p3[i] = p3[i-1] * 3;
	}
	ar[0] = ag[0] = ab[0] = 0;
	for (int i=1; i<=10; ++i) {
		ar[i] = ar[i-1];
		ag[i] = ag[i-1] * 3 + 1;
		ab[i] = ab[i-1] * 3 + 2;
	}
	string s;
	while (cin >> s, s != "0") {
		memset(checked, false, sizeof checked);
		int n = s.size();
		int first = conv(s);
		queue<pair<int,int> > q;
		q.push(make_pair(first, 0));
		checked[first] = true;
		int res = -1;
		while ( ! q.empty()) {
			int col = q.front().first;
			int t = q.front().second;
			q.pop();
			if (col == ar[n] || col == ag[n] || col == ab[n]) {
				res = t;
				break;
			}
			for (int i=0; i<n-1; ++i) {
				int replace = change[(col / p3[i]) % 9];
				int nx = (col / p3[i+2]) * p3[i+2] + replace * p3[i] + (col % p3[i]);
				if ( ! checked[nx]) {
					q.push(make_pair(nx, t+1));
					checked[nx] = true; nx = rotate(nx, n);
					checked[nx] = true; nx = rotate(nx, n);
					checked[nx] = true;
				}
			}
		}
		if (res < 0) cout << "NA" << endl;
		else cout << res << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
bool same(string s) {
	rep(i,s.size()-1) {
		if(s[i+1] != s[i]) return false;
	}
	return true;
}
char change(char a, char b) {
	char c[3] = {'r', 'b', 'g'};
	rep(i,3) {
		if(a != c[i] && b != c[i]) return c[i];
	}
}
int bfs(string str) {
	queue<string> q;
	q.push(str);
	map<string, int> m;
	m[str] = 0;
	while(!q.empty()) {
		string s = q.front(); q.pop();
		if(same(s)) return m[s];
		rep(i,s.size()-1) {
			if(s[i+1] != s[i]) {
				string ns = s;
				ns[i+1] = ns[i] = change(ns[i+1],ns[i]);
				if(!m[ns]) {
					q.push(ns);
					m[ns] = m[s] + 1;
				}
			}
		}
	}
	return -1;
}
int main(){
	string s;
	while(cin >> s) {
		if(s =="0") break;
		int ret = bfs(s);
		if(ret == -1) cout << "NA" << endl;
		else cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
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();

		//d¡p^[Ícontinue
		if(d[p.first]) continue;

		string str = p.first;

		//SF¯¶©Ç¤©Ì`FbN
		bool flag = false;
		for(int i=0;i<str.size()-1;i++){
			if(str[i] != str[i+1]){
				flag = true;
				break;
			}
		}
		//¯¶Èçreturn
		if(!flag) return p.second;

		//d¡p^[»Êp
		d[p.first] = p.second;

		for(int i=0;i<str.size()-1;i++){
			//×èí¹ÅFªÙÈéÆ«
			if(str[i] != str[i+1]){
				char temp;
				string next;

				//tempÉÜÜêÈ¢êFðãü
				if(str[i] != 'b' && str[i+1] != 'b'){
					temp = 'b';
				}else if(str[i] != 'g' && str[i+1] != 'g'){
					temp = 'g';
				}else{
					temp = 'r';
				}

				//Ï»ãÌÔðnextÉlßÞ
				next = str;
				next[i] = next[i+1] = temp;

				//L[ÉvbV
				que.push(P(next, p.second+1));
			}
		}
	}
	return -1;
}

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