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>

struct Bug{
	int red, blue, green, days;
	std::string body;
};

int main(){
	std::string s;
	while(std::cin >> s, s != "0"){
		int bug_length = s.length();
		std::queue<Bug> bug_s;
		std::map<std::string, int> bug_m;
		Bug first_bug = {0, 0, 0, 0, s};
		for(int i=0;i<s.length();i++){
			if(s[i] == 'r'){
				first_bug.red++;
			}
			if(s[i] == 'g'){
				first_bug.green++;
			}
			if(s[i] == 'b'){
				first_bug.blue++;
			}
		}
		bug_s.push(first_bug);
		int res = -1;
		while(!bug_s.empty()){
			Bug b = bug_s.front();bug_s.pop();
			if(bug_m[b.body] > 0){
				continue;
			}
			if(b.red == bug_length || b.blue == bug_length || b.green == bug_length){
				res = b.days;
				break;
			}
			Bug temp;
			for(int i=0;i<bug_length-1;i++){
				temp = b;
				if(temp.body[i] != temp.body[i+1]){
					if(temp.body[i]+temp.body[i+1] == 'r'+'b'){
						temp.body[i] = temp.body[i+1] = 'g';
						temp.red--;temp.blue--;temp.green+=2;
					}else if(temp.body[i]+temp.body[i+1] == 'r'+'g'){
						temp.body[i] = temp.body[i+1] = 'b';
						temp.red--;temp.green--;temp.blue+=2;
					}else{
						temp.body[i] = temp.body[i+1] = 'r';
						temp.blue--;temp.green--;temp.red+=2;
					}
					temp.days++;
					bug_s.push(temp);

				}
			}
			bug_m[b.body]++;
		}

		if(res != -1){
			std::cout << res << std::endl;
		}else{
			std::cout << "NA\n";
		}
	}
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;

bool OK(string in){
	for(int i = 0;i<in.size();i++){
		if (in[0] != in[i]) return false;
	}
	return true;
}

char ch(char a, char b){
	if (a != 'r' && b != 'r') return 'r';
	else if (a != 'g' && b != 'g') return 'g';
	else return 'b';
}

int main(){
	string in;
	while(cin >> in){
		if (in == "0") break;
		queue<string> que;
		que.push(in);
		vector<string> check(pow(3, in.size()));
		int n = 0;
		bool map[60000] = {};
		while(true){
			int que_size = que.size();
			bool flag = false;
			if(que_size == 0){
				cout << "NA" << endl;
				break;
			}
			for(int i = 0;i<que_size;i++){
				string que_front = que.front();
				que.pop();
				if(OK(que_front)){
					cout << n << endl;
					flag = true;
					break;
				}
				for (int j = 0; j < que_front.size() - 1; j++) {
					if (que_front[j] == que_front[j + 1]) continue;
					string for_push = que_front;
					for_push[j] = for_push[j + 1] = ch(que_front[j], que_front[j + 1]);
					int tmp = 0;
					for (int k = 0; k < for_push.size(); k++){
						if (for_push[k] == 'r') tmp += 2 * pow(3, k);
						else if (for_push[k] == 'g') tmp += pow(3, k);
					}
					if(!map[tmp]){
						map[tmp] = true;
						que.push(for_push);
					}
				}
			}
			if (flag) break;
			n++;
		}

	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using P = pair<string, int>;

int main() {
  map<char, int> rbg;
  rbg['r'] = 0;
  rbg['b'] = 1;
  rbg['g'] = 2;

  string s;
  while (cin >> s && s[0] != '0') {
    vector<string> goal;
    for (int i = 0; i < 3; ++i) goal.push_back(string(s.length(), "rbg"[i]));

    auto check = [&](string s) {
      for (int i = 0; i < 3; ++i) {
        if (goal[i] == s) return true;
      }
      return false;
    };

    map<string, bool> mp; mp[s] = true;
    queue<P, deque<P>> que; que.push({s, 1});

    int ans = 0;
    bool ok = check(s);
    while (!que.empty() && !ok) {
      tie(s, ans) = que.front(); que.pop();
      if (!mp[s]) continue;

      for (int i = 0; i < s.length() - 1; ++i) {
        string tmp(s);
        if (s[i] == s[i + 1]) continue;
        int c = 3 - rbg[s[i]] - rbg[s[i + 1]];
        tmp[i] = tmp[i + 1] = "rbg"[c];
        if (check(tmp)) ok = true;
        if (!mp[tmp]) que.push({tmp, ans + 1});
        mp[tmp] = true;
      }
    }
    cout << ( (ok) ? to_string(ans) : "NA" ) << endl;
  }
  return 0;
}