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 <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    string s;
    while(cin >> s, s != "0"){
        map<string, int> m;
        queue<string> Q;
        Q.emplace(s);
        m[s] = 0;
        int p = 0;
        while(!Q.empty()){
            string x = Q.front(); Q.pop();
            int ok = 1;
            for (int i = 0; i+1 < x.size(); ++i) {
                if(x[i] != x[i+1]) {
                    ok = 0;
                    break;
                }
            }
            if(ok) {
                p = 1;
                cout << m[x] << "\n";
                break;
            }
            for (int i = 0; i+1 < x.size(); ++i) {
                if(x[i] == x[i+1]) continue;;
                string y = x;
                auto w = 'r'^'g'^'b'^x[i]^x[i+1];
                y[i] = y[i+1] = w;
                if(!m.count(y)){
                    m[y] = m[x]+1;
                    Q.emplace(y);
                }
            }
        }
        if(!p){
            puts("NA");
        }
    }
    return 0;
}

C++: #include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define rep(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
typedef pair<string, int> P;
/** Problem0179 : Mysterious Worm **/
int bfs(string in)
{
	queue<P> Q;
	map<string, int> d;
	
	Q.push(P(in, 0));
	
	while (Q.size()) {
		P p = Q.front(); Q.pop();
		string s = p.first;
		
		if (d[s]) continue;
		
		bool flag=true;
		rep(i, s.length()-1) {
			if (s[i] != s[i+1]) {
				flag = false;
				break;
			}
		}
		if (flag)
			return p.second;
		
		d[s] = 1;
		
		rep(i, s.length()-1) {
			string ns = s;
			if (s[i] != s[i+1]) {
				char c1 = s[i], c2 = s[i+1], nc;
				if (c1 != 'r' && c2 != 'r')
					nc = 'r';
				else if (c1 != 'g' && c2 != 'g')
					nc = 'g';
				else if (c1 != 'b' && c2 != 'b')
					nc = 'b';
				
				ns[i]=nc;
				ns[i+1]=nc;
				
				Q.push(P(ns, p.second+1));
			}
		}
	}
	return -1;
}

int main()
{
	string s;
	int ans;
	while (cin>>s, s!="0") {
		ans = bfs(s);
		if (ans >= 0)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef pair<int, string> PS;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

char color(char s, char l){
	if(s + l == 'r' + 'g') return 'b';
	if(s + l == 'r' + 'b') return 'g';
	if(s + l == 'b' + 'g') return 'r';
	return 0;
}

int main(void){
	
	string S;
	while(cin >> S){
		if(S == "0")break;
		int str_size = S.size();
		string R, G, B;
		rep(i, str_size){
			R += 'r';
			G += 'g';
			B += 'b';
		}
		bool flag = 0;
		queue<PS> que;
		que.push(PS(0, S));
		map<string, bool> state;
		while(que.size()){
			PS p = que.front(); que.pop();
			string str = p.second;
			
			if(str == R || str == G || str == B){
				flag = 1;
				cout << p.first << endl;
				break;
			}

			if(state[str]){
				continue;
			}
			state[str] = 1;

			rep(i, str.size()-1){
				if(str[i] != str[i+1]){
					char L = color(str[i], str[i+1]);
					string SS = str;
					SS[i] = L;
					SS[i+1] = L;
					que.push(PS(p.first+1,SS));
				}
			}
		}
		if(!flag) cout << "NA" << endl;
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}