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

struct Worm{
  int body[10];
  bool operator < ( const Worm &p ) const{
    for(int i=0;i<10;i++){
      if(body[i]==p.body[i])continue;
      return body[i] < p.body[i];
    }
  }
};

int size,nowcnt;
Worm now,next,X;
string str;
map < Worm , bool > vd;
queue <Worm> Q;
queue < int > cnt;
void input();
void init();
void solve();

int main(){
  while(cin>>str&&str!="0"){
    input();
    init();
    solve();
  }
  return 0;
}

void init(){
  vd.clear();
  while(!Q.empty()){
    Q.pop();
    cnt.pop();
  }
}

void input(){
  size=str.size();
  for(int i=0;i<size;i++){
    if(str[i]=='r')X.body[i]=1;
    if(str[i]=='g')X.body[i]=2;
    if(str[i]=='b')X.body[i]=3;
  }
  for(int i=size;i<10;i++){
    X.body[i]=0;
  }
}

void solve(){
  int count;
  Q.push(X);
  cnt.push(0);
  vd[X]=true;

  while(!Q.empty()){
    now=Q.front();Q.pop();
    nowcnt=cnt.front();cnt.pop();
    
    count=0;
    for(int i=1;i<size;i++)
      if(now.body[i]==now.body[i-1])count++;
    if(count==size-1){
      cout<<nowcnt<<endl;
      return;
    }
    
    for(int i=0;i<size-1;i++){
      if(now.body[i]==now.body[i+1])continue;
      next=now;
      next.body[i]=next.body[i+1]=(6-now.body[i]-now.body[i+1]);
      if(vd[next]==true)continue;
      vd[next]=true;
      Q.push(next);
      cnt.push(nowcnt+1);

    }

  }
  cout<<"NA"<<endl;
}

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

string s;
char c[] = {'r','b','g'};

char rem(char a,char b){
  for(int i=0;i<3;i++){
    if(c[i] != a && c[i] != b)return c[i];
  }
  return 'x';
}

int main(){
  while(cin >> s,s!="0"){
    map<string,int> m;
    m[s] = 0;
    queue<string> q;
    q.push(s);
    
    int res = -1;
    while(q.size()){
      s = q.front(); q.pop();
      bool f = true;
      for(int i=0;i<s.size()-1;i++){
	if(s[i]!=s[i+1]){
	  f = false;
	  string tmp = s;
	  tmp[i] = tmp[i+1] = rem(s[i],s[i+1]);
	  if(m.find(tmp)==m.end()){
	    m[tmp] = m[s] + 1;
	    q.push(tmp);
	  }
	}
      }
      if(f){
	res = m[s];
	break;
      }
    }
    if(res<0)cout << "NA\n";
    else cout << res << endl;
  }
}

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

using namespace std;

main(){
  string in;                    //¶ñÌúóÔ(ªÆÈé)
  while(cin>>in){
    if(in[0]=='0')break;        //I¹ð

    //gpÏÌú»
    int ans=-1;                 //answer(ðÉHèÂ¯È¢ê-1ÌÜÜ)
    set<string> vis;            //Tõµ½m[h
    queue<pair<string,int> > q; //ÉTõ·é¶ñðüêéB

    q.push(make_pair(in,0));    //ªðL[ÉüêéBÅZèÍà¿ëñ0
    vis.insert(in);             //¶ñssÍTõµ½B

    
    //TõÌæªcÁÄ¨èAðÉ½Çè¢ÄÈ¢ÀèTõ
    while(!q.empty()){
      //Tõm[hÌ¶ñÆA[³ðæèo·B
      string str=q.front().first;
      int depth=q.front().second;
      q.pop();
      
      //¶ñªê¶ÌÝ©ç\¬³êÄ¢é©ð»èB
      //ê¶ÌÝ©çÈÁÄ¢½êA¦ª©Â©Á½±ÆÉÈéÌÅTõðÅ¿ØéB
      int r=0,g=0,b=0;
      for(int i=0;i<str.size();i++){
        switch(str[i]){
        case 'r':++r;break;
        case 'g':++g;break;
        case 'b':++b;break;
        }
      }
      if(r==str.size() || g==str.size() || b==str.size()){
        ans=depth;
        break;
      }

      //ÙÈéñgÌ¶ðTµoµA»êðL[ÉlßéB
      for(int i=0;i<str.size()-1;i++){
        if(str[i]!=str[i+1]){
          string next(str);      //ðÁ¦é¶ñ(ÈºÌ3ÂÌif¶ÅKp)
          
          if( (str[i]=='b' && str[i+1]=='g') ||
              (str[i]=='g' && str[i+1]=='b')){
            next[i]='r';
            next[i+1]='r';
          }

          if( (str[i]=='r' && str[i+1]=='g') ||
              (str[i]=='g' && str[i+1]=='r')){
            next[i]='b';
            next[i+1]='b';
          }
          
          if( (str[i]=='b' && str[i+1]=='r') ||
              (str[i]=='r' && str[i+1]=='b')){
            next[i]='g';
            next[i+1]='g';
          }

          //·ÅÉTõµÄ¢½¶ñ¾Á½êXLbv
          if(vis.count(next)==1)continue;

          //Tõ\èÌm[hÆµÄL[ÉüêéB
          q.push(make_pair(next,depth+1));
          vis.insert(next);
        }
      }
    }
    
    if(ans==-1)cout<<"NA"<<endl;    //SÄðTõµÄàðª©Â©çÈ©Á½ê
    else cout<<ans<<endl;           //ðª©Â©Á½êÅZðoÍ
  }
}