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<algorithm>
#include<vector>
#include<stack>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<complex>
#include<cfloat>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define EPS (1e-10)
using namespace std;

typedef unsigned int uint;
typedef long long ll;

int encode(char *buff, int &len){
  int ret = 0;
  len = 0;
  while(true){
    if(*buff == 'r'){
      ret <<= 2;
      ret += 0;
    }else if(*buff == 'g'){
      ret <<= 2;
      ret += 1;
    }else if(*buff == 'b'){
      ret <<= 2;
      ret += 2;
    }else{
      return ret;
    }
    buff++;
    len++;
  }
  return ret;
}

void print(int now, int len){
  REP(i,len){
    int t = (now >> (2 * i)) & ((1<<2) - 1);
    if(t == 0) putchar('r');
    if(t == 1) putchar('g');
    if(t == 2) putchar('b');
  }
  puts("");
}

int main(){
  char buff[64];
  int change[1<<6];

  memset(change, -1, sizeof(change));
  REP(i,3) REP(j,3) REP(k,3)
    if(i != j && j != k && i != k)
      change[i | (j << 2)] = (k | (k << 2));

  while(fgets(buff,sizeof(buff),stdin), *buff != '0'){
    int len;
    int now = encode(buff,len);

    int ans[3];
    REP(i,3){
      int tmp = 0;
      REP(j,len){
	tmp <<= 2;
	tmp += i;
      }
      ans[i] = tmp;
    }

    

    set<int> s;
    queue<int> q;
    int n = 0;
    q.push(now);
    q.push(-1);
    s.insert(now);

    while(true){
      now = q.front();

      bool f = false;
      REP(i,3){
	if(ans[i] == now){
	  printf("%d\n",n);
	  f = true;
	  break;
	}
      }
      if(f) break;

      q.pop();
      
      if(q.empty()){
	puts("NA");
	break;
      }

      if(now == -1){
	n++;
	q.push(-1);
	continue;
      }

      //printf("now: "); print(now,len);

      REP(i,len-1){
	int cc   = (now >> (i * 2)) & ((1<<4) - 1);

	if(change[cc] != -1){
	  int next = now & ~(((1<<4)-1) << (i * 2));
	  next |= (change[cc] << (i * 2));
	  //print(next, len);
	  if(s.find(next) == s.end()){
	    s.insert(next);
	    q.push(next);
	  }
	}
      }
    }

  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<string, int> I;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
char rest(char a, char b){
  char rgb[5] = "rgb";
  REP(i,3){
    if(rgb[i] != a && rgb[i] != b){
      return rgb[i];
    }
  }
  return ',';
}
  
int bfs(I insect){
  set<string> memo;
  queue<I> que;
  que.push(insect);
  while(!que.empty()){
    I inc = que.front(); que.pop();
    string s = inc.first;
    int t = inc.second;

    if(memo.find(s) != memo.end()) continue;
    memo.insert(s);

    bool goal = true; 
    REP(i,s.size()-1){
      if(s[i] != s[i+1]){
        goal = false;
        string nxt = s;
        char rschar = rest(s[i],s[i+1]);
        if(rschar == '.') abort();
        nxt[i] = rschar;
        nxt[i+1] = rschar;
        que.push(I(nxt, t+1));
      }
    }
    if(goal) return t;
  }
  return -1;
}

int main(void){
  string insect;
  while(cin>>insect){
    if(insect == "0") break;
    int ans = bfs(I(insect, 0));
    if(ans == -1) cout<< "NA" << endl;
    else  cout<< ans << endl;
  }
  return 0;
}