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;

const int inf = 99999;

struct St{
  string worm;
  int cnt;
};

typedef map<string, bool> Mp;

bool check(string str)
{
  bool f = true;  

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

  return f;
}

char getC(char s, char t)
{
  if((s == 'r' && t == 'g') || (s == 'g' && t == 'r')){
    return 'b';
  }
  if((s == 'r' && t == 'b') || (s == 'b' && t == 'r')){
    return 'g';
  }

  return 'r';
}

int bfs(string str)
{
  int r = inf+1;
  queue<St> que;
  map<string, bool> mpp;
  St st1, st2;
  st1.worm = str;
  st1.cnt = 0;
  que.push(st1);
  mpp[str] = true;

  while(!que.empty()){
    st1 = que.front();
    que.pop();
    if(check(st1.worm) || st1.cnt > inf){
      r = st1.cnt;
      break;
    }

    for(int i = 0; i < (st1.worm).size()-1; i++){
      string s1 = st1.worm;
      if(s1[i] != s1[i+1]){
	s1[i] = s1[i+1] = getC(s1[i], s1[i+1]);
	if(!mpp[s1]){
	  mpp[s1] = true;
	  st2.worm = s1;
	  st2.cnt = st1.cnt+1;
	  que.push(st2);
	}
      }
    }
  }


  return r;
}

int main()
{
  int t;
  string str;

  while(cin>>str && str != "0"){
    t = bfs(str);
    if(t > inf){
      cout << "NA" << endl;
    } else {
      cout << t << endl;
    }
  }

  return 0;
}

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

int main() {
	int ans,i,j,k,f,e,u[60000],v;
	string c,a,b,z1,z2,z3;
	stack<string> s[2];
	while(cin >> c) {
		if (c[0]=='0') break;
		for (i=0;i<60000;i++) u[i]=0; v=f=0; ans=-1;
		z1=z2=z3=""; z1.append(c.size(),'r'); z2.append(c.size(),'g'); z3.append(c.size(),'b');
		s[f].push(c);
		while(!s[f].empty()) {
			ans++;
			while(!s[f].empty()) {
				c=s[f].top(); s[f].pop();
				if (c==z1 || c==z2 || c==z3) { v=1; break;} 
				for (i=0; i<c.size()-1; i++) {
				  a=c[i]; b=c[i+1];
			      if (c[i]==c[i+1]) continue;
			      if ((a=="r" && b=="g") || (a=="g" && b=="r")) c[i]=c[i+1]='b';
			      if ((a=="b" && b=="r") || (a=="r" && b=="b")) c[i]=c[i+1]='g';	 	
			      if ((a=="b" && b=="g") || (a=="g" && b=="b")) c[i]=c[i+1]='r';
			      for (j=0,k=1,e=0;j<c.size();j++,k*=3) if (c[j]=='g') e+=k; else if (c[j]=='b') e+=2*k;
			      if (u[e]==0) { u[e]=1; s[1-f].push(c);}
			      c[i]=a[0]; c[i+1]=b[0];
				  }
				}
				if (v==1) {
					 while(!s[f].empty()) s[f].pop();
					 while(!s[1-f].empty()) s[1-f].pop();
					 break;
				 } 
				f=1-f;
		 }
		 if (v==1) cout << ans << endl; else cout << "NA" << endl;
	 }
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

char convert[256];

void init() {
	convert['r' + 'g'] = 'b';
	convert['g' + 'b'] = 'r';
	convert['b' + 'r'] = 'g';
}

string bfs(const string &worm) {
	typedef pair<string, int> state;

	const int n = worm.size();

	unordered_set<string> visited{worm};
	queue<state> que;
	que.push({worm, 0});


	while(!que.empty()) {
		const auto s = que.front().first;
		const auto d = que.front().second;
		que.pop();

		if(all_of(s.begin(), s.end(), [&](char c) { return c == s[0]; })) {
			return to_string(d);
		}

		for(int i = 0; i < n - 1; ++i) {
			if(s[i] != s[i + 1]) {
				string next(s);
				next[i] = next[i + 1] = convert[s[i] + s[i + 1]];

				if(!visited.count(next)) {
					visited.insert(next);
					que.push({next, d + 1});
				}
			}
		}
	}

	return "NA";
}

int main() {
	init();
	for(string worm; cin >> worm && worm != "0";) {
		cout << bfs(worm) << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using namespace std;

const int INF = 1e+9;
typedef pair<int, int> P;

int ans = 0;

string change(string s, int a, int b){
	if((s[a] == 'r' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'r'))
		s[b] = s[a] = 'b';
	else if((s[a] == 'b' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'b'))
		s[b] = s[a] = 'r';
	else
		s[b] = s[a] = 'g';
	
	return s;
}

void bfs(string s, int res){
	if(res >= ans)
		return;
	for(int i = 0; i < s.size() - 1; ++i){
		if(s[i] != s[i + 1])
			break;
		if(i == s.size() - 2){
			ans = min(res, ans);
			return;
		}
	}
	if(res >= 9)
		return;
	for(int i = 0; i < s.size() - 1; ++i){
		if(s[i] != s[i + 1]){
			string str = change(s, i, i + 1);
			bfs(str, res + 1);
		}
	}
}

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