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<cstdio>
#include<map>
#include<queue>

using namespace std;

class wormcheck{
    public:
    string Worm;
    int Val;
    wormcheck(string W,int V){
        Worm=W;
        Val=V;
    }
};

int main(){
    string worm;
    while(cin>>worm){
        if(worm=="0")break;
        queue<wormcheck> que;
        que.push(wormcheck(worm,0));
        map<string,int> visit;
        while(1){
            if(que.empty()){cout<<"NA"<<endl;break;}
            wormcheck w = que.front();
            que.pop();
            if(visit[w.Worm]==1)continue;
            visit[w.Worm]=1;
            for(int i=1;i<w.Worm.size();i++){
                if(w.Worm[i]!=w.Worm[i-1]){
                    goto Skip;
                }
            }
            //printf("%s %d",w.Worm,w.Val);
            cout<<w.Val<<endl;
            break;
            Skip:;
            string subWorm = w.Worm;
            for(int i=1;i<w.Worm.size();i++){
                if(subWorm[i]!=subWorm[i-1]){
                    if(subWorm[i]!='b'&&subWorm[i-1]!='b'){
                        subWorm[i]='b';
                        subWorm[i-1]='b';
                    }
                    else if(subWorm[i]!='g'&&subWorm[i-1]!='g'){
                        subWorm[i]='g';
                        subWorm[i-1]='g';
                    }
                    else if(subWorm[i]!='r'&&subWorm[i-1]!='r'){
                        subWorm[i]='r';
                        subWorm[i-1]='r';
                    }
                    que.push(wormcheck(subWorm,w.Val+1));
                    subWorm=w.Worm;
                }
            }
        }

    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <queue>

using namespace std;

void solve()
{
	string str;
	while(cin >> str)
	{
		if(str == "0")
		{
			break;
		}
		queue<string> Que;
		Que.push(str);
		queue<string> NextQue;
		int depth = 0;
		while(true)
		{
			if(depth >= 10)
			{
				cout << "NA" << endl;
				break;
			}
			while(!Que.empty())
			{
				string worm = Que.front();
				Que.pop();
				bool same_color_flag = true;
				for(int i = 1; i < worm.size(); ++i)
				{
					if(worm[i] != worm[0])
					{
						same_color_flag = false;
					}
				}
				if(same_color_flag)
				{
					cout << depth << endl;
					goto END;
				}
				char before_color = worm[0];
				for(int i = 1; i < worm.size(); ++i)
				{
					if(before_color != worm[i])
					{
						string next = worm;
						if(before_color == 'b')
						{
							if(worm[i] == 'g')
							{
								next[i - 1] = 'r';
								next[i] = 'r';
							}
							else if(worm[i] == 'r')
							{
								next[i - 1] = 'g';
								next[i] = 'g';
							}
						}
						else if(before_color == 'g')
						{
							if(worm[i] == 'b')
							{
								next[i - 1] = 'r';
								next[i] = 'r';
							}
							else if(worm[i] == 'r')
							{
								next[i - 1] = 'b';
								next[i] = 'b';
							}
						}
						else if(before_color == 'r')
						{
							if(worm[i] == 'g')
							{
								next[i - 1] = 'b';
								next[i] = 'b';
							}
							else if(worm[i] == 'b')
							{
								next[i - 1] = 'g';
								next[i] = 'g';
							}
						}
						NextQue.push(next);
					}
					before_color = worm[i];
				}
			}
			while(!NextQue.empty())
			{
				Que.push(NextQue.front());
				NextQue.pop();
			}
			++depth;
		}
END:
		;
	}
}

int main()
{
	solve();
	return(0);
}