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 <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
struct nc{
	int number;
	int count;
};
int main() {
	map<string, int> msi;
	FOR(i, 2, 11){
		queue<string> qs;
		REP(j, 3){
			string s2;
			if (j == 0)REP(k, i)s2 += 'r';
			if (j == 1)REP(k, i)s2 += 'g';
			if (j == 2)REP(k, i)s2 += 'b';
			msi.insert({ s2, 0 });
			qs.push(s2);
		}
		while (!qs.empty()){
			string s3 = qs.front(); qs.pop();
			REP(j, i - 1){
				if (s3[j] == 'r' && s3[j + 1] == 'r'){
					string s4 = s3;
					s4[j] = 'g'; s4[j+1] = 'b';
					if (msi.find(s4) == msi.end()){
						msi.insert({ s4, msi[s3] + 1 });
						qs.push(s4);
					}
					s4[j] = 'b'; s4[j + 1] = 'g';
					if (msi.find(s4) == msi.end()){
						msi.insert({ s4, msi[s3] + 1 });
						qs.push(s4);
					}
				}
				if (s3[j] == 'g' && s3[j + 1] == 'g'){
					string s4 = s3;
					s4[j] = 'b'; s4[j + 1] = 'r';
					if (msi.find(s4) == msi.end()){
						msi.insert({ s4, msi[s3] + 1 });
						qs.push(s4);
					}
					s4[j] = 'r'; s4[j + 1] = 'b';
					if (msi.find(s4) == msi.end()){
						msi.insert({ s4, msi[s3] + 1 });
						qs.push(s4);
					}
				}
				if (s3[j] == 'b' && s3[j + 1] == 'b'){
					string s4 = s3;
					s4[j] = 'r'; s4[j + 1] = 'g';
					if (msi.find(s4) == msi.end()){
						msi.insert({ s4, msi[s3] + 1 });
						qs.push(s4);
					}
					s4[j] = 'g'; s4[j + 1] = 'r';
					if (msi.find(s4) == msi.end()){
						msi.insert({ s4, msi[s3] + 1 });
						qs.push(s4);
					}
				}
			}
		}
	}

	string s;
	while (cin >> s, s != "0"){
		if (msi.find(s) != msi.end()){
			cout << msi[s] << endl;
		}
		else{
			cout << "NA" << endl;
		}
	}
}

C++: //57
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<utility>

using namespace std;

int main(){
  for(char w[11];cin>>w,w[0]!='0';){
    queue<pair<vector<int>,int> >que;
    vector<int> in;
    for(int i=0;w[i];i++){
      int n=(w[i]=='r')?1:(w[i]=='g')?2:4;
      in.push_back(n);
    }
    que.push(make_pair(in,0));
    set<vector<int> > s;
    while(!que.empty()){
      int c=0;
      vector<int> v=que.front().first;
      if(s.find(v)!=s.end()){
	que.pop();
      }else{
	for(int i=0;i<v.size();i++){
	  c|=v[i];
	}
	if((c&(c-1))==0)break;
	for(int i=0;i<v.size()-1;i++){
	  vector<int> n=v;
	  int nc=7^v[i]^v[i+1];
	  n[i]=n[i+1]=nc;
	  if(s.find(n)==s.end()){
	    que.push(make_pair(n,que.front().second+1));
	  }
	}
	que.pop();
      }
    }
    if(que.empty()){
      cout<<"NA"<<endl;
    }else{
      cout<<que.front().second<<endl;
    }
  }
  return 0;
}