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 <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
typedef long long int lli;
using namespace std;

int main(){
	string str;
	while(cin>>str&&str!="0"){
		map<string,int> d;
		queue<string> que;
		que.push(str);
		d[str]=0;
		int flag1=true;
		while(que.size()){
			string tmp=que.front();
			que.pop();
			int flag2=true;
			char a=tmp[0];
			for(int i=1; i<tmp.size(); ++i){
				if(a!=tmp[i]) {
					flag2=false;
					break;
				}
				a=tmp[i];
			}
			if(flag2){
				cout << d[tmp] << "\n";
				flag1=false;
				break;
			}
			for(int i=0; i+1<tmp.size(); ++i){
				if(tmp[i]!=tmp[i+1]){
					string nstr=tmp;
					if(tmp[i]!='r'&&tmp[i+1]!='r'){
						nstr[i]='r';
						nstr[i+1]='r';
					} else if(tmp[i]!='g'&&tmp[i+1]!='g'){
						nstr[i]='g';
						nstr[i+1]='g';
					} else if(tmp[i]!='b'&&tmp[i+1]!='b'){
						nstr[i]='b';
						nstr[i+1]='b';
					}
					if(nstr[i]==nstr[i+1]&&d.count(nstr)==0){
						que.push(nstr);
						d[nstr]=d[tmp]+1;
					}
				}
			}
		}
		if(flag1) cout << "NA\n";
	}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int a[11];
int main()
{
	ios_base::sync_with_stdio(0);
	string s;
	while (cin >> s, s != "0") {
		int n = s.size();
		rep(i, n) switch (s[i]) {
			case 'r': a[i] = 0; break;
			case 'g': a[i] = 1; break;
			case 'b': a[i] = 2; break;
		}
		int x = 0;
		rep(i, n) x = x * 3 + a[n-1-i];

		queue<int> q;
		map<int, int> m;
		q.push(x);
		m[x] = 0;
		int res = -1;
		while (!q.empty()) {
			int p = q.front(); q.pop();
			int t = p, d = p % 3;
			bool ok = true;
			rep(i, n) { a[i] = t % 3; t /= 3; ok = (ok && (a[i] == d)); }
			if (ok) { res = m[p]; break; }

			rep(i, n-1) if (a[i] != a[i+1]) {
				int t1 = a[i], t2 = a[i+1];
				a[i] = a[i+1] = 3 - t1 - t2;
				int y = 0;
				rep(j, n) y = y * 3 + a[n-1-j];
				if (!contains(m, y)) { q.push(y); m[y] = m[p] + 1; }
				a[i] = t1, a[i+1] = t2;
			}
		}
		if (res >= 0) cout << res << endl;
		else cout << "NA" << endl;
	}
	return 0;
}