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>
#include<queue>
#include<vector>
#include<cstring>
char p[3][12]={
    "rrrrrrrrrr",
    "bbbbbbbbbb",
    "gggggggggg"
};
typedef struct worm{
    char b[12];
    int s;
}worm;
using namespace std;
int main(){
    char a[15];
    while(cin>>a,a[0]!='0'){
        int len=strlen(a);
        queue<worm>Q;
        worm o;
        strcpy(o.b,a);
        o.s=0;
        Q.push(o);
        bool f=true;
        while(Q.size()){
            worm gar=Q.front();
            Q.pop();
            bool f2=false;
            for(int i=0;i<3;i++)if(!strncmp(gar.b,p[i],len))f2=true;
            if(f2){
                cout<<gar.s<<endl;
                f=false;
                break;
            }
            if(gar.s>9)break;
            for(int i=0;i<len-1;i++){
                char gar2[12];
                strcpy(gar2,gar.b);

                if(gar2[i]!=gar2[i+1]){
                    if(gar2[i]!='g'&&gar2[i+1]!='g')strncpy(gar2+i,p[2],2);
                    else if(gar2[i]!='r'&&gar2[i+1]!='r')strncpy(gar2+i,p[0],2);
                    else if(gar2[i]!='b'&&gar2[i+1]!='b')strncpy(gar2+i,p[1],2);
                }
                if(strcmp(gar.b,gar2)){
                    worm gar3;
                    strcpy(gar3.b,gar2);
                    gar3.s=gar.s+1;
                    Q.push(gar3);
                }
            }

        }
        if(f)cout<<"NA"<<endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,string> P;
int main(){
  string s1;
  int i;
  while(1){
    cin >> s1;
    if(s1=="0"){
      return 0;
    }
    int ans=(1<<29);
    map<string,int> mp;
    queue<P> que;
    que.push(P(0,s1));
    while(!que.empty()){
      int x=que.front().first;
      string s=que.front().second;
      que.pop();
      if(mp.find(s)!=mp.end() && mp[s]<=x){
        continue;
      }
      if(x>=ans){
        continue;
      }
      mp[s]=x;
      int res=0;
      for(i=1;i<s.size();i++){
        if(s[i-1]!=s[i]){
          res=1;
          break;
        }
      }
      if(res==0){
        ans=x;
        continue;
      }
      for(i=1;i<s.size();i++){
        string t=s;
        if(s[i-1]=='r' && s[i]=='g'){
          t[i-1]='b',t[i]='b';
          que.push(P(x+1,t));
        }
        if(s[i-1]=='r' && s[i]=='b'){
          t[i-1]='g',t[i]='g';
          que.push(P(x+1,t));
        }
        if(s[i-1]=='g' && s[i]=='b'){
          t[i-1]='r',t[i]='r';
          que.push(P(x+1,t));
        }
        if(s[i-1]=='g' && s[i]=='r'){
          t[i-1]='b',t[i]='b';
          que.push(P(x+1,t));
        }
        if(s[i-1]=='b' && s[i]=='g'){
          t[i-1]='r',t[i]='r';
          que.push(P(x+1,t));
        }
        if(s[i-1]=='b' && s[i]=='r'){
          t[i-1]='g',t[i]='g';
          que.push(P(x+1,t));
        }
      }
    }
    if(ans==(1<<29)){
      cout << "NA" << endl;
      continue;
    }
    cout << ans << endl;
  }
}