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;

bool isOneColor(const string &s) {
  for(int i = 1; i < s.size(); ++i)
    if(s[0] != s[i]) return false;
  return true;
}

int bfs(string src) {
  if(isOneColor(src)) return 0;
  const static string color = "rgb";
  map<string,int> cost;
  queue<string> que;
  cost[src] = 0;
  que.push(src);
  while(que.size()) {
    const string s = que.front();
    que.pop();
    for(int i = 0; i+1 < s.size(); ++i) {
      if(s[i] == s[i+1]) continue;
      string t = s;
      for(int j = 0; j < color.size(); ++j) {
        if(t[i] != color[j] && t[i+1] != color[j]) {
          t[i] = t[i+1] = color[j];
          break;
        }
      }
      if(cost.count(t)) continue;
      cost[t] = cost[s] + 1;
      que.push(t);
      if(isOneColor(t)) return cost[t];
    }
  }
  return -1;
}

int main() {
  string s;
  while(cin >> s && s != "0") {
    int res = bfs(s);
    if(res == -1) cout << "NA" << endl;
    else cout << res << endl;
  }
  return 0;
}

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

int main() {
	int ans,i,j,k,f,g,e,u[60000],v;
	string c,a,b,z1,z2,z3;
	stack<string> s[2];
	while(cin >> c) {
		if (c[0]=='0') break;
		for (i=0;i<60000;i++) u[i]=0; v=f=0; ans=-1;
		z1=z2=z3=""; z1.append(c.size(),'r'); z2.append(c.size(),'g'); z3.append(c.size(),'b');
		s[f].push(c);
		while(!s[f].empty()) {
			ans++;
			while(!s[f].empty()) {
				c=s[f].top(); s[f].pop();
				if (c==z1 || c==z2 || c==z3) { v=1; break;} 
				for (i=0; i<c.size()-1; i++) {
				  a=c[i]; b=c[i+1];
			      if (c[i]==c[i+1]) continue;
			      if ((a=="r" && b=="g") || (a=="g" && b=="r")) c[i]=c[i+1]='b';
			      if ((a=="b" && b=="r") || (a=="r" && b=="b")) c[i]=c[i+1]='g';	 	
			      if ((a=="b" && b=="g") || (a=="g" && b=="b")) c[i]=c[i+1]='r';
			      for (j=0,k=1,e=0;j<c.size();j++,k*=3) if (c[j]=='g') e+=k; else if (c[j]=='b') e+=2*k;
			      if (u[e]==0) { u[e]=1; s[1-f].push(c);}
			      c[i]=a[0]; c[i+1]=b[0];
				  }
				}
				if (v==1) { while(!s[f].empty()) s[f].pop(); break;} 
				f=1-f;
		 }
		 if (v==1) cout << ans << endl; else cout << "NA" << endl;
	 }
	return 0;
}