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 ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  while(cin >> s && s!= "0"){
    set<string> st;
    queue<string> q;
    q.push(s);
    int num = 0;
    int flag = 0;
    while(1){
      auto n = q.size();
      rep(i,n){
        auto el = q.front();q.pop();
//         debug(el);
        if(st.insert(el).se){
          auto el2 = el;
          bool f = true;
          rep(j,el.size()){
            if(el[0] != el[j]){
              f = false;
              break;
            }
          }
          if(f){
            flag = 2;
            break;
          }
          rep(j,el.size()-1){
            el = el2;
            if(el[j] != el[j+1]){
              if(el[j] != 'g' && el[j+1] != 'g'){
                el[j] = 'g';
                el[j+1] = 'g';
              }
              else if(el[j] != 'r' && el[j+1] != 'r'){
                el[j] = 'r';
                el[j+1] = 'r';
              }
              else{
                el[j] = 'b';
                el[j+1] = 'b';
              }
              q.push(el);
            }
          }
        }
      }
      if(!q.size() && flag != 2){
        cout << "NA" <<endl;
        break;
      }
      else if(flag == 2){
        cout << num <<endl;
        break;
      }
      ++num;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef pair<int,int> Pii;
typedef pair<string, int> Psi;
typedef vector<int> Vi;
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int bfs(string worm) {
  queue<Psi> q;
  q.push(Psi(worm, 0));
  map<string, int> m;

  while(!q.empty()) {
    Psi p = q.front(); q.pop();
    if(m[p.fr]) continue;
    bool flag = false;
    string s = p.fr;
    for(int i=0; i<s.size()-1; i++) {
      if(s[i] != s[i+1]) {
	flag = true; 
	break;
      }
    }

    if(!flag) return p.sc;
    m[p.fr] = p.sc;
    for(int i=0; i<s.size()-1; i++) {
      if(s[i] != s[i+1]) {
	string next = s;
	char c;
	if(s[i] != 'r' && s[i+1] != 'r') {
	  c = 'r';
	}
	else if(s[i] != 'g' && s[i+1] != 'g') {
	  c = 'g';
	}
	else if(s[i] != 'b' && s[i+1] != 'b') {
	  c = 'b';
	}
	next[i] = next[i+1] = c;
	q.push(Psi(next, p.sc+1));
      }
    }
  }
  return -1;
}

int main() {
  string s;
  while(cin>> s && s != "0") {
    int ans = bfs(s);
    if(ans >= 0) cout<< ans<< endl;
    else cout<< "NA"<< endl;
  }
}