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 <algorithm>
#include <vector>
#include <map>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <set>
#include <queue>
#include <stack>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<string, int>;
using edge = struct
{
    int to;
    int cost;
};

int wfs(string worm)
{
    queue<P> que;
    que.push(P(worm, 0));

    map<string, int> mp;

    while (!que.empty())
    {
        P p = que.front();
        que.pop();

        string tmp = p.first;

        if(mp[p.first]) continue;

        bool flag = true;

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

        if (flag) return p.second;

        mp[p.first] = p.second;

        for(int i = 0; i < tmp.size() - 1; i++)
        {
            if(tmp[i] == tmp[i + 1]) continue;

            string nworm = tmp;

            if(tmp[i] != 'g' && tmp[i + 1] != 'g')
            {
                nworm[i] = 'g';
                nworm[i + 1] = 'g';
            }
            else if(tmp[i] != 'b' && tmp[i + 1] != 'b')
            {
                nworm[i] = 'b';
                nworm[i + 1] = 'b';
            }
            else
            {
                nworm[i] = 'r';
                nworm[i + 1] = 'r';
            }

            que.push(P(nworm, p.second + 1));

        }        
    }
    return -1;
}

int main()
{
    string input;

    while(cin >> input && input != "0")
    {
        cout << (wfs(input) != - 1 ? to_string(wfs(input)) : "NA") << endl;
    }

    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define QUEUE_NUM (20386)

struct Data{
	char color[15];
	int cnt;
};

typedef struct {
	int top,bottom;
	Data data[QUEUE_NUM];
}Queue;

//QÉdataðÇÁ
void push(Queue* Q, Data data){
	Q->data[Q->bottom] = data;
	Q->bottom = (Q->bottom +1);
}

//QÌæªvfðæ¾
Data front(Queue* Q)
{
	return Q->data[Q->top];
}

//QÌæªvfðí
void pop(Queue* Q)
{
	Q->top = (Q->top + 1) %QUEUE_NUM;
}

int check(char str[],int n){
	int i,number,tem=1,temp;
	for(i=0;i<n;i++){
		if(str[i]=='r'){
			str[i]='0';
		}
		else if(str[i]=='g'){
			str[i]='1';
		}
		else if(str[i]=='b'){
			str[i]='2';
		}
		else{
			str[i]='\0';
		}
	}
	
	number = atoi(str);
//	puts("\n--in--");
//	printf("%d  ",number);
	number=0;
	while(i!=-1){
		temp = (str[i]-48);
		number += tem*temp;
		tem*=3;
		i--;
	}
//	printf("%d\n",number);
//	puts("--o--");
	return number;
}

int main(void)
{
	while(1){
		int i,j,n,ans=-1;
		char col[4]={'r','g','b'},str[16];
		int temp;
		bool visited[178000]={0};
		Queue Q = {0,0,{0}};
		Data bag;
		
		scanf("%s",bag.color);
		if(bag.color[0]=='0')break;
		n = strlen(bag.color);
		bag.cnt=0;
		push(&Q,bag);
		
		while(!(Q.bottom==Q.top)){
			int flg=0,flag=0;
			Data d = front(&Q); 
			pop(&Q);
			
			
			for(i=0;i<n-1;i++){
				if(d.color[i]!=d.color[i+1])flg=1;
			}
			if(flg==0){
				ans=d.cnt;
				break;
			}
			
			for(i=0;i<n-1;i++){
				if(d.color[i] != d.color[i+1]){
					int used[4]={0};
					Data next;
					flg=0;
					strcpy(next.color ,d.color);
					
					for(j=0;j<3;j++){
						if(col[j]==next.color[i])used[j]=1;
						if(col[j]==next.color[i+1])used[j]=1;
					}
					
					for(j=0;j<3;j++){
						if(used[j]==0){
							next.color[i]=col[j];
							next.color[i+1]=col[j];
						}
					}
					
					/**/
					strcpy(str,next.color);
					temp=check(str,n);
					
					if(visited[temp]==1){
				//		printf("d¡\n");
						continue;
					}
					else {
						visited[temp]=1;
					}
					
					/**/
	//				printf("now%d == %s\n",next.cnt,next.color);
				//	Sleep(100);
					next.cnt = d.cnt+1;
					push(&Q,next);
				}
			}
		}
		if(ans==-1){
			puts("NA");
		}
		else {
			printf("%d\n",ans);
		}
	}
	return 0;
}