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 <algorithm>

using namespace std;

typedef pair <string, int> P;

#define mp(a, b) make_pair(a, b)

char diff(char a, char b)
{
	if (a != 'r' && b != 'r') return 'r';
	if (a != 'g' && b != 'g') return 'g';
	if (a != 'b' && b != 'b') return 'b';
	return 'a';
}

bool check(string str)
{
	char c = str[0];
	for (int i = 0; i < str.length(); i++){
		if (c != str[i]) return false;
	}

	return true;
}

int main(void)
{
	string str;

	while (cin >> str){
		if (str == "0") return 0;

		int ret = -1, a;
		queue < P > q;
		q.push(mp(str, 0));
		while (!q.empty()){
			P now = q.front();
			q.pop();
			if (check(now.first)){
				ret = now.second;
				break;
			}
			if (q.size() > 500000){
				break;
			}
			for (int i = 0; i < now.first.length()-1; i++){
				if (now.first[i] != now.first[i + 1]){
					string tmp = now.first;
					tmp[i] = diff(now.first[i], now.first[i + 1]);
					tmp[i + 1] = diff(now.first[i], now.first[i + 1]);
					q.push(mp(tmp, now.second + 1));
				}
			}
		}

		if (ret >= 0) cout << ret << endl;
		else cout << "NA" << endl;
	}

	return 0;
}

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

string insect = "brg";
string str,str2;
int cnt;
queue <string> Q;


bool bfs( string str) {
  int l = Q.size();
  while ( !Q.empty() ) {
    
    str = Q.front();
    Q.pop();
    l--;
    //cout << "l" << " " << l << endl;
    int len = str.size();
     
    for ( int i = 0; i < len-1; i++ ) {
      str2 = str;
      for ( int j = 0; j < 3; j++ ) {
	if ( str2[i] != str2[i+1] 
	     && str2[i] != insect[j] && str2[i+1] != insect[j] ) {
	  str2[i] = insect[j];
	  str2[i+1] = insect[j];

	  Q.push( str2 );
	  int flag = 0;
	  for ( int k = 0; k < len-1; k++ ) {
	    if ( str2[k] != str2[k+1] ) flag = 1;
	  }
	  // cout << "i" << " " << i << " " << "j" << " " << j << endl;
	  // cout << "cnt" << " " << cnt << endl;
	  // cout << "str2" << " " << str2 << endl;
	  // cout << endl;
	  
	  if ( flag == 0 ) return 1;
	}
      }
    }
    if ( l == 0 ) return 0;
  }
  return 0;
}

int main() {

  while (1) {

    cin >> str;
    int length = str.size();
    int f = 0;
    for ( int i = 0; i < length-1; i++ ) {
      if ( str[i] != str[i+1] ) f = 1;
    }

    if ( str == "0" ) break;
    while( !Q.empty() ) Q.pop();
    cnt = 0;
    Q.push( str );
    while (1) {
    if ( f == 0 ){
      cout << 0 << endl;
      break;
    }
      cnt++;
      if ( cnt == 10 ){
	cout << "NA" << endl;
	break;
      }
      if ( bfs( str ) ) {
	cout <<  cnt << endl;
	break;
      }
    }

  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<queue>
#include<map>
using namespace std;struct D{string s;int n;};main(){map<int,int>C;string s;int i,n,F;for(C[217]=98,C[201]='r',C[212]='g';cin>>s,s!="0";F?puts("NA"):printf("%d\n",n)){map<string,int>M;queue<D>q;D d={s,0};q.push(d);for(M[s]=1;q.size();){d=q.front();q.pop();s=d.s;n=d.n;for(i=F=0;++i<s.size();)if(s[i-1]-s[i]){D d={s,n+1};d.s[i-1]=d.s[i]=C[s[i-1]+s[i]];if(M.find(d.s)==M.end())q.push(d);F=M[d.s]=1;}if(!F)break;}}}

C++: // AOJ 0179
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<functional>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

typedef long long int integer;

bool end(string s) {
  char c = s[0];
  int len = s.size();
  REP(i, len) {
    if (c != s[i]) {
      return false;
    }
  }
  return true;
}

vector<string> gonext(string worm) {
  vector<string> v;
  for (int i = 1, len = worm.length(); i < len; i++) {
    if (worm[i] == worm[i-1]) continue;
    string s = worm;
    char c = 'r' + 'g' + 'b' - worm[i] - worm[i-1];
    s[i] = s[i-1] = c;
    v.push_back(s);
  }
  return v;
}

typedef pair<integer,string> P;

integer solve(string worm) {
  priority_queue<P,vector<P>,greater<P> > q;
  set<string> s;
  q.push(make_pair(0,worm));
  while (!q.empty()) {
    P w = q.top();
    q.pop();
    if (end(w.second)) return w.first;
    vector<string> nxts = gonext(w.second);
    for (auto nxt : nxts) {
      if (s.find(nxt) == s.end()) {
        q.push(make_pair(w.first + 1, nxt));
        s.insert(nxt);
      }
    }
  }
  return -1;
}

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