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 <bits/stdc++.h>
using namespace std;

struct State{
  string s;
  int c;
  State(string s, int c) : s(s), c(c){}
};

bool check(string s){
  for(int i = 0 ; i < (int)s.size()-1 ; i++){
    if(s[i] != s[i+1]) return false;
  }
  return true;
}

string change(string s){
  sort(s.begin(), s.end());
  if(s[0] == 'b' && s[1] == 'g') return "rr";
  if(s[0] == 'b' && s[1] == 'r') return "gg";
  if(s[0] == 'g' && s[1] == 'r') return "bb";
}

int bfs(string s){
  queue<State> que;
  que.push(State(s, 0));
  
  set<string> S;
  
  while(!que.empty()){
    State q = que.front(); que.pop();
    if( check(q.s) ) return q.c;
    
    if(S.find(q.s) != S.end()) continue;
    S.insert(q.s);
    
    for(int i = 0 ; i < (int)q.s.size()-1 ; i++){
      string t = q.s.substr(i, 2);
      if(t != "rr" && t != "gg" && t != "bb"){
	que.push(State(q.s.substr(0, i) + change(t) + q.s.substr(i+2), q.c+1));
      }
    }   
  }
  return -1;
}

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

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vvi;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007
 
string str;
bool f(string s){
	for(int i = 1; i <s.size(); i++){
		if(s[i] != s[i-1]) return false;
	}
	return true;
}

string calc(int i,string s){
	string ret = s;
	if(s[i] != s[i+1]){
		if((ret[i]=='g' && ret[i+1] == 'r') ||(ret[i]=='r' && ret[i+1] == 'g')) ret[i] = ret[i+1] ='b';
		else if((ret[i]=='b' && ret[i+1] == 'r') ||(ret[i]=='r' && ret[i+1] == 'b')) ret[i] = ret[i+1] ='g';
		else if((ret[i]=='g' && ret[i+1] == 'b') ||(ret[i]=='b' && ret[i+1] == 'g')) ret[i] = ret[i+1] ='r';
	}
	return ret;
}

int main(){
	while(cin>>str){
		if(str == "0") break;
		set<string> s;
		queue<pair<string,int> > q;
		int ans = 0;
		pair<string,int> p;
		s.insert(str);
		q.push(pair<string,int>(str,0));
		bool flag = false;
		while(!q.empty()){
			p = q.front();
			q.pop();
			if(f(p.fi)) {
				ans = p.se;
				flag = true;
				break;
			}
			for(int i = 0; i< str.size()-1; i++){
				if(!s.count(calc(i,p.fi))){
					q.push(pair<string,int>(calc(i,p.fi),p.se+1));
					s.insert(calc(i,p.fi));
				}
			}
		}
		if(flag) cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
}

C++: #include <iostream>
#include <map>
#include <string>
#include <queue>
using namespace std;

typedef pair<string,int> bug;

bool check(string str ) {
  for ( int i = 1; i < str.size(); i++ ) {
    if ( str[0] != str[i] ) return 0;
  }
  return 1;
}

char change( char a, char b ) {
  if ( a > b ) swap(a,b);
  if ( a == 'b' && b == 'g' ) return 'r';
  else if ( a == 'b' && b == 'r' ) return 'g';
  else return 'b';
}

void bfs(string str ) {
  int ans = -1;
  queue<bug> Q;
  map<string,int> close;

  Q.push( make_pair(str, 0) );
  close.insert( make_pair(str, 1) );

  while( !Q.empty() ) {
    bug b = Q.front(); Q.pop();
    string tmp1 = b.first;
    if ( check(tmp1) ) {
      ans = b.second;
      break;
    }

    for ( int i = 0; i < tmp1.size()-1; i++ ) {
      if ( tmp1[i] != tmp1[i+1] ) {
	string tmp2 = tmp1;
	char nc = change(tmp2[i],tmp2[i+1]);
	tmp2[i] = nc;
	tmp2[i+1] = nc;
	if ( close[tmp2] ) continue;
	Q.push( make_pair(tmp2,b.second+1) );
	close[tmp2] = 1;
	
      }
    }

  }

  if ( ans == -1 ) cout << "NA" << endl;
  else cout << ans << endl;

}


int main() {
  string str;
  while ( cin >> str , str != "0") {
    bfs(str);
  }

  return 0;
}