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<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define EPS 1e-8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define MAX_v 100
typedef long long ll;
typedef unsigned long long ull;
typedef pair<string,int> P;
int main(){
	string s;
	while(cin >> s , s!="0"){
		int len = s.size();
		int check = -1;
		queue<P> Q;
		Q.push(P(s,0));
		set<string> vis;
		while( Q.size() ){
			P p = Q.front();Q.pop();
			string tmp = p.first;
			string k = p.first;
			//cout << "D:" << tmp << endl;
			bool f = false;
			if(vis.find(tmp) != vis.end())continue;
			rep(i,len-1){
				if(tmp[i] != tmp[i+1]){f = true;break;}
			}
			if(!f){check = p.second;break;}
			vis.insert(tmp);
			rep(i,len-1){
				if(tmp[i] < tmp[i+1]){
					swap(k[i],k[i+1]);
				}
				if(k[i]=='r'&&k[i+1]=='g'){k[i]=k[i+1]='b';Q.push(P(k,p.second+1));}
				else if(k[i]=='r'&&k[i+1]=='b'){k[i]=k[i+1]='g';Q.push(P(k,p.second+1));}
				else if(k[i]=='g'&&k[i+1]=='b'){k[i]=k[i+1]='r';Q.push(P(k,p.second+1));}
				k = p.first;
			}
		}
		(check != -1) ? printf("%d\n",check) : puts("NA");
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,string> P;

static const double eps = 1e-8;

static const char a[] = {'r','g','b'};

bool checkColor(const string& str){

	bool res = false;
	for(int i=0;i<3;i++){
		if(count(str.begin(),str.end(),a[i]) == str.size()) res=true;
	}

	return res;
}

int changeColor(string str,int cost,map<string,int>& visited){

	if(checkColor(str)) return 0;

	priority_queue<P,vector<P>,greater<P> > que;
	que.push(P(0,str));

	while(!que.empty()){
		int cost = que.top().first;
		string now = que.top().second;

		que.pop();
		for(int i=0;i+1<now.size();i++){
			string tmp = now;
			if(now[i] != now[i+1]){
				for(int j=0;j<3;j++){
					if(a[j] != now[i] && a[j] != now[i+1]){
						tmp[i] = a[j];
						tmp[i+1] = a[j];
						break;
					}
				}

				if(visited.find(tmp) != visited.end()) continue;

				visited[tmp] = cost + 1;
				if(checkColor(tmp)) return cost+1;
				que.push(P(cost+1,tmp));
			}
		}
	}

	return -1;

}

int main(){
	string str;

	while(cin>>str){
		if(str.size() == 1 && str[0] == '0') break;
		map<string,int> visited;
		visited[str] = 0;

		int res = changeColor(str,0,visited);

		if(res==-1) printf("%s\n","NA");
		else printf("%d\n",res);
	}
}