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"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

map<vector<int>, int>mp;

map<char, int>chmp;
int getans(vector<int> st,int m) {
	if (mp.find(st) != mp.end())return  mp[st];
	else {
		if (m >= 1000)return 1e8;
		if (all_of(st.begin(), st.end(), [=](const int n) {return n == st[0]; }))return mp[st] = 0;
		mp[st] = 1e8;
		int ans = 1e8;
		for (int i = 0; i < st.size() - 1; ++i) {
			if (st[i] != st[i + 1]) {
				vector<int>nowst(st);
				nowst[i] = 3 - st[i] - st[i + 1];
				nowst[i + 1] = 3 - st[i] - st[i + 1];
				ans = min(ans, getans(nowst,m+1) + 1);
			}
		}
		return mp[st] = ans;
	}
}

int main() {
	chmp['r'] = 0;
	chmp['g'] = 1;
	chmp['b'] = 2;
	while (1) {

		string st; cin >> st;
		if (st == "0")break;
		vector<int>nums;
		for (auto c : st) {
			nums.emplace_back(chmp[c]);
		}
		int ans = getans(nums,0);
		if (ans > 1e7) {
			cout << "NA" << endl;
		}
		else {
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<map>
using namespace std;
string S;
char f(char a,char b)
{
	if(a=='r'&&b=='b')return 'g';
	else if(a=='r'&&b=='g')return 'b';
	else if(a=='b'&&b=='r')return 'g';
	else if(a=='b'&&b=='g')return 'r';
	else if(a=='g'&&b=='r')return 'b';
	else return 'r';
}
main()
{
	while(cin>>S,S!="0")
	{
		queue<pair<int,string> >P;
		P.push(make_pair(0,S));
		map<string,bool>M;
		M[S]=1;
		bool flag=1;
		while(!P.empty())
		{
			int u=P.front().first;
			string s=P.front().second;
			P.pop();
			bool F=0;
			for(int i=0;i<s.size()-1;i++)
			{
				if(s[i]==s[i+1])continue;
				F=1;
				string t=s;
				t[i]=t[i+1]=f(s[i],s[i+1]);
				if(!M[t])
				{
					M[t]=1;
					P.push(make_pair(u+1,t));
				}
			}
			if(!F)
			{
				cout<<u<<endl;
				flag=0;
				break;
			}
		}
		if(flag)cout<<"NA"<<endl;
	}
}

C++: #include <iostream>
#include <queue>
#include <map>

using namespace std;

int solve(string s)
{
    map<string, int> mem;
    queue<pair<string, int> > q;

    q.push(pair<string, int>(s, 0));
    mem[s] = 0;

    while (!q.empty()) {
        pair<string, int> tmp;
        bool flag = true;

        tmp = q.front();
        q.pop();

        // Check
        for (int i = 1; i < tmp.first.size(); i++)
            if (tmp.first[0] != tmp.first[i]) {
                flag = false;
                break;
            }
        if (flag) 
            return tmp.second;

        for (int i = 0; i < tmp.first.size() - 1; i++) {
            if (tmp.first[i] != tmp.first[i + 1]) {
                string str = tmp.first;
                int num = (int)(tmp.first[i] + tmp.first[i + 1]);

                if (num == 217) 
                    str[i] = str[i + 1] = 'b';
                else if (num == 212) 
                    str[i] = str[i + 1] = 'g';
                else if (num == 201) 
                    str[i] = str[i + 1] = 'r';

                if (mem.count(str) == 0) {
                    q.push(pair<string, int>(str, tmp.second + 1));
                    mem[str] = tmp.second + 1;
                }
            }
        }
    }
    return -1;
}

int main()
{
    string inp;

    while (cin >> inp, inp != "0") {
        int num = solve(inp);

        if (num >= 0)
            cout << num << endl;
        else 
            cout << "NA" << endl;
    }

    return 0;
}