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<string>
#include<queue>
#include<cstring>
#include<set>
using namespace std;

//I¹Èç1ðÔ·
int samechar(string str);

//¯¶àÌª©Â©Á½ç1AÝÂ©ÈçÈ©Á½ç0ðÔ·
int samecheck(string str);

//strÌ[chari_1]Æ[chari_2]ÌFðÏ»³¹é
string exchange(string str, int chari_1, int chari_2);

set<string> samecheckset;

int main(){
	string input;
	while(cin >>input){
		if(input[0] == '0') break;
		
		samecheckset.clear();
		
		//cnt = oÍ·é¦([³) endflag = I¹ðª©Â©Á½ç1
		int cnt = 0, endflag = 0;
		
		//qu1 = ¡Ì[³Ìg qu2 = Ì[³Ìg
		queue<string> qu1, qu2;
		qu1.push(input);
		qu2.push("temp");
		while(!qu2.empty()){
			while(!qu2.empty()){
				qu2.pop();
			}
			while(!qu1.empty()){
				string temp = qu1.front();
				qu1.pop();
				
				//I¹ð¾Á½çbreak
				if(samechar(temp) == 1){
					endflag = 1;
					break;
				}
				
				//¯¶àÌªsamechecksetÉ©Â©Á½çcontinue
				if(samecheck(temp) == 1){
					continue;
				}
				
				//temp©çÏ»·é·×ÄÌFÌp^[ÌÂ\«ðqu2Éüêé
				for(int i = 0; i < temp.size() - 1; i++){
					if(temp[i] != temp[i + 1]){
						qu2.push(exchange(temp, i, i + 1));
					}
				}
			}
			
			if(endflag == 1){
				break;
			}
			
			//Ì[³ÉÚéÌÅAqu2Ìgð·×Äqu1ÉÚ·
			qu1 = qu2;
			
			//[³(b)ð1Ââ·
			cnt++;
		}
		if(endflag == 0){
			cout <<"NA" <<endl;
		} else{
			cout <<cnt <<endl;
		}
	}
}

string exchange(string str, int chari_1, int chari_2){
	if(str[chari_1] != 'r' && str[chari_2] != 'r'){
		str[chari_1] = 'r';
		str[chari_2] = 'r';
	} else if(str[chari_1] != 'g' && str[chari_2] != 'g'){
		str[chari_1] = 'g';
		str[chari_2] = 'g';
	} else if(str[chari_1] != 'b' && str[chari_2] != 'b'){
		str[chari_1] = 'b';
		str[chari_2] = 'b';
	}
	return str;
}

int samechar(string str){
	int ans = 1;
	char temp = str[0];
	for(int i = 1; i < str.size(); i++){
		if(temp != str[i]){
			ans = 0;
		}
	}
	return ans;
}

int samecheck(string str){
	if(samecheckset.find(str) == samecheckset.end()){
		samecheckset.insert(str);
		return 0;
	} else{
		return 1;
	}
}

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

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

typedef pair<int,string> pis;

int solve(string s)
{
	set<string> memo;
	priority_queue<pis,vector<pis>,greater<pis> > pq;
	pq.push(mp(0,s));
	while(!pq.empty()){
		pis cur=pq.top(); pq.pop();
		int curf=cur.first;
		string curs=cur.second;
		if(count(allof(curs),curs[0])==curs.size())
			return curf;
		if(memo.find(curs)!=memo.end())
			continue;
		memo.insert(curs);
		
		rep(i,curs.size()-1){
			char a=curs[i],b=curs[i+1];
			if(a==b)
				continue;
			if(a=='r' && b=='g' || a=='g' && b=='r') curs[i]=curs[i+1]='b';
			if(a=='g' && b=='b' || a=='b' && b=='g') curs[i]=curs[i+1]='r';
			if(a=='b' && b=='r' || a=='r' && b=='b') curs[i]=curs[i+1]='g';
			pq.push(mp(curf+1,curs));
			curs[i]=a;
			curs[i+1]=b;
		}
	}
	return -1;
}

int main()
{
	for(string s;cin>>s,s[0]!='0';){
		int res=solve(s);
		if(res==-1)
			puts("NA");
		else
			cout<<res<<endl;
	}
	
	return 0;
}