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 <queue>
using namespace std;
queue<int> q;
int t[59049];
int INF=100000;
int encode(vector<int> vc){
	int p=0;
	for(int i=0;i<vc.size();i++){
		p=p*3+vc[i];
	}
	return p;
}
vector<int> decode(int p,int n){
	vector<int> vc(n);
	for(int i=0;i<n;i++){
		vc[n-i-1]=p%3;
		p/=3;
	}
	return vc;
}
int main(){
	while(true){
		for(int i=0;i<59049;i++) t[i]=INF;
		string s;
		cin >> s;
		int n=s.size(),start=0,goal=1;
		for(int i=0;i<n;i++) goal*=3;
		goal-=1;
		for(int i=0;i<n;i++){
			if(s[i]=='r') start=start*3+2;
			if(s[i]=='g') start=start*3+1;
			if(s[i]=='b') start=start*3;
		}
		q.push(start);
		t[start]=0;
		int ok=-1;
		while(q.size()){
			int p=q.front();
			if(p==0 || p==goal/2 || p==goal){
				ok=p;
				while(q.size()) q.pop(); 
				break;
			}
			q.pop();
			vector<int> vc=decode(p,n);
			for(int i=0;i<n-1;i++){
				if(vc[i]==vc[i+1]) continue;
				vector<int> vc_c=vc;
				vc_c[i]=vc_c[i+1]=((vc[i]+vc[i+1])*2)%3;
				int hoge=encode(vc_c);
				if(t[hoge]!=INF) continue;
				q.push(hoge);
				t[hoge]=t[p]+1;
			}
		}
		if(ok==-1) cout << "NA" << endl;
		else cout << t[ok] << endl;
	}
	return 0;
}

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

struct bug{
	string name;
	int tim;
};

int main()
{
	string worm;
	while(cin >> worm){
		if(worm == "0"){
			break;
		}
		map<string,int> memo;
		bug w;
		w.name = worm;
		w.tim = 0;
		queue<bug> que;
		que.push(w);
		bool fl = false;
		while(que.size()){
			bug tmp = que.front();
			que.pop();
			//êF»è
			bool flag = true;
			for(int i=0;i<tmp.name.size();i++){
				if(tmp.name[0] != tmp.name[i]){
					flag = false;
					break;
				}
			}
			if(flag){
				fl = true;
				cout << tmp.tim << endl;
				break;
			}
			//êxo½©»è
			if(memo.find(tmp.name) != memo.end())
				continue;
			else
				memo[tmp.name] = tmp.tim;

			//ÇÁ
			for(int i=0;i<tmp.name.size();i++){
				if(tmp.name[i] != tmp.name[i+1]){
					bug t = tmp;
					if((tmp.name[i] == 'r' && tmp.name[i+1] == 'g') || (tmp.name[i] == 'g' && tmp.name[i+1] == 'r') ){
						t.name[i] = 'b'; t.name[i+1] = 'b';
					}else if((tmp.name[i] == 'g' && tmp.name[i+1] == 'b') || (tmp.name[i] == 'b' && tmp.name[i+1] == 'g') ){
						t.name[i] = 'r'; t.name[i+1] = 'r';
					}else if((tmp.name[i] == 'b' && tmp.name[i+1] == 'r') || (tmp.name[i] == 'r' && tmp.name[i+1] == 'b') ){
						t.name[i] = 'g'; t.name[i+1] = 'g';
					}
					t.tim++;
					que.push(t);
				}
			}
		}
		if(!fl)
			cout << "NA" << endl;
	}
	return 0;
}

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

int main(){
	int n, g, b, c, x, y, i, c1, c2;
	char s[20];
	for(; cin >> s, *s - '0';){
		bitset<0xAAAAB> t;
		queue<int> q;
		for( x = n = 0; s[n]; n++ ){
			x |= ( s[n] == 'b' ? 2 : s[n] == 'g' ) << ( 2 * n );
		}
		n *= 2;
		g = 0x55555 & ( 1 << n ) - 1;
		b = 0xAAAAA & ( 1 << n ) - 1;

		q.push(x);
		q.push(-1);
		c = 0;
		for(; q.size() > 1; ){
			x = q.front();
			if( !x || x == g || x == b ){	break;	}
			q.pop();
			if( x == -1 ){	++c;	q.push(-1);	}
			else{
				c1 = x & 3;
				for( i = 2; i < n; i += 2 ){
					c2 = x >> i & 3;
					if( c1 != c2 ){
						y = x & ~( 15 << i - 2 );
						y |= ( 3 - c1 - c2 ) * 5 << i - 2;
						if( !t[y] ){
							t.set(y);
							q.push(y);
						}
					}
					c1 = c2;
				}
			}
		}
		if( q.size() > 1 ){	cout << c << "\n";	}
		else{	cout << "NA\n";	}
	}
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using namespace std;

const int INF = 1e+9;
typedef pair<int, int> P;

int ans = 0;

string change(string s, int a, int b){
	if((s[a] == 'r' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'r'))
		s[b] = s[a] = 'b';
	else if((s[a] == 'b' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'b'))
		s[b] = s[a] = 'r';
	else
		s[b] = s[a] = 'g';
	
	return s;
}

void bfs(string s, int res){
	if(res >= ans)
		return;
	for(int i = 0; i < s.size() - 1; ++i){
		if(s[i] != s[i + 1])
			break;
		if(i == s.size() - 2){
			ans = min(res, ans);
			return;
		}
	}
	if(res >= 10)
		return;
	for(int i = 0; i < s.size() - 1; ++i){
		if(s[i] != s[i + 1]){
			string str = change(s, i, i + 1);
			bfs(str, res + 1);
		}
	}
}

int main() {
	string s;
	while(cin >> s){
		if(s == "0")
			break;
		ans = INF;
		bfs(s, 0);
		if(ans == INF)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
	return 0;
}