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 <utility>
#include <set>
using namespace std;
typedef pair<string,int> P;
int main() {
    string S;
    while(cin>>S,S[0]!='0') {
        set<string> ST;
        queue<P> Q;
        Q.push(P(S,0));
        string R,G,B;
        bool OK;
        while(!Q.empty()) {
            P p=Q.front();Q.pop();
            OK=true;
            for(int i=0;i<p.first.size()-1;i++) {
                string SS=p.first;
                if((p.first[i]=='r'&&p.first[i+1]=='g')||(p.first[i]=='g'&&p.first[i+1]=='r')) {
                        SS[i]=SS[i+1]='b';
                        if(ST.find(SS)==ST.end()){
                            Q.push(P(SS,p.second+1));
                            ST.insert(SS);
                        }
                        OK=false;
                }else if((p.first[i]=='r'&&p.first[i+1]=='b')||(p.first[i]=='b'&&p.first[i+1]=='r')) {
                        SS[i]=SS[i+1]='g';
                        if(ST.find(SS)==ST.end()){
                            Q.push(P(SS,p.second+1));
                            ST.insert(SS);
                        }
                        OK=false;
                }else if((p.first[i]=='g'&&p.first[i+1]=='b')||(p.first[i]=='b'&&p.first[i+1]=='g')) {
                        SS[i]=SS[i+1]='r';
                        if(ST.find(SS)==ST.end()){
                            Q.push(P(SS,p.second+1));
                            ST.insert(SS);
                        }
                        OK=false;
                }
            }
            if(OK) {cout<<p.second<<endl;break;}
        }
        if(!OK)cout<<"NA"<<endl;
    }
}

C++: #include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
map<char, map<char, char> > convert;

void solve(const string& s) {
	map<string, int> visited;
	queue<string> que;
	visited[s] = 0;
	que.push(s);
	while (!que.empty()) {
		string current = que.front();
		que.pop();
		if (current == string(current.size(), current[0])) {
			cout << visited[current] << endl;
			return;
		}
		for (unsigned i = 0; i < current.size() - 1; i++) {
			if (current[i] != current[i + 1]) {
				char c = convert[current[i]][current[i + 1]];
				string next = current;
				next[i] = next[i + 1] = c;
				if (visited.find(next) != visited.end()) continue;
				visited[next] = visited[current] + 1;
				que.push(next);
			}
		}
	}
	cout << "NA" << endl;
}

int main() {
	convert['r']['g'] = 'b';
	convert['g']['r'] = 'b';
	convert['r']['b'] = 'g';
	convert['b']['r'] = 'g';
	convert['b']['g'] = 'r';
	convert['g']['b'] = 'r';
	string str;
	while (cin >> str, str != "0") {
		solve(str);
	}
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

// c1, c2 テ」ツ??r', 'g', 'b', テ」ツ?凝」ツ?、(c1 != c2)テ」ツ?ョテ」ツ?ィテ」ツ??
// m[c1+c2] テ」ツ?ァ c1, c2 テ」ツ?ァテ」ツ?ェテ」ツ??r', 'g', 'b'テ」ツ?古ィツソツ氾」ツ?」テ」ツ?ヲテ」ツ?湘」ツつ?
map<int,char> m;

// テ・ツ?ィテゥツδィテ・ツ青古」ツ?佚ィツ可イテ」ツ?凝」ツ?ゥテ」ツ??」ツ??
bool isSameColor(const string& s){
	char c[3] = {'r','g','b'};
	for(int i = 0 ; i < 3 ; i++ ){
		int cnt = 0;
		for(int j=0 ; j < s.size() ; j++ ){
			if( s[j] == c[i] )
				cnt++;
		}
		if( cnt == s.size() )
			return true;
	}
	return false;
}

int solve(string s_ ){
	// memo[s] = true テ」ツ?ェテ」ツつ嘉ヲツ篠「テァツエツ「テヲツクツ暗」ツ?ソ
	map<string,bool> memo;
	// q.front().first テ」ツ?ッ ティツ卍ォテ」ツ?ョテァツ環カテヲツ??string) q.front().second テ」ツ?ッテヲツ可凝ヲツ閉ー(int)
	queue< pair<string,int> > q;
	
	// テ・ツ按敕ヲツ慊淌ァツ環カテヲツ?凝」ツつ津」ツつュテ」ツδ・テ」ツδシテ」ツ?ォテ・ツ?・テ」ツつ古」ツつ?
	pair<string,int> ini( s_ , 0  );
	q.push( ini );
	memo[s_] = true;
	
	// テ」ツつュテ」ツδ・テ」ツδシテ」ツ?古ァツゥツコテ」ツ?ォテ」ツ?ェテ」ツつ凝」ツ?セテ」ツ?ァテ」ツδォテ」ツδシテ」ツδ?
	while( !q.empty() ){
		// stirng s; テ」ツ?ッティツ卍ォテ」ツ?ョテァツ環カテヲツ?? int cnt; テ」ツ?ッテヲツ可凝ヲツ閉ー
		string s = q.front().first;
		int cnt = q.front().second;
		q.pop();
		
		// テ・ツ?ィテゥツδィテ・ツ青古」ツ?佚ィツ可イテ」ツ?ェテ」ツつ嘉ヲツ篠「テァツエツ「テァツオツづ、ツコツ?
		if( isSameColor(s) ){
			return cnt;
		}
		
		// テ」ツ?セテ」ツ??ヲツ篠「テァツエツ「テ」ツ?療」ツ?ヲテ」ツ??」ツ?ェテ」ツ??ヲツャツ。テ」ツ?ョテァツ環カテヲツ?凝」ツつ津」ツつュテ」ツδ・テ」ツδシテ」ツ?ォテ・ツ?・テ」ツつ古」ツつ?
		for(int i=1 ; i < s.size() ; i++ ){
			if( s[i-1] != s[i] ){
				char next_color = m[s[i-1]+s[i]];
				char c1 = s[i-1];
				char c2 = s[i];
				s[i-1] = s[i] = next_color;
				if( memo[s] != true ){
					memo[s] = true;
					pair<string,int> next;
					next.first = s;
					next.second = cnt + 1;
					q.push( next );
				}
				s[i-1] = c1;
				s[i] = c2;
			}
		}
	}
	return -1;
}

int main(){
	string s;
	m['r'+'g'] = 'b';
	m['g'+'b'] = 'r';
	m['b'+'r'] = 'g';
	
	while( cin >> s , s != "0" ){
		int ans = solve( s );
		if( ans == -1 )
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
}