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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

typedef pair<string, int> P;

string input;
set<string> checked;
queue<P> que;

bool check_all_same(string s)
{
	char color = s[0];
	for (int i=1; i<s.length(); i++)
	{
		if (color != s[i]) return false;
	}
	return true;
}

char get_other(char a, char b)
{
	//             r     g     b
	bool arr[3] = {true, true, true};
	if (a == 'r') arr[0] = false;
	if (a == 'g') arr[1] = false;
	if (a == 'b') arr[2] = false;
	if (b == 'r') arr[0] = false;
	if (b == 'g') arr[1] = false;
	if (b == 'b') arr[2] = false;
	rep(i,3)
	{
		if (arr[i])
		{
			if (i == 0) return 'r';
			else if (i == 1) return 'g';
			else return 'b';
		}
	}
}

vector<string> get_next_str(string in)
{
	vector<string> ret;

	for (int i=0; i<in.length()-1; i++)
	{
		if (in[i] != in[i + 1])
		{
			string next(in);
			char new_char = get_other(in[i], in[i + 1]);
			next[i] = new_char;
			next[i + 1] = new_char;

			ret.push_back(next);
		}
	}

	return ret;
}

signed main()
{
	/*
	vector<string> nexts = get_next_str("rbgrg");
	for (int i=0; i<nexts.size(); i++)
	{
		cout << nexts[i] << endl;
	}
	*/

	while (true)
	{
		cin >> input;
		if (input == "0") break;

		checked.clear();
		checked.insert(input);

		queue<P> empty;
		swap(que, empty);
		que.push(P(input, 0));

		bool find = false;

		while (que.size())
		{
			P p = que.front(); que.pop();
			if (check_all_same(p.first))
			{
				printf("%d\n", p.second);
				find = true;
				break;
			}

			vector<string> nexts = get_next_str(p.first);
			rep(i,nexts.size())
			{
				if (checked.find(nexts[i]) == checked.end())
				{
					que.push(P(nexts[i], p.second + 1));
					checked.insert(nexts[i]);
				}
			}
		}

		if (!find) printf("NA\n");
	}
}

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

struct worm{
	int c;
	string s;
};
queue<worm> que;
bool str_check(string str){
	for(int i = 1;i < str.size();++i){
		if(str[0] != str[i]) return false;
	}
	return true;
}
int f(){
	while(que.size()){
		worm w = que.front(),tmp;
		que.pop();
		int n = w.c;
		if(n > 10) break;
		string str = w.s;
		if(str_check(str)){
			return n;
		}
		for(int i = 0;i < str.size() - 1;++i){
			if(str[i] != str[i+1]){
				if((str[i] == 'r' && str[i+1] == 'b') || (str[i] == 'b' && str[i+1] == 'r')){
					str[i] = str[i+1] = 'g';
				}
				else if((str[i] == 'g' && str[i+1] == 'b') || (str[i] == 'b' && str[i+1] == 'g')){
					str[i] = str[i+1] = 'r';
				}
				else {
					str[i] = str[i+1] = 'b';
				}
				tmp.s = str;
				tmp.c = n + 1;
				que.push(tmp);
				str = w.s;
			}
		}
	}
	return -1;
}
int main(){
	string str;
	worm w;
	while(cin >> str){
		if(str == "0") break;
		w.s = str;
		w.c = 0;
		que.push(w);
		int n = f();
		if(n == -1) cout << "NA\n";
		else cout << n << endl;
		while(que.size()){
			que.pop();
		}
	}
	return 0;
}