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

typedef pair<string,int> P;

int n;
set<string> closed;

bool check(string s){
  for(int i = 0; i < n - 1; i++){
    if(s[i] != s[i + 1]){
      return false;
    }
  }
  return true;
}

void solve(string s){
  queue<P> open;
  open.push(P(s, 0));
  closed.clear();
  closed.insert(s);

  while(!open.empty()){
    P p = open.front(); open.pop();
    s = p.first;

    if(check(s)){
      cout << p.second << endl;
      return;
    }

    for(int i = 0; i < n - 1; i++){
      if(s[i] != s[i + 1]){
        string next = s;
        if(s[i] != 'r' && s[i + 1] != 'r') next[i] = next[i + 1] = 'r';
        if(s[i] != 'g' && s[i + 1] != 'g') next[i] = next[i + 1] = 'g';
        if(s[i] != 'b' && s[i + 1] != 'b') next[i] = next[i + 1] = 'b';

        if(closed.find(next) == closed.end()){
          closed.insert(next);
          open.push(P(next, p.second + 1));
        }
      }
    }
  }

  cout << "NA\n";
}

int main(){
  string s;

  while(cin >> s, s != "0"){
    n = s.length();
    solve(s);
  }
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	char a[] = {'r','g','b'};
	set<string> u;
	queue< pair<string,int> > q;
	cin >> s;
	if(s == "0") break;
	u.insert(s);
	q.push( pair<string,int>(s,0));
	int ans = 0;
	while(!q.empty()){
	    s = q.front().first;
	    int v = q.front().second;
	    q.pop();
	    REP(i,s.size()-1){
		if(s[i] != s[i+1]){
		    string s1 = s;
		    char key;
		    REP(j,3){
			if(s[i] != a[j] && s[i+1] != a[j]){
			    key = a[j];
			}
		    }
		    s1[i] = key;
		    s1[i+1] = key;
		    if(u.find(s1) == u.end()){
			u.insert(s1);
			q.push(pair<string,int>(s1,v+1));
		    }
		}
	    }
	    bool flg = true;
	    REP(i,s.size()-1){
		if(s[i] != s[i+1] ) flg = false;
	    }
	    if(flg){
		cout << v << endl;
		break;
	    }else if(q.empty()){
		cout << "NA" << endl;
		break;
	    }
	}
    }
    return 0;
}