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 <algorithm>
using namespace std;


string rgb = "rgb";
int tab[300];
int n;

int encode(string s){
  int ret = 0;
  for(int i=0; i<n; i++){
    ret *= 3;
    ret += tab[s[i]];
  }
  return ret;
}

string decode(int c){
  string s = "";
  for(int i=0; i<n; i++){
    s = rgb[c%3] + s;
    c /= 3;
  }
  return s;
}


int main(){
  int q[100000];
  int memo[100000];
  tab['r'] = 0;
  tab['g'] = 1;
  tab['b'] = 2;
  
  for(;;){
    for(int i=0; i<100000; i++){
      memo[i] = -1;
    }
    string s;
    cin >> s;
    if(s[0]=='0') break;
    n = s.length();
    
    int ql = 0;
    int qr = 1;
    q[0] = encode(s);
    memo[encode(s)] = 0;
    
    int found = 0;
    while(qr-ql>0){
      int c = q[ql];
      string s = decode(c);
//      cout << s << endl;
      int ok = 1;
      for(int i=0; i<n-1; i++){
        if(s[i]!=s[i+1]){
          ok = 0;
          break;
        }
      }
      if(ok){
        found = 1;
        cout << memo[c] << endl;
        break;
      }
      
      for(int i=0; i<n-1; i++){
        if(s[i]!=s[i+1]){
          string ns = s;
          if('r'!=s[i] && 'r'!=s[i+1]){
            ns[i] = 'r';
            ns[i+1] = 'r';
          }
          if('g'!=s[i] && 'g'!=s[i+1]){
            ns[i] = 'g';
            ns[i+1] = 'g';
          }
          if('b'!=s[i] && 'b'!=s[i+1]){
            ns[i] = 'b';
            ns[i+1] = 'b';
          }
          int nc = encode(ns);
          if(memo[nc]==-1){
            memo[nc] = memo[c]+1;
            q[qr] = nc;
            qr++;
          }
        }
      }
      
      ql++;
      
    }
    if(found==0){
      cout << "NA" << endl;
    }
  }
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <set>
#include <queue>
#include <stack>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<string, int>;
using edge = struct
{
    int to;
    int cost;
};

int wfs(string worm)
{
    queue<P> que;
    que.push(P(worm, 0));

    map<string, int> mp;

    while (!que.empty())
    {
        P p = que.front();
        que.pop();

        string tmp = p.first;

        //if(mp[p.first]) continue;

        bool flag = true;

        for (int i = 0; i < tmp.size() - 1; i++)
        {
            if (tmp[i] != tmp[i + 1])
            {
                flag = false;
                break;
            }
        }

        if (flag) return p.second;

        //mp[p.first] = p.second;

        for(int i = 0; i < tmp.size() - 1; i++)
        {
            if(tmp[i] == tmp[i + 1]) continue;

            string nworm = tmp;

            if(tmp[i] != 'g' && tmp[i + 1] != 'g')
            {
                nworm[i] = 'g';
                nworm[i + 1] = 'g';
            }
            else if(tmp[i] != 'b' && tmp[i + 1] != 'b')
            {
                nworm[i] = 'b';
                nworm[i + 1] = 'b';
            }
            else
            {
                nworm[i] = 'r';
                nworm[i + 1] = 'r';
            }

            que.push(P(nworm, p.second + 1));

        }        
    }
    return -1;
}

int main()
{
    string input;

    while(cin >> input && input != "0")
    {
        cout << (wfs(input) ? to_string(wfs(input)) : "NA") << endl;
    }

    return 0;
}