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 <queue>
#include <map>
#include <cstring>
using namespace std;

typedef pair<string, int> P;

int bfs(string worm){
  queue<P> que;
  que.push(P(worm, 0));

  map<string, int>  d;

  while(!que.empty()){
    P p = que.front();
    que.pop();

    if(d[p.first])  continue;

    string str = p.first;

    bool flag = true;
    for(int i = 0; i < str.size()-1; i++){
      if(str[i] != str[i+1]){
        flag = false;
        break;
      }
    }

    if(flag) return p.second;

    d[p.first] = p.second;

    for(int i = 0; i < str.size()-1; i++){
      if(str[i] != str[i+1]){
        string next;
        char tmp;
        if(str[i] != 'b' && str[i+1] != 'b'){
          tmp = 'b';
        }else if(str[i] != 'g' && str[i+1] != 'g'){
          tmp = 'g';
        }else{
          tmp = 'r';
        }

        next = str;
        next[i] = next[i+1] = tmp;
        que.push(P(next, p.second+1));
      }
    }
  }
  return -1;
}

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

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool same(char* seq){
  for(int i = 0; seq[i+1] != '\0'; ++i){
    if(seq[i] != seq[i+1]){
      return false;
    }
  }
  return true;
}
int dls(char* seq, int depth){
  if(depth<0){
    return false;
  }
  if(same(seq)){
    return true;
  }
  for(int i = 0; seq[i+1] != '\0'; ++i){
    char t = seq[i];
    char n = seq[i+1];
    if(t != n){
      if((t + n) == ('r' + 'b')){
        seq[i] = seq[i+1] = 'g';
      }
      else if((t + n) == ('r' + 'g')){
        seq[i] = seq[i+1] = 'b';
      }
      else{
        seq[i] = seq[i+1] = 'r';
      }
      if(dls(seq, depth-1)){
        return true;
      }
      seq[i] = t;
      seq[i+1] = n;
    }
  }
  return false;
}
void iddfs(char* seq){
  for(int depth = 0;depth <= 10; ++depth){
    if(dls(seq, depth)){
      cout << depth << endl;
      return;
    }
  }
  cout << "NA" << endl;
}
int main(){
  char seq[11];
  while(true){
    cin >> seq;
    if(seq[0] == '0'){
      break;
    }
    iddfs(seq);
  }
  return 0;
}

C++: #include<map>
#include<iostream>
#include<queue>
#include<string>

using namespace std;

class sint{
	public:
	string st;int m;
	sint(){}
	sint(string a,int b){st=a;m=b;}
};

int musi(string str){
	queue<sint> state;
	map<string,int> mm;
	
	mm[str]=1;
	
	state.push(sint(str,0));
	
	while(1){
		
		if(state.empty()==1){
			break;
		}
		
		sint u;
		u=state.front();
		state.pop();
		
		
		
		//cout<<u.st<<" "<<u.m<<endl;
		
		int cont[3];
		cont[0]=cont[1]=cont[2]=0;
		for(int i=0;i<u.st.size();i++){
			if(u.st[i]=='r'){
				cont[0]++;
			}
			if(u.st[i]=='g'){
				cont[1]++;
			}
			if(u.st[i]=='b'){
				cont[2]++;
			}
		}
		int aaa=0;
		for(int i=0;i<3;i++){
			if(cont[i]==0){
				aaa++;
			}
		}
		if(aaa==2){
			return u.m;
		}
		
		for(int i=1;i<=u.st.size();i++){
			string nst=u.st;
			if(nst[i]!=nst[i-1]){
				if(nst[i]=='r'&&nst[i-1]=='g'){
					nst[i]=nst[i-1]='b';
				}
				if(nst[i]=='g'&&nst[i-1]=='b'){
					nst[i]=nst[i-1]='r';
				}
				if(nst[i]=='b'&&nst[i-1]=='r'){
					nst[i]=nst[i-1]='g';
				}
				if(nst[i-1]=='r'&&nst[i]=='g'){
					nst[i]=nst[i-1]='b';
				}
				if(nst[i-1]=='g'&&nst[i]=='b'){
					nst[i]=nst[i-1]='r';
				}
				if(nst[i-1]=='b'&&nst[i]=='r'){
					nst[i]=nst[i-1]='g';
				}
			}
			if(mm[nst]!=1){
				mm[nst]=1;
				state.push(sint(nst,u.m+1));
			}
			
		}
	}
	return -1;
}

int main(){
	
	while(1){
		string a;
		cin>>a;
		if(a=="0"){
			break;
		}
		int b=musi(a);
		if(b==-1){
			puts("NA");
		}else{
			printf("%d\n",b);
		}
	}
}