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 <string>
#include <queue>
#include <set>

using namespace std;

typedef pair<string, int> P;

bool hantei (string a) {
	for (int i = 1; i < a.size(); i++) {
		if (a[0] == a[i]) {
		} else {
			return false;
		}
	}
	return true;
}

int main() {
	string a;
	string abc = "rgb";
	while (getline(cin, a)) {
		if (a[0] == '0') {
			break;
		}
		queue<P> q;
		q.push(P(a, 0));
		bool han = true;
		int t;
		set<string> s;
		if (hantei(a)) {
			han = false;
			cout << 0 << endl;
		}
		while (!q.empty()) {
			P p = q.front();
			q.pop();
			string pp = p.first;
			for (int i = 0; i < pp.size()-1; i++) {
				if (!han) {
					break;
				}
				if (pp[i] == pp[i+1]) {
				} else {
					string qq = pp;
					int v, w;
					for (int j = 0; j < abc.size(); j++) {
						if (abc[j] == pp[i]) {
							v = j;
						}
						if (abc[j] == pp[i+1]) {
							w = j;
						}
					}
					qq[i] = qq[i+1] = abc[3-(v+w)];
					if (hantei(qq)) {
						han = false;
						cout << p.second+1 << endl;
						break;
					}
					if (s.find(qq) == s.end()) {
						s.insert(qq);
						q.push(P(qq, p.second+1));
					}
				}
			}
		}
		if (han) {
			cout << "NA" << endl;
		}
	}
	return 0;
}

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

int main(){
  while(true){
    string s;
    cin>>s;
    if(s=="0")break;
    string sr(s.size(),'r');
    string sg(s.size(),'g');
    string sb(s.size(),'b');
    map<string,int> mp;
    queue<string> q;
    q.push(s);
    mp[s]=0;
    int f=0;
    if(s==sr||s==sb||s==sg){
      cout<<0<<endl;
    }else{
      //for(int i=0;i<1e6;i++){
      while(!q.empty()){
        string x=q.front();
        q.pop();
        for(int j=0;j<s.size()-1;j++){
          string y=x;
          if(y[j]=='r'&&y[j+1]=='g'){
            y[j]='b';
            y[j+1]='b';
          }
          if(y[j]=='b'&&y[j+1]=='g'){
            y[j]='r';
            y[j+1]='r';
          }
          if(y[j]=='r'&&y[j+1]=='b'){
            y[j]='g';
            y[j+1]='g';
          }
          if(y[j]=='g'&&y[j+1]=='b'){
            y[j]='r';
            y[j+1]='r';
          }
          if(y[j]=='g'&&y[j+1]=='r'){
            y[j]='b';
            y[j+1]='b';
          }
          if(y[j]=='b'&&y[j+1]=='r'){
            y[j]='g';
            y[j+1]='g';
          }
          //cout<<mp[x]+1<<" "<<y<<endl;
          if(y==sr||y==sb||y==sg){
            cout<<mp[x]+1<<endl;
            f=1;
            break;
          }
          if(mp[y]==0){
            mp[y]=mp[x]+1;
            q.push(y);
          }
        }
        if(f)break;
      }
      if(!f)cout<<"NA"<<endl;
    }
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

char hage[] = {'r','g','b'};
char hoge(char a, char b) {
  REP(i,3)
    if (a != hage[i] && b != hage[i])
      return hage[i];
}
typedef pair<int, string> pis;

int bfs(string s) {
  int res = INF;
  queue<pis> Q;
  Q.push(pis(0,s));
  while(!Q.empty()) {
    pis p = Q.front(); Q.pop();
    if (p.first > s.length()) continue;
    string ss = p.second;
//    cout << p.first << " " <<ss << endl;
    bool f = true;
    REP(i, ss.length()-1)
      if (ss[i] != ss[i+1])
        f = false;
    if (f) return p.first;
    REP(i, ss.length()-1) {
      if (ss[i] != ss[i+1]) {
        string sss = ss;
        sss[i] = sss[i+1] = hoge(ss[i], ss[i+1]);
        Q.push(pis(p.first + 1, sss));
      }
    }
  }
  return -1;
}

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

C++: #include<iostream>
#include<queue>
#include<map>
using namespace std;
int main(){
	string s,t;for(int i,n,f;cin>>s,s!="0";){
		queue<string>q;
		map<string,int>d;d[s]=1;
		for(q.push(s);q.size();){
			s=q.front();q.pop();n=s.length();
			for(f=i=0;i<n-1;i++)if(s[i]!=s[i+1]){
				t=s;t[i]=t[i+1]=(char)('r'+'g'+'b'-s[i]-s[i+1]);f++;
				if(!d[t])d[t]=d[s]+1,q.push(t);
			}
			if(!f){cout<<d[s]-1<<endl;goto N;}
		}
		cout<<"NA"<<endl;N:;
	}
}