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 <utility>
#include <string>
#include <queue>
#include <set>
using namespace std;
#define P pair<string,int>
#define C(X)((X)=='r'?0:(X)=='b'?1:2)
int main() {
	string w;
	while(cin>>w,w!="0") {
		queue<P> q;
		set<string> s;
		q.push(P(w,0));
		s.insert(w);
		bool OK=false;
		while(!q.empty()) {
			P p=q.front();q.pop();
			bool ok=true;
			for(int i=1;i<p.first.size();i++) {
				if(p.first[i-1]!=p.first[i]) {
					ok=false;break;
				}
			}
			if(ok) {
				printf("%d\n",p.second);
				OK=true;break;
			}
			for(int i=1;i<p.first.size();i++) {
				if(p.first[i-1]!=p.first[i]) {
					string ns=p.first;
					bool chr[3]={0};
					chr[C(ns[i-1])]=true;
					chr[C(ns[i])]=true;
					for(int j=0;j<3;j++) {
						if(chr[j]==false) {
							ns[i-1]=ns[i]=(j==0?'r':j==1?'b':'g');
						}
					}
					if(s.find(ns)==s.end()) {
						q.push(P(ns,p.second+1));
						s.insert(ns);
					}
				}
			}
		}
		if(OK==false)printf("NA\n");
	}
}

C++: #include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
struct data{
	string str;
	int cnt;
	data(){}
	data(string str,int cnt):str(str),cnt(cnt){}
};
string func(string str,int i) {
	int r,g,b;
	char change;
	if(str[i] == 'r') {
		if(str[i+1] == 'g') change='b';
		else if(str[i+1] == 'b') change = 'g';
		else return str;
	}
	else if(str[i] == 'g') {
		if(str[i+1] == 'r') change='b';
		else if(str[i+1] == 'b') change = 'r';
		else return str;
	}
	else {
		if(str[i+1] == 'g') change = 'r';
		else if(str[i+1] == 'r') change = 'g';
		else return str;
	}
	str[i] = change;
	str[i+1] = change;
	return str;
}
bool check(string str) {
	char c = str[0];
	for(int i = 1; i < str.size(); i++) {
		if(str[i] != c) return false;
	}
	return true;
}
int main() {
	string str;
	while(1) {
		cin >> str;
		if(str == "0") break;
		int ans = -1;
		map<string,bool> memo;
		queue<data> Q;
		Q.push(data(str,0));
		while(!Q.empty()) {
			data d = Q.front();Q.pop();
			if(memo[d.str]) continue;
			memo[d.str] = true;
			if(check(d.str)) {
				ans = d.cnt;
				break;
			}
			for(int i = 0; i < d.str.size() - 1; i++) {
				string nstr = func(d.str,i);
				if(nstr == d.str) continue;
				Q.push(data(nstr,d.cnt+1));
			}
		}
		if(ans == -1) puts("NA");
		else printf("%d\n",ans);
	}
	return 0;
}

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

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

using P = pair<string, int>;

char getLast(char c1, char c2) {
    if (c1 > c2) swap(c1, c2);
    if (c1 == 'b' && c2 == 'g') return 'r';
    if (c1 == 'b' && c2 == 'r') return 'g';
    return 'b';
}

#include <set>
bool check(string s) {
    set<int> st;
    for(auto &c: s) {
        st.insert(c);
    }
    return st.size() == 1;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string s;
    while (cin >> s, s != "0") {
        map<string, int> mp;
        int n = s.size();
        queue<P> q;
        q.emplace(s, 0);
        int ans = -1;
        while (q.size()) {
            string e = q.front().first;
            int cnt = q.front().second;
            q.pop();
            if (check(e)) {
                ans = cnt;
                break;
            }
            mp[e] = cnt;
            REP (i, 0, n - 1) {
                if (e[i] != e[i + 1]) {
                    string tmp = e;
                    tmp[i] = tmp[i + 1] = getLast(e[i], e[i + 1]);
                    if (mp.count(tmp) == 0) {
                        q.emplace(tmp, cnt + 1);
                        mp[tmp] = cnt + 1;
                    }
                }
            }
        }
        if (ans == -1) {
            cout << "NA" << endl;
        } else {
            cout << ans << endl;
        }
    }
    return 0;
}