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 <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vvi;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007
 
string str;
bool f(string s){
	for(int i = 1; i <s.size(); i++){
		if(s[i] != s[i-1]) return false;
	}
	return true;
}

string calc(int i,string s){
	string ret = s;
	if(s[i] != s[i+1]){
		if((ret[i]=='g' && ret[i+1] == 'r') ||(ret[i]=='r' && ret[i+1] == 'g')) ret[i] = ret[i+1] ='b';
		else if((ret[i]=='b' && ret[i+1] == 'r') ||(ret[i]=='r' && ret[i+1] == 'b')) ret[i] = ret[i+1] ='g';
		else if((ret[i]=='g' && ret[i+1] == 'b') ||(ret[i]=='b' && ret[i+1] == 'g')) ret[i] = ret[i+1] ='r';
	}
	return ret;
}

int main(){
	while(cin>>str){
		if(str == "0") break;
		set<string> s;
		queue<pair<string,int> > q;
		int ans = 0;
		bool flag = false;
		pair<string,int> p;
		s.insert(str);
		q.push(pair<string,int>(str,0));
		while(!q.empty()){
			p = q.front();
			q.pop();
			if(f(p.fi)) {
				ans = p.se;
				flag = true;
				break;
			}
			for(int i = 0; i< str.size()-1; i++){
				if(!s.count(calc(i,p.fi))){
					q.push(pair<string,int>(calc(i,p.fi),p.se+1));
					s.insert(calc(i,p.fi));
				}
			}
		}
		if(flag) cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
}

C++: import java.util.*;
public class Main{
    static Scanner kbd = new Scanner(System.in);
    public static void main(String[] args){
	while(kbd.hasNext()){
	    String s = kbd.nextLine();
	    if(!s.equals("0")) solve(s);
	}
    }

    static void solve(String s){
	//String[] pattern = new String[60000];
	HashSet<String> set = new HashSet<String>();
	int pcnt = 0;
	Stack stack = new Stack(s);
	String a = s;
	set.add(s);
	//pattern[pcnt++] = a;
	int t = 0;
	while(!stack.isEmpty() && !color(a)){
	    //System.out.println(a+" "+color(a));
	    Worm w = (Worm)stack.pop();
	    a = w.getbody();
	    t = w.getTime();
	    for(int i=0; i<a.length()-1; i++){
		if(!a.substring(i, i+1).equals(a.substring(i+1, i+2))){
		    String b = change(a, i, i+1);  
		    /*
		    int j=0;
		    while(j<pcnt && !pattern[j].equals(b)) j++;
		    if(pattern[j]==null){
			//System.out.println(b);
			pattern[j] = b;
			stack.in(b, t+1);
			pcnt++;
		    }
		    */
		    if(set.contains(b)) continue;
		    else{
			stack.in(b, t+1);
			set.add(b);
		    }
		}
	    }
	}
	if(color(a))
	    System.out.println(t);
	else
	    System.out.println("NA");
    }

    static boolean color(String x){
	String a = x.substring(0, 1);
	boolean ans = true;
	for(int i=0; i<x.length(); i++){
	    String y = x.substring(i, i+1);
	    ans = ans && a.equals(y);
	}
	return ans;
    }

    static String change(String x, int s, int e){
	String ans = "";
	for(int i=0; i<x.length(); i++){
	    if(i==s){
		String a = x.substring(i, i+1);
		i++;
		String b = x.substring(i, i+1);
		a = select(a, b);
		ans += a+a;
	    }
	    else{
		ans += x.substring(i, i+1);
	    }
	}
	return ans;
    }
	
    static String select(String x, String y){
	if(x.equals("r") || y.equals("r")){
	    if(y.equals("b") || x.equals("b")){
		return "g";
	    }
	    else return "b";
	}
	else return "r";
    }
}

class Stack {
    Object[] stack = new Object[60000];
    int sp;
    int out;
    public Stack(String s){
	sp = 0;
	out = 0;
	this.in(s, 0);
    }

    public  void in(String s, int t){
	Worm w = new Worm(s, t);
	stack[sp++] = w;
    }
    public Object pop(){
	return stack[out++];
    }
    /*
    public String getb(){
	Worm a = (Worm)stack[sp-1];
	return a.getbody();
    }
    public  int gett(){
	Worm a = (Worm)stack[sp-1];
	sp--;
	return a.getTime();
    }
    */
    public boolean isEmpty(){
	return sp==out;
    }
}

class Worm{
    String body;
    int time;
    public  Worm(String s, int t){
	body = s;
	time = t;
    }
    public  String getbody(){
	return body;
    }
    public int getTime(){
	return time;
    }
}