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 <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;
}

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

using namespace std;

#define fst first
#define scd second
#define pb push_back
#define mp make_pair
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define rep(i,x) for(int i=0;i<(int)x;++i)
#define rep1(i,x) for(int i=1;i<=(int)x;++i)
#define rrep(i,x) for(int i=(int)x-1;i>=0;--i)
#define rrep1(i,x) for(int i=(int)x;i>=1;--i)
#define repf(i,a,x) for(int i=a;i<(int)x;++i)
#define repf1(i,a,x) for(int i=a;i<=(int)x;++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

template<typename T> T get(){T a;cin >> a;return a;}
template<typename T> T rev(T & a){reverse(all(a));return a;}

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int inf = 1e9;
const ll linf = 1e18;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    string s;
    while (cin >> s, s[0] != '0') {
        map<string, bool> m;

        queue<string> q, r;

        q.push(s);

        int cnt = 0;

        while (q.size()) {
            while (q.size()) {
                string t = q.front(); q.pop();
                if (m[t]) continue;
                m[t] = true;
                bool f = true;
                rep(i, t.size() - 1) {
                    if ((t[i] == 'r' && t[i + 1] == 'b') || (t[i] == 'b' && t[i + 1] == 'r')) {
                        char c = t[i], d = t[i + 1];
                        t[i] = t[i + 1] = 'g';
                        r.push(t);
                        t[i] = c, t[i + 1] = d;
                        f = false;
                    } else if ((t[i] == 'b' && t[i + 1] == 'g') || (t[i] == 'g' && t[i + 1] == 'b')) {
                        char c = t[i], d = t[i + 1];
                        t[i] = t[i + 1] = 'r';
                        r.push(t);
                        t[i] = c, t[i + 1] = d;
                        f = false;
                    } else if ((t[i] == 'r' && t[i + 1] == 'g') || (t[i] == 'g' && t[i + 1] == 'r')) {
                        char c = t[i], d = t[i + 1];
                        t[i] = t[i + 1] = 'b';
                        r.push(t);
                        t[i] = c, t[i + 1] = d;
                        f = false;
                    }
                }
                if (f) {
                    cout << cnt << endl;
                    goto nxt;
                }
            }
            while (r.size()) {
                string t = r.front(); r.pop();
                q.push(t);
            }
            cnt++;
        }

        cout << "NA\n";
nxt:;
    }
}