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

int cnt,ccnt = 0;
int len;
string s, ss, tmp;
int flag = 0;
int fflag=0;
char c;
int cc=0;
vector<string> v_list;
pair<string,int> ps;
map<string,int> m;
int main() {
	while (cin >> s) {
		if (s == "0") {
			break;
		}
		fflag=0;
		//v_list.push_back(s);
		len = s.length();
		queue<pair<string,int> > qq;
		qq.push(make_pair(s,0));
		while (!qq.empty()) {
			ps = qq.front();
			qq.pop();
			c = ps.first[0];
			ccnt=ps.second;
			flag = 0;

			for (int i = 1; i < len; i++) {
				if (c != ps.first[i]) {
					//cout<<ps.first[i]<<endl;//deb
					flag = 1;
					cc=i;
					break;
				}
			}
			if (flag) {
				for (int i = cc-1; i < len - 1; ++i) {
					tmp = ps.first;
					if (ps.first[i] != ps.first[i+1]) {
						if (ps.first[i] != 'r' && ps.first[i+1] != 'r') {
							tmp[i] = 'r';
							tmp[i + 1] = 'r';
							if(m[tmp])continue;
							m[tmp]=ps.second;
							qq.push(make_pair(tmp,ccnt+1));
						}
						if (ps.first[i] != 'g' && ps.first[i+1] != 'g' ){
							tmp[i] = 'g';
							tmp[i + 1] = 'g';
							if(m[tmp])continue;
							m[tmp]=ps.second;
							qq.push(make_pair(tmp,ccnt+1));
						}
						if (ps.first[i] != 'b' && ps.first[i+1] != 'b') {
							tmp[i] = 'b';
							tmp[i + 1] = 'b';
							if(m[tmp])continue;
							m[tmp]=ps.second;
							qq.push(make_pair(tmp,ccnt+1));
						}
					}
				}
			} else {
				cout << ccnt << endl;
				fflag=1;
				break;
			}
		}
		if(!fflag){printf("NA");cout<<endl;};
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <set>
using namespace std;
typedef pair<string,int> P;
int main() {
	string str;
	while(cin>>str,str!="0") {
		queue<P> Q;
		Q.push(P(str,0));
		set<string> S;
		S.insert(str);
		while(!Q.empty()) {
			P p=Q.front(); Q.pop();
			bool Flag=true;
			for(int i=0;i<p.first.size()-1;i++) {
				if(p.first[i]!=p.first[i+1]) {Flag=false;break;}
			}
			if(Flag) {printf("%d\n",p.second); break;}
			for(int i=0;i<p.first.size()-1;i++) {
				string tmp=p.first;
				if((p.first[i]=='r' && p.first[i+1]=='g') || (p.first[i+1]=='r' && p.first[i]=='g')) {
					tmp[i]=tmp[i+1]='b';
				}else if((p.first[i]=='r' && p.first[i+1]=='b') || (p.first[i+1]=='r' && p.first[i]=='b')) {
					tmp[i]=tmp[i+1]='g';
				}else if((p.first[i]=='b' && p.first[i+1]=='g') || (p.first[i+1]=='b' && p.first[i]=='g')) {
					tmp[i]=tmp[i+1]='r';
				}else continue;
				if(S.find(tmp)==S.end()) {
					S.insert(tmp);
					Q.push(P(tmp,p.second+1));
				}
			}
		}
	}
}

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

map<char, char> rot = {
    {'r', 'g'}, {'g', 'b'}, {'b', 'r'}
};
map<string, bool> used;

void rotate(string &s) {
    for (int i=0; i<(int)s.size(); ++i) s[i] = rot[s[i]];
}

int main() {
    string s;
    while (cin >> s, s != "0") {
        used.clear();
        string r = "", g = "", b = "";
        for (int i=0; i<(int)s.size(); ++i) {
            r += "r"; g += "g"; b += "b";
        }
        queue<pair<string,int> > q;
        q.push(make_pair(s, 0));
        int res = -1;
        while ( ! q.empty()) {
            string col = q.front().first;
            used[col] = true; rotate(col);
            used[col] = true; rotate(col);
            used[col] = true; rotate(col);
            int t = q.front().second;
            if (col == r || col == g || col == b) {
                res = t;
                break;
            }
            q.pop();
            for (int i=0; i<(int)col.size()-1; ++i) {
                if (col[i] != col[i+1]) {
                    string nxt = col;
                    if (col[i] != 'r' && col[i+1] != 'r') {
                        nxt[i] = 'r'; nxt[i+1] = 'r';
                    } else if (col[i] != 'g' && col[i+1] != 'g') {
                        nxt[i] = 'g'; nxt[i+1] = 'g';
                    } else if (col[i] != 'b' && col[i+1] != 'b') {
                        nxt[i] = 'b'; nxt[i+1] = 'b';
                    }
                    if ( ! used[nxt]) {
                        q.push(make_pair(nxt, t+1));
                    }
                }
            }
        }
        if (res < 0) {
            cout << "NA" << endl;
        } else {
            cout << res << endl;
        }
    }
    return 0;
}