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 <string>
#include <queue>
#include <map>

using namespace std;

int solve(string taregt, string origin, int count);
bool checkSameColor(string str);
bool checkLastColor(vector<string> last_str, string str);

int main(int argc, char const *argv[]){
    string color;
    int count;

    while(cin>>color, color != "0"){
        count = 0;
        int result = solve(color, color, count);
        if (result == -1) cout<<"NA"<<endl;
        else cout<<result<<endl;
    }
    return 0;
}

bool checkSameColor(string str){
    char first = str[0];

    for (int i = 0; i < str.size(); i++){
        if (str[i] != first) return false;
    }

    return true;
}

int solve(string target, string origin, int count){   
    queue<pair<string, int> > que;
    que.push(make_pair(origin, 0));
    pair<string, int> worm;
    map<string, int> d;

    while (!que.empty()){
        worm = que.front();
        que.pop();
        string str = worm.first;
        
        if (d[str]) continue;

        if (checkSameColor(str)){
            return worm.second;
        }

        d[str] = worm.second;

        for (int i = 0; i < str.size()-1; ++i){
            string tmp_str = str;
            if((str[i] == 'r' && str[i+1] == 'b') 
                || (str[i] == 'b' && str[i+1] == 'r')){
                pair<string, int> tmp = make_pair(tmp_str.replace(i, 2, "gg"), worm.second+1);
                que.push(tmp);
            }
            else if((str[i] == 'b' && str[i+1] == 'g') 
                || (str[i] == 'g' && str[i+1] == 'b')){
                pair<string, int> tmp = make_pair(tmp_str.replace(i, 2, "rr"), worm.second+1);
                que.push(tmp);
            }
            else if((str[i] == 'g' && str[i+1] == 'r') 
                || (str[i] == 'r' && str[i+1] == 'g')){
                pair<string, int> tmp = make_pair(tmp_str.replace(i, 2, "bb"), worm.second+1);
                que.push(tmp);
            }
        }
    }
    return -1;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
typedef pair<string,int> Psi;

string in;

inline bool check(const string& str) {
  rep (i,str.size()) if (str[0] != str[i]) return false;
  return true;
}

char another(const char a, const char b) {
  static const char cs[3] = { 'r', 'g', 'b' };
  rep (i,3) if (cs[i] != a && cs[i] != b) return cs[i];
}

int main() {
  while (cin >> in) {
    if (in == "0") break;
    if (check(in)) { cout << 0 << endl; continue; }
    
    queue<Psi> q;
    set<string> s;
    q.push(make_pair(in, 0));

    int ans = -1;
    while (!q.empty()) {
      Psi p = q.front(); q.pop();
      string str = p.first;
      
      if (s.count(str)) continue;
      s.insert(str);
      
      if (check(str)) {
        ans = p.second;
        break;
      }
      
      for (int i = 0; i < str.size() - 1; ++i) {
        if (str[i] != str[i+1]) {
          string copy = str;
          char c = another(str[i], str[i+1]);
          copy[i] = copy[i+1] = c;
          q.push(make_pair(copy, p.second + 1));
        }
      }
    }
    
    
    if (ans == -1) {
      cout << "NA" <<endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}