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 <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int,int> pii;

bool isAns(string s){
  int len = s.length();
  FOR(i,1,len){
    if(s[0] != s[i]) return false;
  }
  return true;
}

char diffColor(char a, char b){
  if((a == 'r' && b == 'g') || (a == 'g' && b == 'r')){
    return 'b';
  }else if((a == 'g' && b == 'b') || (a == 'b' && b == 'g')){
    return 'r';
  }else{
    return 'g';
  }
}

int main(){

  while(true){
    string s;
    cin >> s;
    if(s == "0") break;

    map<string, int> mp;
    mp[s] = 1;
    queue<string> que;

    que.push(s);
    bool found = false;
    while(!que.empty()){
      s = que.front();
      que.pop();

      if(isAns(s)){
        found = true;
        cout << mp[s] - 1 << "\n";
        break;
      }

      int len = s.length();
      REP(i,len-1){
        if(s[i] != s[i+1]){
          string v = s;
          char tmp = diffColor(s[i], s[i+1]);
          v[i] = tmp; v[i+1] = tmp;
          if(mp[v] == 0){
            mp[v] = mp[s] + 1;
            que.push(v);
          }
        }
      }
    }
    if(!found){
      cout << "Na\n";
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

string col = "rgb";

int main() {
  
  string worm;
  while(cin >> worm) {
    if(worm=="0") break;
    int const wsize = worm.size();
    set<string> used;
    queue<pair<int, string> > q;
    q.push(make_pair(0, worm));
    bool flg = false;
    while(!q.empty()) {
      int cost = q.front().first; worm = q.front().second; q.pop();
      if(used.count(worm)) continue;
      used.insert(worm);
      bool check = true;
      for(int i=0; i<wsize-1; i++) check = check && worm[i] == worm[i+1];
      if(check) {
        flg = true;
        cout << cost << endl;
        break;
      }
      for(int i=0; i<wsize-1; i++) {
        if(worm[i]!=worm[i+1]) {
          char next;
          for(int j=0; j<3; j++) {
            if(worm[i] == col[j]) continue;
            if(worm[i+1] == col[j]) continue;
            next = col[j];
          }
          string nworm = worm;
          nworm[i] = nworm[i+1] = next;
          if(used.count(nworm)) continue;
          q.push(make_pair(cost+1, nworm));
        }
      }
    }
    if(!flg) cout << "NA" << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <string>
#include <queue>
using namespace std;

int main()
{
  string s;
  while (cin >> s) {
    if (s == "0")
      break;

    queue<pair<int, string> > que;
    que.push(make_pair(0, s));
    set<string> memo;
    int ans = -1;
    while (!que.empty()) {
      const pair<int, string> p = que.front();
      que.pop();

      if (p.second.find_first_of("rg") == string::npos ||
	  p.second.find_first_of("rb") == string::npos ||
	  p.second.find_first_of("gb") == string::npos) {
	ans = p.first;
	break;
      }

      if (memo.find(p.second) != memo.end())
	continue;
      memo.insert(p.second);

      for (int i = 0; i < p.second.size()-1; ++i) {
	if (p.second[i] != p.second[i+1]) {
	  s = p.second;
	  if (s[i] == 'r' && s[i+1] == 'g' || s[i] == 'g' && s[i+1] == 'r')
	    s[i] = s[i+1] = 'b';
	  else if (s[i] == 'r' && s[i+1] == 'b' || s[i] == 'b' && s[i+1] == 'r')
	    s[i] = s[i+1] = 'g';
	  else if (s[i] == 'g' && s[i+1] == 'b' || s[i] == 'b' && s[i+1] == 'g')
	    s[i] = s[i+1] = 'r';

	  if (memo.find(s) == memo.end())
	    que.push(make_pair(p.first+1, s));
	}
      }

    }

    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}