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<string>
#include<queue>
#include<map>
using namespace std;
int main(){

  string s,col[3];
  queue<string> que;
  map<string,int> mp;
  int flag;

  while(1){
    cin >> s;
    if(s == "0") break;
    flag = 1;
    que.push(s);
    mp[s] = 0;
    for(int i=0;i<3;i++) col[i] = "";
    for(int i=0;i<s.size();i++){
      col[0] += "r";
      col[1] += "b";
      col[2] += "g";
    }
    while(!que.empty()){
      if(que.front()==col[0]){
	cout << mp[col[0]] << endl;
	flag--;
	break;
      }
      if(que.front()==col[1]){
	cout << mp[col[1]] << endl;
	flag--;
	break;
      }
      if(que.front()==col[2]){
	cout << mp[col[2]] << endl;
	flag--;
	break;
      }
      for(int i=0;i<s.size()-1;i++){
	if(que.front()[i] != que.front()[i+1]){
	  s = que.front();
	  if(s[i] != 'r' && s[i+1] != 'r'){
	    s[i] = 'r';
	    s[i+1] = 'r';
	  } else if(s[i] != 'b' && s[i+1] != 'b'){
	    s[i] = 'b';
	    s[i+1] = 'b';
	  }else if(s[i] != 'g' && s[i+1] != 'g'){
	    s[i] = 'g';
	    s[i+1] = 'g';
	  }
	  if(mp.find(s) == mp.end()){
	    que.push(s);
	    mp[s] = mp[que.front()]+1;
	  }
	}
      }
      que.pop();
    }
    while(!que.empty()) que.pop();
    if(flag) cout << "NA" << endl;
  }
  return (0);
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<ll, string> PLS;

int ans;
string s;

map<string, int> msi;

void solve(){
	msi[s] = 0;

	queue<string> que;

	que.push(s);

	while (que.size()){
		string ret = que.front();
		que.pop();

		bool f = 0;
		REP(i, ret.size() - 1){
			if (ret[i] != ret[i + 1])break;
			if (i == ret.size() - 2)f = 1;
		}
		if (f){
			ans = min(ans, msi[ret]);
		}

		REP(i, ret.size() - 1){
			string t = ret;
			if (ret[i] != ret[i + 1]){
				if (ret[i] == 'r'&&ret[i + 1] == 'g'){
					t[i] = 'b';
					t[i + 1] = 'b';
				}
				if (ret[i] == 'g'&&ret[i + 1] == 'r'){
					t[i] = 'b';
					t[i + 1] = 'b';
				}
				if (ret[i] == 'r'&&ret[i + 1] == 'b'){
					t[i] = 'g';
					t[i + 1] = 'g';
				}
				if (ret[i] == 'b'&&ret[i + 1] == 'r'){
					t[i] = 'g';
					t[i + 1] = 'g';
				}
				if (ret[i] == 'b'&&ret[i + 1] == 'g'){
					t[i] = 'r';
					t[i + 1] = 'r';
				}
				if (ret[i] == 'g'&&ret[i + 1] == 'b'){
					t[i] = 'r';
					t[i + 1] = 'r';
				}
				if (t == ret)continue;
				if (msi.find(t) == msi.end()){
					//cout << t << endl;
					que.push(t);
				}
				msi[t] = msi[ret] + 1;
			}
		}
	}
}

int main(){
	while (cin >> s&&s != "0"){
		ans = INF;
		msi.clear();
		solve();
		if (ans == INF)cout << "NA" << endl;
		else cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

bool check(string& str)
{
	for (int i = 1; i < str.size(); i++){
		if (str[i] != str[0]) return false;
	}
	return true;
}

int main()
{
	string str;
	while (cin >> str, str != "0"){
		queue<pair<string, int> > que;
		que.push({str, 0});
		set<string> done;
		int res = -1;
		while (!que.empty()){
			pair<string, int> p = que.front(); que.pop();
			//cout << p.first << endl;
			if (check(p.first)){
				res = p.second;
				break;
			}
			if (done.count(p.first)) continue;
			done.insert(p.first);
			
			for (int i = 1; i < p.first.size(); i++){
				if (p.first[i] != p.first[i - 1]){
					int cnt[256] = {0};
					cnt[p.first[i]]++;
					cnt[p.first[i - 1]]++;
					
					string tmp = p.first;
					if (!cnt['r']){
						tmp[i] = tmp[i - 1] = 'r';
					}
					else if (!cnt['g']){
						tmp[i] = tmp[i - 1] = 'g';
					}
					else {
						tmp[i] = tmp[i - 1] = 'b';
					}
					
					que.push({tmp, p.second + 1});
				}
			}
		}
		if (res == -1) puts("NA");
		else printf("%d\n", res);
	}
	
	return 0;
}