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;

int cnt,ccnt = 0;
int len;
string s, ss, tmp;
int flag = 0;
char c;
pair<string,int> ps;
int main() {
	while (cin >> s) {
		if (s == "0") {
			break;
		}
		len = s.length();
		queue<pair<string,int> > qq;
		qq.push(make_pair(s,0));
		while (!qq.empty()) {
			ps = qq.front();
			qq.pop();
			c = ps.first[0];
			ccnt=ps.second;
			flag = 0;
			for (int i = 1; i < len; i++) {
				if (c != ps.first[i]) {
					//cout<<ps.first[i]<<endl;//deb
					flag = 1;
					break;
				}
			}
			if (flag) {
				//cout<<ps.first<<endl;//deb
				if(ccnt>8){cout<<"NA"<<endl;break;}

				for (int i = 0; i < len - 1; ++i) {
					tmp = ps.first;
					if (ps.first[i] != ps.first[i+1]) {
						if (ps.first[i] != 'r' && ps.first[i+1] != 'r') {
							tmp[i] = 'r';
							tmp[i + 1] = 'r';
							//cout<<tmp<<endl;//deb
							qq.push(make_pair(tmp,ccnt+1));
						}
						if (ps.first[i] != 'g' && ps.first[i+1] != 'g' ){
							tmp[i] = 'g';
							tmp[i + 1] = 'g';
							//cout<<tmp<<endl;//deb
							qq.push(make_pair(tmp,ccnt+1));
						}
						if (ps.first[i] != 'b' && ps.first[i+1] != 'b') {
							tmp[i] = 'b';
							tmp[i + 1] = 'b';
							//cout<<tmp<<endl;//deb
							qq.push(make_pair(tmp,ccnt+1));
						}
					}
				}
			} else {
				cout << ccnt << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

string s, ans[3];
set<string> ses;
int res;

vector<string> change(string s){
	vector<string> vs;
	string t;
	REP(i, t.size() - 1){
		t = s;
		if (t[i] != t[i + 1]){
			if (t[i] == 'r'){
				if (t[i + 1] == 'g') t[i] = t[i + 1] = 'b';
				else t[i] = t[i + 1] = 'g';
			}
			else if (t[i] == 'g'){
				if (t[i + 1] == 'r')t[i] = t[i + 1] = 'b';
				else t[i] = t[i + 1] = 'r';
			}
			else{
				if (t[i + 1] == 'r')t[i] = t[i + 1] = 'g';
				else t[i] = t[i + 1] = 'r';
			}
			if (ses.find(t) == ses.end()){
				ses.insert(t);
				vs.push_back(t);
			}
		}
	}
	return vs;
}

void solve(string s){
	bool f = 1;
	queue<string> que;

	que.push(s);
	ses.insert(s);

	int k = que.size(), co = -1;

	for (int j = 0; j < k;j++){
		vector<string> vs;
		string t = que.front();

		//cout << "!" << t << endl;
		//if (f){
		//	cout << "!";
		//	REP(i, 3){
		//		if (t == ans[i]){
		//			cout << "!";
		//			cout << t << endl;
		//			f = 0;
		//		}
		//	}
		//}

		//cout << t << endl;
		//cout << "res ; " << res << endl;
		//cout << "j : " << j << endl;

		que.pop();
		vs = change(t);
		REP(i, vs.size()){
			que.push(vs[i]);
			/*cout << vs[i] << " ";*/
		}
		//cout << endl;

		if (j == k - 1){
			j = -1;
			k = que.size();
			res++;
		}
		/*cout << res << endl;*/

		if (f){
			REP(i, 3){
				if (find(ALL(vs), ans[i]) != vs.end()){
					//cout << "!";
					//cout << t << endl;
					//cout << "!";
					if (res == 1) cout << res << endl;
					else cout << res + 1 << endl;
					
					f = 0;
					break;
				}
			}
		}
		if (!f)break;
	}

	if (f)cout << "NA" << endl;
}

int main(){
	while (cin >> s){
		if (s == "0")break;
		ans[0] = ans[1] = ans[2] = "";
		REP(i, s.size()){
			ans[0] += 'r';
			ans[1] += 'g';
			ans[2] += 'b';
		}
		if (ans[0] == s || ans[1] == s || ans[2] == s){
			cout << 0 << endl;
			continue;
		}
		res = 0;
		solve(s);
	}
}