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 <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

map<char, int> m;
map<string, int> ans;

int main() {

	m['r'] = 1;
	m['g'] = 2;
	m['b'] = 4;
	
	string str;
	
	while (cin >> str, str != "0") {
		char c[3] = {'r', 'g', 'b'};
		string ser[3] = {};
		REP(i, 3) REP(j, str.size()) {
			ser[i] += c[i];
		}
		
		queue<pair<int, string> > que;
		REP(i, 3) que.push(make_pair(0, ser[i]));
		
		while (!que.empty()) {
			int t = que.front().first;
			string now = que.front().second;
			que.pop();
			
			if (ans.find(now) != ans.end()) continue;
			ans[now] = t;
			if (now == str) break;
			
			REP(i, now.size() - 1) {
				if (now[i] == now[i + 1]) {
					string next = now;
					int nextcor = 7 - m[now[i]];
					vector<char> pl;
					REP(j, 3) if (nextcor & (1<<j)) pl.push_back(c[j]);
					REP(j, 2) {
						next[i] = pl[j];
						next[i + 1] = pl[(j + 1) % 2];
						que.push(make_pair(t + 1, next)); 
					}
				}
			}
		}
		if (ans.find(str) == ans.end()) puts("NA");
		else cout << ans[str] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <set>
 
using namespace std;
 
int len;
string str;
 
bool check(string s){
  char ch = s[0];
 
  for(int i = 1 ; i < len ; i++){
    if(ch != s[i]) return false;
  }
  return true;
}
 
int bfs(){
  string ans;
  queue<string> Q;
  Q.push(str);
 
  bool found = false;
  set<string> used;
  used.insert(str);
  map<string,int> dist;
  dist[str] = 0;
 
  while(!Q.empty()){
    string s = Q.front(); Q.pop();
     
    if(check(s)){
      found = true;
      ans = s;
      break;
    }
 
    for(int i = 1 ; i < len ; i++){
      string next = s;
      if(s[i-1] == 'b' && s[i] == 'r' ||
     s[i-1] == 'r' && s[i] == 'b'){
    next[i-1] = next[i] = 'g';
      }else if(s[i-1] == 'b' && s[i] == 'g' ||
           s[i-1] == 'g' && s[i] == 'b'){
    next[i-1] = next[i] = 'r';
      }else if(s[i-1] == 'r' && s[i] == 'g' ||
           s[i-1] == 'g' && s[i] == 'r'){
    next[i-1] = next[i] = 'b';
      }
       
      if(!used.count(next)){
    dist[next] = dist[s] + 1;
    used.insert(next);
    Q.push(next);
      }
    }
  }
 
  if(!found) return -1;
  return dist[ans];
}
 
int main(){
  while(cin >> str,str != "0"){
    len = (int)str.size();
    int res = bfs();
    if(res == -1){
      cout << "NA" << endl;
    }else{
      cout << res << endl;
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

int main(){
	
	while(true){
		string str;
		cin >> str;
		if(str[0] == '0'){
			break;
		}
		map<string, int> m;
		
		queue<string> que;
		que.push(str);
		
		for(int loop = 0; loop < 10000000; loop++){
			int N = que.size();
			if(N == 0){
				break;
			}
			for(int poyo = 0; poyo < N; poyo++){
				string s = que.front();
				//cout << s << endl;
				que.pop();
				if(m.find(s) != m.end()){
					continue;
				}
				m[s] = loop;
				for(int i = 0; i < s.size() - 1; i++){
					string ss = s;
					if(s[i] != s[i + 1]){
						if(s[i] != 'r' && s[i + 1] != 'r'){
							ss[i] = 'r';
							ss[i + 1] = 'r';
						}else if(s[i] != 'b' && s[i + 1] != 'b'){
							ss[i] = 'b';
							ss[i + 1] = 'b';
						}else{
							ss[i] = 'g';
							ss[i + 1] = 'g';
						}
					}
					que.push(ss);
				}
			}
		}
		int ans = 10000000;
		for(int i = 0; i < str.size(); i++){
			str[i] = 'r';
		}
		if(m.find(str) != m.end()){
			ans = min(ans, m[str]);
		}
		for(int i = 0; i < str.size(); i++){
			str[i] = 'b';
		}
		if(m.find(str) != m.end()){
			ans = min(ans, m[str]);
		}
		for(int i = 0; i < str.size(); i++){
			str[i] = 'g';
		}
		if(m.find(str) != m.end()){
			ans = min(ans, m[str]);
		}
		if(ans == 10000000){
			cout << "NA" << endl;
		}else{
			cout << ans << endl;
		}
	}
	
	return 0;
}