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, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 30;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };



int ans;
string str;
set<string> can_str;

int main() {
	while ( cin >> str && str != "0" ) {
		int ans = -1;
		queue<pair<string, int> > q;
		q.push( make_pair( str, 0 ) );
		while ( q.size() ) {
			string s = q.front().first;
			int n = q.front().second;
			q.pop();
			if ( can_str.count( s ) ) {
				continue;
			}
			can_str.insert( s );
			bool can_break = true;
			for ( int i = 1; i < s.size(); i++ ) {
				if ( s[ i ] != s[ i - 1 ] ) {
					can_break = false;
					break;
				}
			}
			if ( can_break ) {
				ans = n;
				break;
			}
			for ( int i = 1; i < s.size(); i++ ) {
				if ( s[ i ] != s[ i - 1 ] ) {
					string s_copy = s;
					if ( s[ i ] == 'r' ) {
						if ( s[ i - 1 ] == 'g' ) {
							s[ i ] = s[ i - 1 ] = 'b';
						}
						else {
							s[ i ] = s[ i - 1 ] = 'g';
						}
					}
					else if ( s[ i ] == 'g' ) {
						if ( s[ i - 1 ] == 'r' ) {
							s[ i ] = s[ i - 1 ] = 'b';
						}
						else {
							s[ i ] = s[ i - 1 ] = 'r';
						}
					}
					else if ( s[ i ] == 'b' ) {
						if ( s[ i - 1 ] == 'g' ) {
							s[ i ] = s[ i - 1 ] = 'r';
						}
						else {
							s[ i ] = s[ i - 1 ] = 'g';
						}
					}
					q.push( make_pair( s, n + 1 ) );
					s = s_copy;
				}
			}
		}
		if ( ans < 0 ) {
			printf( "NA\n" );
		}
		else {
			printf( "%d\n", ans );
		}
		can_str.clear();
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;

bool OK(string in){
	for(int i = 0;i<in.size();i++){
		if (in[0] != in[i]) return false;
	}
	return true;
}

char ch(char a, char b){
	if (a != 'r' && b != 'r') return 'r';
	else if (a != 'g' && b != 'g') return 'g';
	else return 'b';
}

int main(){
	string in;
	while(cin >> in){
		if (in == "0") break;
		queue<string> que;
		que.push(in);
		vector<string> check(pow(3, in.size()));
		int n = 0, N = 0;
		while(true){
			int que_size = que.size();
			bool flag = false;
			if(que_size == 0){
				cout << "NA" << endl;
				break;
			}
			for(int i = 0;i<que_size;i++){
				string que_front = que.front();
				que.pop();
				if(OK(que_front)){
					cout << n << endl;
					flag = true;
					break;
				}
				for (int j = 0; j < que_front.size() - 1; j++) {
					if (que_front[j] == que_front[j + 1]) continue;
					string for_push = que_front;
					for_push[j] = for_push[j + 1] = ch(que_front[j], que_front[j + 1]);
					for (int k = 0; k <= N; k++){
						if (check[k] == for_push) break;
						if(k == N){
							N++;
							check[N] = for_push;
							que.push(for_push);
							break;
						}
					}
				}
			}
			if (flag) break;
			n++;
		}

	}
	return 0;
}