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 <vector>
#include <map>
#include <climits>
using namespace std;

map<string,bool> isre;
map<string,int> dp;

int solve(string st){
    if(dp.find(st)==dp.end()){
        bool is=true;
        for(int i=0;i<st.size();i++){
            if(st[i]!=st[0]) is=false;
        }
        if(is) return dp[st]=0;
        if(isre[st]) return dp[st]=-1;
        isre[st]=true;
        int miti=INT_MAX;
        for(int i=1;i<st.size();i++){
            string ne=st;
            if(st[i-1]!=st[i]){
                if(st[i-1]!='r'&&st[i]!='r'){
                    ne[i-1]=ne[i]='r';
                }else if(st[i-1]!='g'&&st[i]!='g'){
                    ne[i-1]=ne[i]='g';
                }else{
                    ne[i-1]=ne[i]='b';
                }
            }
            int ret=solve(ne);
            if(ret!=-1) miti=min(miti,ret+1);
        }
        isre[st]=false;
        if(miti==INT_MAX) return dp[st]=-1;
        else return dp[st]=miti;
    }else return dp[st];
}

int main(){
    string in;
    for(;;){
        cin>>in;
        if(in=="0") break;
        isre=map<string,bool>();
        dp=map<string,int>();
        int ret=solve(in);
        if(ret==-1) cout<<"NA"<<endl;
        else cout<<ret<<endl;
    }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int dist[60000];
vector<int> x[60000];
string S; queue<int>Q;
int power[12], p2[12];
int main() {
	power[0] = 1; p2[0] = 1;
	for (int i = 1; i < 12; i++)power[i] = power[i - 1] * 3;
	for (int i = 1; i < 12; i++)p2[i] = p2[i - 1] + power[i];
	while (true) {
		cin >> S; if (S == "0") break;
		int bit[12], c[12], D = 0;
		for (int i = 0; i < S.size(); i++) {
			if (S[i] == 'r')D += 0 * power[i];
			if (S[i] == 'g')D += 1 * power[i];
			if (S[i] == 'b')D += 2 * power[i];
		}
		for (int i = 0; i < 60000; i++) {
			dist[i] = 99999; x[i].clear();
		}
		for (int i = 0; i < power[S.size()]; i++) {
			for (int j = 0; j < S.size(); j++)bit[j] = (i / power[j]) % 3;
			for (int j = 0; j < S.size() - 1; j++) {
				for (int k = 0; k < S.size(); k++)c[k] = bit[k];
				if (bit[j] != bit[j + 1]) {
					int V = bit[j] + bit[j + 1];
					V = 3 - V; c[j] = V; c[j + 1] = V;
					int T = 0;
					for (int k = 0; k < S.size(); k++) {
						T += c[k] * power[k];
					}
					x[i].push_back(T);
				}
			}
		}
		Q.push(D); dist[D] = 0;
		while (!Q.empty()) {
			int a = Q.front(); Q.pop();
			for (int i = 0; i < x[a].size(); i++) {
				if (dist[x[a][i]] == 99999) {
					dist[x[a][i]] = dist[a] + 1;
					Q.push(x[a][i]);
				}
			}
		}
		int minx = min(dist[0], min(dist[p2[S.size() - 1]], dist[2 * p2[S.size() - 1]]));
		if (minx == 99999)cout << "NA" << endl;
		else cout << minx << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

char get_another_color(char a, char b){
  if (a > b) swap(a, b);
  if (a == 'b') return "gr"[b == 'g'];
  if (a == 'g') return 'b';
  return '#';
}

int solve(string init)
{
  queue<string> Q;
  set<string> vis;
  Q.push(init);
  vis.insert(init);
  int turn = 0;
  while (!Q.empty()){
    queue<string> nQ;
    while (!Q.empty()){
      string state = Q.front();
      Q.pop();
      bool ok = true;
      rep(i, state.size() - 1){
        if (state[i] != state[i + 1]){
          ok = false;
          string nstate = state;
          nstate[i] = nstate[i + 1] = get_another_color(nstate[i], nstate[i + 1]);
          if (vis.find(nstate) != vis.end()) continue;
          vis.insert(nstate);
          nQ.push(nstate);
        }
      }
      if (ok) return turn;
    }
    turn++;
    swap(Q, nQ);
  }
  return -1;
}

int main()
{
  string state;
  while (cin >> state, state != "0"){
    int result = solve(state);
    if (result < 0) puts("NA");
    else printf("%d\n", result);
  }
  return 0;
}