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

using namespace std;

map<string, char> rule = { 
	{"rb", 'g'},
	{"br", 'g'},
	{"rg", 'b'},
	{"gr", 'b'},
	{"gb", 'r'},
	{"bg", 'r'} };

bool isjudge(string s){
	bool r = true;
	char c = s[0];
	for(int i = 0;i < s.length();++i){
		r &= (c == s[i]);
	}
	return r;
}

int bfs(string s){
	queue<string> q;
	map<string, int> v;
	v[s] = 0;
	q.push(s);
	while(!q.empty()){
		string t = q.front(), n;
		char c;
		q.pop();
		if(isjudge(t)) return v[t];
		for(int i = 0;i < (t.length() - 1);++i){
			if(t[i] != t[i + 1]){
				n = t;
				c = rule[t.substr(i, 2)];
				n[i] = n[i + 1] = c;
				if(v.count(n) == 0){
					q.push(n);
					v[n] = v[t] + 1;
				}
			}
		}
	}
	return -1;
}

int main(){
	string s;
	int c;
	while(cin >> s){
		if(s == "0") break;
		else {
			c = bfs(s);
			if(c == -1) cout << "NA" << endl;
			else cout << c << endl;
		}
	}
	return 0;
}

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

typedef pair<string ,int> P;

int bfs(string str)
{
	map<string, int> m;
	queue<P> que;
	que.push(P(str,1));
	while(!que.empty())
	{
		P p = que.front();que.pop();
		if(m[p.first]) continue;//すでに数字が存在するときはやりなおし
		bool flag=true;
		for(int i=0;i<p.first.size()-1;++i){
			if(p.first[i] != p.first[i+1])
				flag = false;
		}
		if(flag) return p.second-1;
		m[p.first] = p.second;//重複をなくす
		for(int i=0;i<str.size()-1;++i){
			char c;
			if(p.first[i] == p.first[i+1])
				continue;
			if(p.first[i] != 'r' && p.first[i+1] != 'r')
				c = 'r';
			if(p.first[i] != 'g' && p.first[i+1] != 'g')
				c = 'g';
			if(p.first[i] != 'b' && p.first[i+1] != 'b')
				c = 'b';
			string next = p.first;
			next[i] = next[i+1] = c;
			que.push(P(next,p.second+1));
		}
	}
	return -1;
}

int main(void)
{
	while(1){
		string str;
		cin >> str;
		if(str[0] == '0')
			return 0;
		int res = bfs(str);
		if(res >= 0)
			cout << res << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int,int> pii;

bool isAns(string s){
  int len = s.length();
  FOR(i,1,len){
    if(s[0] != s[i]) return false;
  }
  return true;
}

char diffColor(char a, char b){
  if((a == 'r' && b == 'g') || (a == 'g' && b == 'r')){
    return 'b';
  }else if((a == 'g' && b == 'b') || (a == 'b' && b == 'g')){
    return 'r';
  }else{
    return 'g';
  }
}

int main(){

  while(true){
    string s;
    cin >> s;
    if(s == "0") break;

    map<string, int> mp;
    mp[s] = 1;
    queue<string> que;

    que.push(s);
    bool found = false;
    while(!que.empty()){
      s = que.front();
      que.pop();

      if(isAns(s)){
        found = true;
        cout << mp[s] - 1 << "\n";
        break;
      }

      int len = s.length();
      REP(i,len-1){
        if(s[i] != s[i+1]){
          string v = s;
          char tmp = diffColor(s[i], s[i+1]);
          v[i] = tmp; v[i+1] = tmp;
          if(mp[v] == 0){
            mp[v] = mp[s] + 1;
            que.push(v);
          }
        }
      }
    }
    if(!found){
      cout << "NA\n";
    }
  }
  return 0;
}