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 <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    string color = "rgb";

    for(;;){
        string s;
        cin >> s;
        if(s == "0")
            return 0;
        int n = s.size();

        set<string> worm;
        queue<pair<string, int> > q;
        worm.insert(s);
        q.push(make_pair(s, 0));
        int ok = false;
        while(!q.empty()){
            s = q.front().first;
            int k = q.front().second;
            q.pop();

            ok = true;
            for(int i=1; i<n; ++i){
                if(s[i] != s[i-1]){
                    ok = false;
                    break;
                }
            }
            if(ok){
                cout << k << endl;
                break;
            }

            for(int i=1; i<n; ++i){
                if(s[i] == s[i-1])
                    continue;
                for(int j=0; j<3; ++j){
                    if(s[i] != color[j] && s[i-1] != color[j]){
                        string s1 = s;
                        s1[i] = s1[i-1] = color[j];
                        if(worm.find(s1) == worm.end()){
                            worm.insert(s1);
                            q.push(make_pair(s1, k+1));
                        }
                    }
                }
            }
        }

        if(!ok)
            cout << "NA" << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

static const char TBL[] = " rgb";

unsigned encode(const string& s)
{
  unsigned t = 0;
  for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
    t <<= 2;
    switch (*it) {
      case 'r': t |= 1; break;
      case 'g': t |= 2; break;
      case 'b': t |= 3; break;
      default:  throw __LINE__;
    }
  }
  return t;
}

string decode(unsigned t)
{
  string s;
  while (t != 0) {
    s = TBL[t&3] + s;
    t >>= 2;
  }
  return s;
}

int bfs(unsigned init)
{
  queue<unsigned> q;
  vector<int> dist(1<<20, 10000000);
  q.push(init);
  dist[init] = 0;
  while (!q.empty()) {
    const int t = q.front();
    const string s = decode(t);
    q.pop();
    const int d = dist[t];
    const int N = s.size();
    if (count(s.begin(), s.end(), s[0]) == N) {
      return d;
    }
    for (int i = 0; i < N-1; i++) {
      if (s[i] != s[i+1]) {
        string u = s;
        for (int j = 1; j <= 3; j++) {
          if (TBL[j] != s[i] && TBL[j] != s[i+1]) {
            u[i] = u[i+1] = TBL[j];
            const unsigned v = encode(u);
            if (d+1 < dist[v]) {
              dist[v] = d+1;
              q.push(v);
            }
            break;
          }
        }
      }
    }
  }
  return -1;
}

int main()
{
  string s;
  while (cin >> s && s != "0") {
    const int ans = bfs(encode(s));
    if (ans < 0) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}