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 <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<string, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

char judge(char a, char b){
    if((a == 'r' && b == 'b') || (a == 'b' && b == 'r')) return 'g';
    if((a == 'b' && b == 'g') || (a == 'g' && b == 'b')) return 'r';
    if((a == 'g' && b == 'r') || (a == 'r' && b == 'g')) return 'b';
}

int main(){
    while(1){
        string s; cin>>s;
        if(s == "0") return 0;

        queue<P> que;
        que.push(P(s, 0));
        map<string, bool> visited;
        visited[s] = true;
        bool isNA = true;
        while(que.size()){
            string t;
            int cnt;
            tie(t, cnt) = que.front();
            que.pop();

            bool valid = true;

            for(int i=0; i<t.size()-1; i++){
                if(t[i] != t[i+1]){
                    valid = false;
                    string nt = t;
                    nt[i] = nt[i+1] = judge(t[i], t[i+1]);

                    if(!visited[nt]){
                        visited[nt] = true;
                        que.push(P(nt, cnt+1));
                    }
                }
            }

            if(valid){
                isNA = false;
                cout << cnt << endl;
                break;
            }
        }

        if(isNA){
            cout << "NA" << endl;
        }
    }
}

C++: #include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<map>
#include<queue>

using namespace std;

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

class S{
public:
  string s;
  int n;
  S(string s,int n):s(s),n(n){}
};

int main(){
  while(1){
    string a;
    cin>>a;
    if(a=="0")break;

    queue<S> que;
    map<string,int> memo;
    char cc[111][111];
    rep(i,111)rep(j,111)cc[i][j]=-1;
    que.push(S(a,0));
    int ans=0;
    int kn=0;
    while(1){
      if(que.empty()==1){
	break;
      }
      S u = que.front();
      que.pop();
      ans=u.n;
      //cout<<u.s<<" "<<u.n<<endl;
      if(memo[u.s]!=0){
	continue;
      }
      memo[u.s]=1;
      int comp=1;
      int ch=u.s[0];
      reps(i,u.s.size()-1){
	if(ch!=u.s[i]){
	  comp=0;
	  break;
	}
      }
      if(comp==1){
	kn=1;
	break;
      }
      cc['r']['g']=cc['g']['r']='b';
      cc['r']['b']=cc['b']['r']='g';
      cc['b']['g']=cc['g']['b']='r';
      rep(i,u.s.size()-1){
	string cop=u.s;
	if(u.s[i]!=u.s[i+1]){
	  cop[i]=cop[i+1]=cc[cop[i]][cop[i+1]];
	  que.push(S(cop,u.n+1));
	}
      }
    }
    if(kn==1){
      printf("%d\n",ans);
    }else{
      printf("NA\n");
    }
  }
}