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& str)
{
	for (int i = 1; i < str.size(); i++){
		if (str[i] != str[0]) return false;
	}
	return true;
}

int main()
{
	string str;
	while (cin >> str, str != "0"){
		queue<pair<string, int> > que;
		que.push({str, 0});
		set<string> done;
		int res = -1;
		while (!que.empty()){
			auto p = que.front(); que.pop();
			//cout << p.first << endl;
			if (check(p.first)){
				res = p.second;
				break;
			}
			if (done.count(p.first)) continue;
			done.insert(p.first);
			
			for (int i = 1; i < p.first.size(); i++){
				if (p.first[i] != p.first[i - 1]){
					int cnt[256] = {0};
					cnt[p.first[i]]++;
					cnt[p.first[i - 1]]++;
					
					string tmp = p.first;
					if (!cnt['r']){
						tmp[i] = tmp[i - 1] = 'r';
					}
					else if (!cnt['g']){
						tmp[i] = tmp[i - 1] = 'g';
					}
					else {
						tmp[i] = tmp[i - 1] = 'b';
					}
					
					que.push({tmp, p.second + 1});
				}
			}
		}
		if (res == -1) puts("NA");
		else printf("%d\n", res);
	}
	
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <set>
#include <climits>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> P;
static const int INF=1<<24;

string s;
int qf;

int foo1(int D[]){
	int res=0;
	for(int i=0;i<s.size();i++){
		res*=10;
		res+=D[i];
	}
	return res;
}
void foo2(int D[],int m){
	rep(j,s.size()){
		D[j]=0;
	}
	//cout<<"D = ";
	for(int i=s.size()-1;i>=0;--i){
		D[i]=m%10;
		m/=10;
		//cout<<m<<endl;
		//cout<<"D = ";
		rep(j,s.size()){
			//cout<<D[j]<<" ";
		}
		//cout<<endl;
		//D[i]=2;
	}
	//cout<<endl;
}


int a[30];
int tmp[30];
set<int> vis;

bool check(){
	int t=a[0];
	rep(i,s.size()){
		if(t!=a[i]) return false;
	}
	return true;
}

int main(){
	
	while(cin>>s,s[0]!='0'){
		vis.clear();
		int ans=0;
		//int lim=pow(3,s.size());
		queue<int> qu;
		rep(i,s.size()){
			if(s[i]=='r') a[i]=0;
			else if(s[i]=='g') a[i]=1;
			else if(s[i]=='b') a[i]=2;
		}
		if(check()){
			cout<<"0"<<endl;
			continue;
		}
		
		qu.push(foo1(a));
		vis.insert(foo1(a));
		bool f=false;
		bool f1=false;
		int c=0;
		while(!qu.empty()&&!f1){
			c++;
			if(c>50) break;
			f=false;
			ans++;
			int qs=qu.size();
			rep(i,qs){
				qf=qu.front();
				//cout<<qf<<endl;
				qu.pop();
				foo2(a,qf);
				if(check()){
					cout<<ans-1<<endl;
					f1=true;
					break;
				}
				rep(j,s.size()-1){
					if(a[j]!=a[j+1]){
						rep(k,s.size()){
							tmp[k]=a[k];
						}
						if(a[j]*a[j+1]==2){
							tmp[j]=0;
							tmp[j+1]=0;
						}
						else if(a[j]==2||a[j+1]==2){
							tmp[j]=1;
							tmp[j+1]=1;
						}
						else{
							tmp[j]=2;
							tmp[j+1]=2;
						}
						if(vis.find(foo1(tmp))==vis.end()){
							qu.push(foo1(tmp));
							vis.insert(foo1(tmp));
							f=true;
						}
					}
				}
				if(!f) break;
			}
			if(f1) break;
		}
		if(f1) continue;
		if(!f&&!f1) cout<<"NA"<<endl;
		
	}
	
	
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;

typedef pair<string,int> psi;

char st[200][200]={0};

int main(void){
	
	A:;
	string str;
	cin>>str;
	
	if(str=="0")return 0;
	
	queue<psi> que;
	que.push(psi(str,0));
	
	map<string,int> memo;
	
	st['r']['b']='g';
	st['b']['r']='g';
	st['r']['g']='b';
	st['g']['r']='b';
	st['g']['b']='r';
	st['b']['g']='r';
	
	int ans = -1;
	while(1){
		if(que.empty()){
			break;
		}
		
		psi u = que.front();
		que.pop();
		
		bool flg = true;
		rep(i,u.first.size()-1)if(u.first[i]!=u.first[i+1])flg=false;
		
		if(flg){
			ans = u.second;
			break;
		}
		
		if(memo[u.first]==1)continue;
		memo[u.first]=1;
		
		rep(i,u.first.size()-1){
			if(u.first[i] == u.first[i+1])continue;
			string copy = u.first;
			char c3 = st[u.first[i]][u.first[i+1]];
			copy[i] = copy[i+1] = c3;
			que.push(psi(copy,u.second+1));
		}
	}
	
	if(ans==-1)printf("NA\n");
	else printf("%d\n",ans);
	
	goto A;
}