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<stdio.h>
#define M 1000000000
int l;
void f(int n,int *a){//kakunou
  int i;
  for(i=0;i<l;n/=3)a[i++]=n%3;
}
int g(int *a){
  int n=0,i;
  for(i=l;i;i--)n=n*3+a[i-1];
  return n;
}
void prih(int *h,int r){
  int i,j=0;
  for(i=0;i<=r;i++){
    if(i==1<<j){
      printf("\n");
      j++;
    }
    printf("%d ",h[i]);
  }printf("\n");
}
int main(){
  char s[20];
  int i,j,k,mi,n,r,a[12],c[60000],h[1000000]={-M};
  while(scanf("%s",s),s[0]-'0'){
    for(i=0;i<60000;i++)c[i]=M;
    for(i=r=1;i<1000000;i++)h[i]=M;
    for(l=n=0;s[l];l++){
      if(s[l]=='r')n=n*3+0;
      if(s[l]=='g')n=n*3+1;
      if(s[l]=='b')n=n*3+2;
    }
    k=-1;
    c[mi=n]=0;
    for(i=n=0;i<l;i++)n=n*3+1;//printf("%d\n",n);
    //h[r=1]=c[n]*60000+n;
    while(1){
      if(mi==0||mi==n||mi==n*2)break;
      for(i=1;i<l;i++){//printf("%d\n",mi);
    f(mi,a);
    //for(j=0;j<l;j++)printf("%d",a[j]);printf("\n");
    a[i-1]=a[i]=(6-a[i-1]-a[i])%3;
    //for(j=0;j<l;j++)printf("%d",a[j]);printf("\n");
    j=g(a);//printf("%d\n",j);
    //if(j==0||j==n||j==n*2)break;
    if(c[j]>c[mi]+1){
      c[j]=c[mi]+1;//printf("%d\n",c[j]);
      k=c[j]*60000+j;//printf("k%d\n",k);
      for(j=r++;h[j/2]>k;j/=2)h[j]=h[j/2];
      h[j]=k;//printf("%d\n",j);
    }
      }//printf("\n");prih(h,r);
      //if(l-i)break;
      mi=h[i=1]%60000;//printf("%d\n",mi);
      k=h[--r];
      for(h[r]=M;k>h[i];i=j){
    h[i]=h[j=i*2];
    if(h[i]>h[j+1])h[i]=h[j=j+1];
      }//printf("mi%d\n",mi);
      if(r==1)break;
      h[i/2]=k;
    }
    if(r-1||k==-1)printf("%d\n",c[mi]);
    else          printf("NA\n");
  }
  return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<string.h>

using namespace std;
char c[11];
int d[10];
int bfs[59049];
int temp[10];
int main(){
	while(1){
		scanf("%s",c);
		if(c[0]=='0')return 0;
		queue<int> Q;
		for(int i=0;i<59049;i++)bfs[i]=-1;
		int first=0;
		int s=strlen(c);
		for(int i=0;i<s;i++){
			first*=3;
			if(c[i]=='r')first+=2;
			if(c[i]=='g')first++;
		}
		bfs[first]=0;
		Q.push(first);
		while(Q.size()){
			int now=Q.front();
			int val=now;
			for(int i=0;i<s;i++){
				d[s-i-1]=val%3;
				val/=3;
				temp[s-i-1]=d[s-i-1];
			}
			for(int i=0;i<s-1;i++){
				if(d[i]!=d[i+1]){
					if(d[i]!=0&&d[i+1]!=0){
						temp[i]=temp[i+1]=0;
						int next=0;
						for(int j=0;j<s;j++){
							next*=3;
							next+=temp[j];
						}
						if(bfs[next]==-1){
							bfs[next]=bfs[now]+1;
							Q.push(next);
						}
					}
					if(d[i]!=1&&d[i+1]!=1){
						temp[i]=temp[i+1]=1;
						int next=0;
						for(int j=0;j<s;j++){
							next*=3;
							next+=temp[j];
						}
						if(bfs[next]==-1){
							bfs[next]=bfs[now]+1;
							Q.push(next);
						}
					}
					if(d[i]!=2&&d[i+1]!=2){
						temp[i]=temp[i+1]=2;
						int next=0;
						for(int j=0;j<s;j++){
							next*=3;
							next+=temp[j];
						}
						if(bfs[next]==-1){
							bfs[next]=bfs[now]+1;
							Q.push(next);
						}
					}
					temp[i]=d[i];
					temp[i+1]=d[i+1];
				}
			}
			Q.pop();
		}
		int q=0;
		for(int i=0;i<s;i++){
			q*=3;
			q++;
		}
		int r=0;
		for(int i=0;i<s;i++){
			r*=3;
			r+=2;
		}
		int ans=99999999;
		if(bfs[0]!=-1&&ans>bfs[0])ans=bfs[0];
		if(bfs[q]!=-1&&ans>bfs[q])ans=bfs[q];
		if(bfs[r]!=-1&&ans>bfs[r])ans=bfs[r];
		if(ans==99999999)printf("NA\n");
		else printf("%d\n",ans);
	}
}

C++: #include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DEBUG 0
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) if(DEBUG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DEBUG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
using pii = pair<int, int>; using vi = vector<int>;
static const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
static const int MOD = (int)(1e9 + 7);

bool f(string s) {
	rep(i, 0, s.size() - 1)
		if (s[i] != s[i + 1])return false;
	return true;
}

signed main() {
	for (string s; cin >> s&&s != "0";) {
		unordered_map<string, int> m;
		m[s] = 0;
		queue<string> q;
		q.emplace(s);
		while (!q.empty()) {
			string t = q.front(); q.pop();
			rep(i, 0, t.size() - 1) {
				if (t[i] == t[i + 1])continue;
				string tmp = t;
				if (t[i] + t[i + 1] == 'r' + 'g')tmp[i] = tmp[i + 1] = 'b';
				if (t[i] + t[i + 1] == 'g' + 'b')tmp[i] = tmp[i + 1] = 'r';
				if (t[i] + t[i + 1] == 'b' + 'r')tmp[i] = tmp[i + 1] = 'g';

				if (m.find(tmp) == m.end()) {
					q.emplace(tmp);
					m[tmp] = m[t] + 1;
				}
			}
		}

		int ans = INF;
		for (auto itr = m.begin(); itr != m.end(); itr++) {
			if (f(itr->first))ans=min(ans,itr->second);
		}
		if (ans == INF)cout << "NA" << endl;
		else cout << ans << endl;
	}
	return 0;
}