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

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

char rgb[3] = {'r', 'g', 'b'};

int main(){
    string x;
    while(cin >> x){
        if(x == "0")    break;

        queue<pair<string,int>> q;
        set<string> s;

        q.push({x,0});

        int ans = -1;
        while(!q.empty()){
            pair<string,int> t = q.front(); q.pop();
            if(check(t.first)){
                ans = t.second;
                break;
            }
            
            if(s.find(t.first) != s.end())  continue;

            s.insert(t.first);
            for(int i = 0; i < t.first.length()-1; i++){
                if(t.first[i] == t.first[i+1])  continue;

                for(int j = 0; j < 3; j++){
                    if(t.first[i] != rgb[j] && t.first[i+1] != rgb[j]){
                        string copy = t.first;
                        copy.replace(i, 2, 2, rgb[j]);
                        if(s.find(copy) == s.end()) q.push({copy, t.second+1});
                    }
                }
            }
        }

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

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

typedef pair<string,int> bug;

bool check(string insect) {
  for ( int i = 1; i < insect.size(); i++ ) {
    if ( insect[0] != insect[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 ) {
  map<string,int> close;
  queue<bug> open;
  open.push(make_pair(str,0));
  close.insert(make_pair(str,1));
  int cnt = -1;
  while ( !open.empty() ) {
    bug b = open.front(); open.pop();
    string insect = b.first;
    if ( check(insect) ) {
      cnt = b.second;
      break;
    }
    
   
    for ( int i = 0; i < insect.size()-1; i++ ) {
      string tmp = insect;   
      if ( tmp[i] != tmp[i+1] ) {
	char nc = change( tmp[i],tmp[i+1] );
	tmp[i] = nc;
	tmp[i+1] = nc;
	if ( close[tmp] ) continue;
	open.push(make_pair(tmp,b.second+1));
	close[tmp] = 1;
      }
    }

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

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

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

int bfs(string start);
bool isSameColor(string state);

int main(){
  string str;
  while(1){
    getline(cin,str);
    if(str=="0") break;

    int ans;
    ans=bfs(str);

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

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

int bfs(string start){
  queue<string> Q;
  map<string,int> data;
  map<string,bool> visited;
  string u, v;

  Q.push(start);
  data[start] = 0;
  visited[start] = true;

  while(!Q.empty()){
    u=Q.front(), Q.pop();
    if(isSameColor(u)) return data[u];
    for(int i=1;i<u.size();i++){
      if(u[i]!=u[i-1]){
	char firo;
        if(u[i]=='r'){
	  if(u[i-1]=='b') firo='g';
	  else if(u[i-1]=='g') firo='b';
	}
	else if(u[i]=='b'){
	  if(u[i-1]=='g') firo='r';
	  else if(u[i-1]=='r') firo='g';
	}
	else if(u[i]=='g'){
	  if(u[i-1]=='r') firo='b';
	  else if(u[i-1]=='b') firo='r';
	}

        v=u;
        v[i]=v[i-1]=firo;
        if(!visited[v]){
	  visited[v]=true;
	  data[v]=data[u]+1;
	  Q.push(v);
	}
      }
    }
  }
  return -1;
}