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<map>
#include<queue>

#define rep(i,n) for(int i=0;i<int(n);i++)

using namespace std;


class Q{
	public:
	string str;
	int dep;
	//Q(string a,int n){str=a;dep=n;}
	Q(string str, int dep) : str(str), dep(dep) {}
};

int saiki(string imo){
	
	queue<Q> que;
	
	que.push(Q(imo,0));
	
	map<string,int> visit;
	
	while(1){
		
		if(que.empty()==1){
			return -1;
		}
		
		Q u = que.front();
		que.pop();
		
		string s = u.str;
		
		if(visit[s]==1){
			continue;
		}
		visit[s]=1;
		
		
		//printf("%d pop( %s )\n",u.dep,s.c_str());
		char ch=s[0];
		int flg=1;
		rep(i,s.size()-1){
			if(s[i+1]!=ch) {flg=0;break;}
		}
		if(flg==1){
			return u.dep;
		}
		
		rep(i,s.size()-1){
			
			if((s[i]=='r' && s[i+1]=='g')||(s[i+1]=='r' && s[i]=='g')){
				string t=s;
				t[i]=t[i+1]='b';
				que.push(Q(t,u.dep+1));
		//printf("%d push( %s )\n",u.dep,t.c_str());
			}else
			if((s[i]=='b' && s[i+1]=='r')||(s[i+1]=='b' && s[i]=='r')){
				string t=s;
				t[i]=t[i+1]='g';
				que.push(Q(t,u.dep+1));
		//printf("%d push( %s )\n",u.dep,t.c_str());
			}else
			if((s[i]=='g' && s[i+1]=='b')||(s[i+1]=='g' && s[i]=='b')){
				string t=s;
				t[i]=t[i+1]='r';
				que.push(Q(t,u.dep+1));
		//printf("%d push( %s )\n",u.dep,t.c_str());
			}
		}
	}
	return 100;
	
}

int main(){
	while(1){
		string a;
		cin>>a;
		
		if(a=="0"){
			break;
		}
		
		int ans=saiki(a);
		
		if(ans==-1){
			puts("NA");
		}else{
		printf("%d\n",ans);
		}
	}
}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i,m,n) for(int i=m;i<n;i++)
#define rep(i,n) rep2(i,0,n)
#define ll long long
#define P pair<int,int>
#define pb push_back

char c[]={'r','g','b'};
string s;

int main(){
	while(cin>>s&&s!="0"){
		map<string,int> M;
		queue<string> Q;
		M[s]=0;
		Q.push(s);
		int ans=INF;
		while(Q.size()){
			string s=Q.front();
			int step=M[s],same=0;
			Q.pop();
			rep(i,s.size()-1){
				if(s[i]!=s[i+1]){
					char a=s[i],b=s[i+1];
					rep(j,3){
						if(s[i]!=c[j]&&s[i+1]!=c[j]){
							s[i]=s[i+1]=c[j];
							break;
						}
					}
					if(!M[s]){
						M[s]=step+1;
						Q.push(s);
					}
					s[i]=a;s[i+1]=b;
				}
				else same++;
			}
			if(same==s.size()-1){
				ans=step;
				break;
			}
		}
		if(ans==INF)cout<<"NA"<<endl;
		else cout<<ans<<endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>

using namespace std;

struct St{
  string worm;
  int cnt;
};

bool check(string str)
{
  bool f = true;  

  for(int i = 1; i < str.size(); i++){
    if(str[i] != str[0]){
      f = false;
      break;
    }
  }

  return f;
}

int bfs(string str)
{
  int r = 101;
  vector<St> vec;
  St stt, stt2;
  stt.worm = str;
  stt.cnt = 0;

  vec.push_back(stt);
  while(!vec.empty()){
    stt = vec[0];
    vec.erase(vec.begin());

    if(check(stt.worm) || stt.cnt > 10){
      r = stt.cnt;
      break;
    }

    for(int i = 0; i < (stt.worm).size()-1; i++){
      string s = stt.worm;
      if(s[i] != s[i+1]){
	if((s[i] == 'r' && s[i+1] == 'g')||(s[i] == 'g' && s[i+1] == 'r')){
	  s[i] = s[i+1] = 'b';
	} else if((s[i] == 'r' && s[i+1] == 'b')||(s[i] == 'b' && s[i+1] == 'r')){
	  s[i] = s[i+1] = 'g';
	} else {
	  s[i] = s[i+1] = 'r';
	}

	bool f = true;
	for(int j = 0; j < vec.size(); j++){

	  if(vec[j].worm == s){
	    f = false;
	    break;
	  }
	}

	if(f){
	  stt2.worm = s;
	  stt2.cnt = stt.cnt + 1;
	  vec.push_back(stt2);
	}
      }
    }

  }

  return r;
}

int main()
{
  int t;
  string str;

  while(cin>>str && str != "0"){

    t = bfs(str);
    if(t > 10){
      cout << "NA" << endl;
    } else {
      cout << t << endl;
    }
  }

  return 0;
}