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;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define MOD 1000000007
#define ALL(a) begin((a)),end((a))
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
typedef vector<edge> edges;
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main()
{
	string s;
	while (cin >> s)
	{
		if (s == "0") break;
		queue<string> que;
		que.push(s);
		map<string,int> t;
		t[s] = 0;
		while (!que.empty())
		{
			string tmp = que.front();
			que.pop();
			bool f = true;
			REP(i, tmp.size())
			{
				if (tmp[i] != tmp[0]) f = false;
			}
			if (f)
			{
				cout << t[tmp] << endl;
				goto end;
			}
			REP(i, tmp.size() - 1)
			{
				if (tmp[i] != tmp[i + 1])
				{
					char a = 'r' + 'g' + 'b' - tmp[i] - tmp[i + 1];
					string tmps = tmp;
					tmps[i] = tmps[i + 1] = a;
					if (t.find(tmps) == t.end())
					{
						t[tmps] = t[tmp] + 1;
						que.push(tmps);
					}
				}
			}
		}
		cout << "NA" << endl;
	end:;
	}
	return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};

char worm[12];
int ans;
bool flag;
void bfs(){
  string change;
  map<string,int> used;
  queue<string> que;
  queue<int> zikan;
  que.push(worm);
  zikan.push(0);
  while(!que.empty()){
    string str = que.front();
    ans = zikan.front();
    que.pop();
    zikan.pop();
    for(int i = 0;i < str.length()-1;i++){//変化した状態が題意を満たすかチェック
      if(str[i] !=str[i+1]){
	break;
      }
      if(i ==str.length()-2){
	flag = true;
	return;
      }
    }
    for(int i = 0;i <strlen(worm)-1;i++){
      if(str[i]=='r'&&str[i+1]!='r'){
	if(str[i+1]=='b'){
	  change = str;
	  change[i] ='g';
	  change[i+1]='g';
	  if(used[change]==1){
	    continue;
	  }
	  que.push(change);
	  zikan.push(ans+1);
	  used[change]=1;
	}else if(str[i+1]=='g'){
	  change = str;
	  change[i] ='b';
	  change[i+1]='b';
	  if(used[change]==1){
	    continue;
	  }
	  que.push(change);
	  zikan.push(ans+1);
	  used[change]=1;
	}
      }else if(str[i] =='b'&&str[i+1]!='b'){
	if(str[i+1]=='r'){
	  change = str;
	  change[i] ='g';
	  change[i+1]='g';
	  if(used[change]==1){
	    continue;
	  }
	  que.push(change);
	  zikan.push(ans+1);
	  used[change]=1;
	}else if(str[i+1]=='g'){
	  change = str;
	  change[i] ='r';
	  change[i+1]='r';
	  if(used[change]==1){
	    continue;
	  }
	  que.push(change);
	  zikan.push(ans+1);
	  used[change]=1;
	}
      }
      else if(str[i]=='g'&&str[i+1]!='g'){
	if(str[i+1]=='b'){
	  change = str;
	  change[i] ='r';
	  change[i+1]='r';
	  if(used[change]==1){
	    continue;
	  }
	  que.push(change);
	  zikan.push(ans+1);
	  used[change]=1;
	}else if(str[i+1]=='r'){
	  change = str;
	  change[i] ='b';
	  change[i+1]='b';
	  if(used[change]==1){
	    continue;
	  }
	  que.push(change);
	  zikan.push(ans+1);
	  used[change]=1;
	}
      }
    }
  }
}

int main()
{
  while(cin>>worm){
    ans = 0;
    if(worm[0]=='0')
      return 0;
    flag = false;
    bfs();
    if(flag)
      cout <<ans<<endl;
    else
      cout <<"NA"<<endl;
  }
  return 0;
}