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 TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    cums(T& v){ T r; r.push_back(v[0]); for(int i=1; i<v.size(); i++) r.push_back(r[i-1] + v[i]); return r; }
TEMP_T void maxe(T& v, T m){ v = max(v, m); }
TEMP_T void mine(T& v, T m){ v = min(v, m); }

static inline int in(){ int x; scanf("%d", &x); return x; }

struct Solver {
  Solver(const string& init) : init(init) {}
  char getAnotherColor(char a, char b){
    if (a > b) swap(a, b);
    if (a == 'b'){
      if (b == 'g') return 'r';
      return 'g';
    }
    return 'b';
  }
  bool is_end(const string& s){
    for (int i = 1; i < s.size(); i++){
      if (s[0] != s[i]) return false;
    }
    return true;
  }

  int solve(){
    queue<string> Q;
    set<string> vis;
    int turn = 0;
    
    Q.push(init);
    vis.insert(init);
    while (!Q.empty()){
      queue<string> nQ;
      while (!Q.empty()){
        string state = Q.front();
        Q.pop();
        if (is_end(state)){
          return turn;
        }
        for (int i = 0; i < state.size() - 1; i++){
          if (state[i] != state[i + 1]){
            char another = getAnotherColor(state[i], state[i + 1]);
            string next_state = state;
            next_state[i + 1] = next_state[i] = another;
            if (vis.find(next_state) != vis.end()) continue;
            vis.insert(next_state);
            nQ.push(next_state);
          }
        }
      }
      turn++;
      swap(Q, nQ);
    }
    return -1;
  }
  string init;
};

int main()
{
  string s;
  while (cin >> s, s != "0"){
    Solver solver(s);
    int turn = solver.solve();
    if (turn == -1) puts("NA");
    else printf("%d\n", turn);
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;

char z[3]={'r','b','g'};

int Unzip(string s){
  int ans=0;
  for(int i=0;i<s.size();i++){
    ans*=3;
    for(int j=0;j<3;j++){
      if(s[i]==z[j])ans+=j;
    }
  }
  return ans;
}
char Other(char a,char b){
  for(int i=0;i<3;i++){
    if(z[i]!=a&&z[i]!=b)return z[i];
  }
  return '#';
}

int dis[60000];
queue<pair<string,int> >Q;
int main(){
  string s;
  while(cin>>s&&s!="0"){
    lol(i,60000)dis[i]=mod;
    Q.push(make_pair(s,0));
    while(!Q.empty()){
      string x=Q.front().first;
      int len=Q.front().second;
      Q.pop();
      int index=Unzip(x);
      if(dis[index]<=len)continue;
      dis[index]=len;
      for(int i=0;i<x.size()-1;i++){
	if(x[i]!=x[i+1]){
	  string news=x;
	  news[i]=Other(x[i],x[i+1]);
	  news[i+1]=Other(x[i],x[i+1]);
	  Q.push(make_pair(news,len+1));
	}
      }
    }
    int ans=mod;
    for(int i=0;i<3;i++){
      for(int j=0;j<s.size();j++)s[j]=z[i];
      int index=Unzip(s);
      ans=min(ans,dis[index]);
    }
    if(ans==mod)cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

string S;
map<string, bool> seen;

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> S) {
    if (S == "0") break;
    int N = S.length();
    rep(i, N) {
      if (S[i] == 'r') S[i] = 1;
      else if (S[i] == 'g') S[i] = 2;
      else S[i] = 3;
    }
    queue< pair<string, int> > q;
    seen.clear();
    seen[S] = true;
    q.push(make_pair(S, 0));

    bool done = false;
    while (q.size()) {
      string s = q.front()._1;
      int r = q.front()._2;
      q.pop();
      if (r > 20) break;

      bool ok = true;
      rep(i, N) if (s[i] != s[0]) ok = false;
      if (ok) {
        done = true;
        cout << r << "\n";
        break;
      }
      rep(i, N-1) {
        if (s[i] == s[i+1]) continue;
        string t(s);
        t[i] = t[i+1] = s[i]^s[i+1];
        if (seen[t]) continue;
        seen[t] = true;
        q.push(make_pair(t, r+1));
      }
    }
    if (!done) {
      cout << "NA\n";
    }
  }
  return 0;
}