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

using namespace std;

typedef pair<int, string> P;

string worm;

bool chkColor(int len){
  for(int i = 0; i < len - 1; ++i) if(worm[i] != worm[i + 1]) return false;
  return true;
}

int bfs(){

  int cnt, len = worm.length();
  queue<P> q; q.push(P(0, worm));
  map<string, bool> mpworm; mpworm[worm] = true;

  while(!q.empty()){

    P p = q.front(); q.pop();
    cnt = p.first;
    worm = p.second;

    if(chkColor(len)) return cnt;

    for(int i = 0; i < len - 1; ++i){

      char ncolor;
      string nworm = worm;

      if(worm[i] != worm[i + 1]){
        if(worm[i] == 'r' && worm[i + 1] == 'g' || worm[i] == 'g' && worm[i + 1] == 'r') ncolor = 'b';
  	    else if(worm[i] == 'g' && worm[i + 1] == 'b' || worm[i] == 'b' && worm[i + 1] == 'g') ncolor = 'r';
        else if(worm[i] == 'b' && worm[i + 1] == 'r' || worm[i] == 'r' && worm[i + 1] == 'b') ncolor = 'g';
        nworm[i] = ncolor;
        nworm[i + 1] = ncolor;
        if(mpworm[nworm] != true){
          mpworm[nworm] = true;
          q.push(P(cnt + 1, nworm));
        }
      }

    }

  }
  return -1;
}


int main(){

  int ans;

  while(1){

    cin >> worm;
    if(worm == "0")break;
    ans = bfs();
    if(ans == -1)cout << "NA" << endl;
    else cout << ans << endl;

  }

  return 0;
}

C++: //57
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<utility>

using namespace std;

int main(){
  for(char w[11];cin>>w,w[0]!='0';){
    queue<pair<vector<int>,int> >que;
    vector<int> in;
    for(int i=0;w[i];i++){
      int n=(w[i]=='r')?1:(w[i]=='g')?2:4;
      in.push_back(n);
    }
    que.push(make_pair(in,0));
    set<vector<int> > s;
    while(!que.empty()){
      vector<int> v=que.front().first;
      if(s.find(v)!=s.end()){
	que.pop();
      }else{
	s.insert(v);
	int c=0;
	for(int i=0;i<v.size();i++){
	  c|=v[i];
	}
	if((c&(c-1))==0)break;
	for(int i=0;i<v.size()-1;i++){
	  if(v[i]!=v[i+1]){
	    vector<int> n=v;
	    int nc=7^v[i]^v[i+1];
	    n[i]=n[i+1]=nc;
	    que.push(make_pair(n,que.front().second+1));
	  }
	}
	que.pop();
      }
    }
    if(que.empty()){
      cout<<"NA"<<endl;
    }else{
      cout<<que.front().second<<endl;
    }
  }
  return 0;
}

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

int main(){
  string worm;
  int cost[60000];
  int count = 0;
  while(1) {
    if(count > 60000) break;
    count++;
    cin >> worm;
    if(worm[0] == '0') break;

    int ima[10];
    for(int i=0;i<worm.size();i++) {
      if(worm[i] == 'r') ima[i] = 0;
      else if(worm[i] =='b') ima[i] = 1;
      else ima[i] = 2;
    }

    int t = 1,m = worm.size();
    int deci = 0;
    // 3 to 10
    for(int i=0;i<m;i++) {
      deci += ima[m-1-i]*t;
      t *= 3;
    }

   
    for(int i=0;i<60000;i++) cost[i] = 1000000000;  
    cost[deci] = 0;

    queue <int> Q;
    Q.push(deci);

    int befo[2];
    int a;
    int ans = 1000000000;
    while(!Q.empty()) {
      a = Q.front();
      Q.pop();
      int b = a;
      //      cout << a << endl;
      for(int i =0;i<m;i++) {
	ima[m-1-i] = b%3;
	b/=3;
      }
      
      //      cout << "start";
      //for(int i=0;i<m;i++) cout << ima[i];
      //cout << endl;
    
      for(int j=0;j<m-1;j++) {
	if(ima[j] != ima[j+1]) break;
	if(j == m-2) ans = min(ans,cost[a]);
      }
      

      int flg[3] = {};
      for(int i=0;i<m-1;i++) {
	if(ima[i] != ima[i+1]) {
	  befo[0] = ima[i];
	  befo[1] = ima[i+1];
	  flg[ima[i]] = flg[ima[i+1]] = 1;
	  for(int j=0;j<3;j++) if(flg[j] == 0) ima[i] = ima[i+1] = j;
	  // for(int j=0;j<m;j++) cout << ima[j];
	  // cout << endl;
	  t = 1;
	  deci = 0;
	  // 3 to 10
	  for(int j=0;j<m;j++) {
	    deci += ima[m-1-j]*t;
	    t *= 3;
	  }

	  if(cost[deci]  > cost[a]+1 && ans > cost[a] +1 ) {
	    cost[deci] = cost[a]+1;
	    // cout << "deci ="<< deci << endl;
	    Q.push(deci);
	  }
	  ima[i] = befo[0];
	  ima[i+1] = befo[1];
	  flg[0] = flg[1] = flg[2] = 0;
	}
      }
    }
	



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


  }	
	return 0;
}