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;

bool check(string s){
    for(int i=0; i< s.size()-1; i++) if(s[i] != s[i+1]) return false;
    return true;
}

int main(){
    string s;
    while(cin >> s, s!= "0"){
        queue<pair<string, int> > que;
        que.push(make_pair(s, 0));
        int ret = 100;

        map<string, int> m;
        while(que.size()){
            s = que.front().first; int count = que.front().second; que.pop();

            if(ret < count) continue;
            if(check(s))ret = count;

            if(m[s] == 0 || count+1 < m[s]){
                string tmp = s;
                m[s] = count+1;
                string t = "rgb";
                vector<int> v(3); v[0] = 0; v[1] = 1; v[2] = 2;
                map<string, int> mp; mp["r"] = 0; mp["g"] = 1; mp["b"] = 2;
                do{
                    for(int i=0; i< s.size(); i++){
                        s[i] = t[v[mp[s.substr(i, 1)]]];
                    }
                    m[s] = count+1;
                }while(next_permutation(v.begin(), v.end()));
                s = tmp;
            }
            else continue;

            for(int i=0; i< s.size()-1; i++){
                if(s[i] != s[i+1]){
                    string t = s.substr(i, 2);
                    sort(t.begin(),t.end());
                    if(t == "br") t = "gg";
                    else if(t == "bg") t = "rr";
                    else if(t == "gr") t = "bb";
                    que.push(make_pair(s.substr(0, i)+t+s.substr(i+2), count+1));
                }
            }
        }
        if(ret == 100) cout << "NA" << endl;
        else cout << ret << endl;
    }
}

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

typedef pair<int, int> P;

int base[11];

int enc(int n, const int *c) {
  int r = 0;
  for (int i=0; i<n; i++) {
    r += c[i] * base[i];
  }
  return r;
}

void dec(int n, int p, int *c) {
  int t = p;
  for (int i=0; i<n; i++) {
    c[i] = t % 3; 
    t /= 3;
  }
}

int main() {
  string line;
  int n, c0, rr, gg, bb, c, m, t1, t2, t3;
  int color[10];
  bool used[60000];
  bool found;
  queue<P> q;

  base[0] = 1;
  for (int i=1; i<=10; i++) {
    base[i] = base[i-1]*3;
  }

  while (1) {
    getline(cin, line);
    if (line == "0") break;
    n = line.length();
    for (int i=0; i<n; i++) {
      if (line[i] == 'r') {
        color[i] = 0;
      } else if (line[i] == 'g') {
        color[i] = 1;
      } else {
        color[i] = 2;
      }
    }
    for (int i=0; i<base[n]; i++) {
      used[i] = false;
    }

    while (!q.empty()) q.pop();
    c0 = enc(n, color);
    for (int i=0; i<n; i++) color[i] = 0;
    rr = enc(n, color);
    for (int i=0; i<n; i++) color[i] = 1;
    gg = enc(n, color);
    for (int i=0; i<n; i++) color[i] = 2;
    bb = enc(n, color);
    found = false;
    q.push(make_pair(c0, 0));
    used[c0] = true;
    while (!q.empty()) {
      c = q.front().first;
      m = q.front().second;
      q.pop();
      if (c == rr || c == gg || c == bb) {
        found = true;
        break;
      }
      dec(n, c, color);
      for (int i=0; i<n-1; i++) {
        if (color[i] != color[i+1]) {
          t1 = color[i];
          t2 = color[i+1];
          t3 = 3 - t1 - t2;
          color[i] = color[i+1] = t3;
          c = enc(n, color);
          color[i] = t1;
          color[i+1] = t2;
          if (used[c]) continue;
          q.push(make_pair(c, m+1));
          used[c] = true;
        }
      }
    }
    if (!found) {
      cout << "NA" << endl;
    } else {
      cout << m << endl;
    }
  }
  return 0;
}