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++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int n;
int mp[1<<20];

int tohash(string &s){
	int a = 0;
	FOR(i,n){
		a *= 4;
		if(s[i] == 'r') a += 1;
		else if(s[i] == 'g') a += 2;
		else a += 3;
	}
	return a;
}

int decodeC(int hash,int i){
	return (hash >> (2*i)) & 3;
}

bool OK(int hash){
	int c = decodeC(hash,0);
	for(int i = 1; i < n; i++)
		if(decodeC(hash,i) != c) return false;
	return true;
}

void setC(int& hash,int i,int v){
	hash = (hash & ~(3 << (2*i))) | (v << (2*i));
}

typedef pair<int,int> P;

int main(){
	string s;
	while(cin>>s,s!="0"){
		n = sz(s);
		memset(mp,0x7F,sizeof(mp));
		mp[tohash(s)] = 0;

		int ans = -1;

		priority_queue<P,vector<P>,greater<P> > q;
		q.push(P(0,tohash(s)));

		while(!q.empty()){
			P p = q.top(); q.pop();
			int v = p.second;
			if(mp[v] < p.first) continue;

			if(OK(v)){
				ans = p.first;
				break;
			}

			for(int i = 0; i < n-1; i++){
				int hash = p.second;
				int c = decodeC(hash , i) ^ decodeC(hash, i + 1);
				if(c == 0) continue;
				setC(hash,i,c);
				setC(hash,i+1,c);
				if(mp[hash] > p.first + 1){
					mp[hash] = p.first + 1;
					q.push(P(p.first + 1,hash));
				}
			}
		}

		if(ans == -1) puts("NA");
		else cout << ans << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <string>
#include <queue>
#include <map>

using namespace std;

int main()
{
	char str[256];
	char col[4] = "rgb";
	
	while (scanf("%s", str) && str[0] !='0'){
		map<string, int> minTime;
		queue<string> q;
		string s = str;
		
		minTime[s] = 0;
		
		bool ng = true;
		for (q.push(s); !q.empty(); q.pop()){
			string look = q.front();
			
			bool flag = true;
			for (int i = 0; i < look.size() - 1; i++){
				if (look[i + 1] != look[i]) flag = false;
			}
			
			if (flag){
				printf("%d\n", minTime[look]);
				ng = false;
				break;
			}
			
			for (int c = 0; c < 3; c++){
				for (int i = 0; i < look.size() - 1; i++){
					if (look[i + 1] != col[c] && look[i] != col[c] && look[i + 1] != look[i]){
						string next = look;
						next[i + 1] = next[i] = col[c];
						if (minTime.count(next) > 0) continue;
						minTime[next] = minTime[look] + 1;
						q.push(next);
					}
				}
			}
		}
		if (ng) printf("NA\n");
	}
	
	return (0);
}

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

char kagasan(char A,char B){
	if(A=='r'&&B=='g')return 'b';
	if(A=='r'&&B=='b')return 'g';
	if(A=='g'&&B=='r')return 'b';
	if(A=='g'&&B=='b')return 'r';
	if(A=='b'&&B=='g')return 'r';
	if(A=='b'&&B=='r')return 'g';
}

int main(){
	string STR;
	while(cin>>STR){
		if(STR=="0")break;
		map<string,int>time;
		queue<string>name;
		name.push(STR);
		time[STR]=1;
		int flag=1;
		while(name.empty()==0){
			int mini=1;
			string s=name.front();
			for(int i=0;i<s.size()-1;i++){
				if(s[i]!=s[i+1]){
					mini=0;
					string ka=s;
					ka[i]=kagasan(s[i],s[i+1]);
					ka[i+1]=kagasan(s[i],s[i+1]);
					if(time[ka]==0){
						time[ka]=time[s]+1;
						name.push(ka);
						
					}
				}
			}
			if(mini){
				cout<<time[name.front()]-1<<endl;
				flag=0;
				break;
			}
			name.pop();
		}
		if(flag)cout<<"NA"<<endl;
	}
	return 0;
}