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>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///


int main()
{
	string s;
	while (cin >> s)
	{
		if (s == "0") break;
		int l = s.size();
		map<string, int> used;
		used[s] = 0;
		queue<string> que;
		que.push(s);
		while (que.size())
		{
			string t = que.front();
			que.pop();
			bool f = true;
			REP(i, l)
			{
				if (t[i] != t[0]) f = false;
			}
			if (f)
			{
				cout << used[t] << endl;
				goto end;
			}
			REP(i, l-1)
			{
				if (t[i] != t[i + 1])
				{
					char c = 'r' + 'g' + 'b' - t[i] - t[i + 1];
					string tmp = t;
					tmp[i] = c; tmp[i + 1] = c;
					if (used.find(tmp) == used.end())
					{
						used[tmp] = used[t] + 1;
						que.push(tmp);
					}
				}
			}
		}
		puts("NA");
	end:;
	}

	return 0;
}

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

typedef pair<int, string> P;

int main()
{
    char worm[32];

    while (scanf("%s", worm), *worm != '0'){
        queue<P> que;

        for (que.push(P(0, string(worm))); que.size(); que.pop()){
            P tmp = que.front();
            string s = tmp.second;
            bool flag = true;

            for (int i = 0; i < s.length() - 1; i++){
                string t = s;

                if (t[i] != t[i + 1]){
                    flag = false;
                    if (t[i] != 'r' && t[i + 1] != 'r'){
                        t[i] = t[i + 1] = 'r';
                    }
                    else if (t[i] != 'g' && t[i + 1] != 'g'){
                        t[i] = t[i + 1] = 'g';
                    }
                    else {
                        t[i] = t[i + 1] = 'b';
                    }
                    que.push(P(tmp.first + 1, t));
                }
            }
            if (flag){
                printf("%d\n", tmp.first);
                break;
            }
        }
        if (que.empty()){
            puts("NA");
        }
    }

    return 0;
}