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 <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

int checkTable[59049];

struct Data{
	int number,depth;
};

int getNum(char ch){
	switch(ch){
	case 'r':return 0;
	case 'g':return 1;
	case 'b':return 2;
	}
	return -1; //must not reach here
}

int getWeight(int w){
	switch(w){
	case 0:return 1;
	case 1:return 3;
	case 2:return 9;
	case 3:return 27;
	case 4:return 81;
	case 5:return 243;
	case 6:return 729;
	case 7:return 2187;
	case 8:return 6561;
	case 9:return 19683;
	}
	return -1;	//must not reach here
}

int translateData(char table[],int size){
	int ret = 0;

	for(int i = 0; i < size; i++){
		ret += getNum(table[i])*pow(3,(size-1)-i);
	}

	return ret;
}

void setData(char warm[],int code,int size){
	int tmp;
	for(int i = 0; i < size; i++){
		tmp = code/getWeight(size-1-i);
		if(tmp == 0){
			warm[i] = 'r';
		}else if(tmp == 1){
			warm[i] = 'g';
		}else{
			warm[i] = 'b';
		}
		code %= getWeight(size-1-i);
	}
}

int dfs(char warm[],int size){

	int ret = -1;
	Data data;
	data.number = translateData(warm,size);
	data.depth = 0;

	queue<Data> Q;
	Q.push(data);

	Data tmp;
	char tmp_warm[11], next_warm[11];
	int code;

	while(!Q.empty()){
		tmp = Q.front();
		Q.pop();

		setData(tmp_warm,tmp.number,size);

		bool FLG = true;
		for(int i = 0; i < size-1; i++){
			if(tmp_warm[i] != tmp_warm[i+1]){
				FLG = false;
				break;
			}
		}
		if(FLG){
			while(!Q.empty())Q.pop();
			ret = tmp.depth;
			break;
		}

		for(int i = 0; i < size-1; i++){
			if(tmp_warm[i] != tmp_warm[i+1]){
				Data new_data;
				for(int k = 0; k < size; k++)next_warm[k] = tmp_warm[k];

				if(tmp_warm[i] != 'r' && tmp_warm[i+1] != 'r'){
					next_warm[i] = 'r';
					next_warm[i+1] = 'r';
				}else if(tmp_warm[i] != 'g' && tmp_warm[i+1] != 'g'){
					next_warm[i] = 'g';
					next_warm[i+1] = 'g';
				}else{
					next_warm[i] = 'b';
					next_warm[i+1] = 'b';
				}

				code = translateData(next_warm,size);
				if(checkTable[code] == -1){
					checkTable[code] = 1;
					new_data.number = code;
					new_data.depth = tmp.depth+1;
					Q.push(new_data);
				}
			}
		}
	}
	return ret;
}

int main(){

	int size,minimum;
	char buf[11];

	while(true){
		scanf("%s",buf);
		if(buf[0] == '0')break;

		for(size = 0;buf[size] != '\0';size++);

		for(int i = 0; i < 59049; i++)checkTable[i] = -1;

		checkTable[translateData(buf,size)] = 1;

		minimum = dfs(buf,size);

		if(minimum == -1)printf("NA\n");
		else{
			printf("%d\n",minimum);
		}
	}

    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<string, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

char judge(char a, char b){
    if((a == 'r' && b == 'b') || (a == 'b' && b == 'r')) return 'g';
    if((a == 'b' && b == 'g') || (a == 'g' && b == 'b')) return 'r';
    if((a == 'g' && b == 'r') || (a == 'r' && b == 'g')) return 'b';
}

int main(){
    while(1){
        string s; cin>>s;
        if(s == "0") return 0;

        queue<P> que;
        que.push(P(s, 0));
        map<string, bool> visited;
        visited[s] = true;
        bool isNA = true;
        while(que.size()){
            string t;
            int cnt;
            tie(t, cnt) = que.front();
            que.pop();

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

            if(valid){
                isNA = false;
                cout << cnt << endl;
                break;
            }

            for(int i=0; i<t.size()-1; i++){
                if(t[i] != t[i+1]){
                    string nt = t;
                    nt[i] = nt[i+1] = judge(t[i], t[i+1]);

                    if(!visited[nt]){
                        visited[nt] = true;
                        que.push(P(nt, cnt+1));
                    }
                }
            }
        }

        if(isNA){
            cout << "NA" << endl;
        }
    }
}