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>
using namespace std;
typedef long long ll;

typedef pair<string, int> P;

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

	string s;
	while(cin >> s, s != "0") {
		int n = s.size();
		string R = string(n, 'r');
		string B = string(n, 'b');
		string G = string(n, 'g');

		map<string, int> m;
		m[s] = 0;
		queue<P> q;
		q.push({ s, 0 });
		while(q.size()) {
			s = q.front().first;
			int d = q.front().second;
			q.pop();
			if(m[s] < d) continue;
			//cout << s << endl;
			bool flag = false;
			for(int i = 0; i < n - 1; i++) {
				string ns = s;
				if(s[i] == 'r' && s[i + 1] == 'b' || s[i] == 'b' && s[i + 1] == 'r') ns[i] = ns[i + 1] = 'g';
				else if(s[i] == 'g' && s[i + 1] == 'b' || s[i] == 'b' && s[i + 1] == 'g') ns[i] = ns[i + 1] = 'r';
				else if(s[i] == 'r' && s[i + 1] == 'g' || s[i] == 'g' && s[i + 1] == 'r') ns[i] = ns[i + 1] = 'b';
				if(ns != s && m.count(ns) == 0) {
					m[ns] = d + 1;
					q.push({ ns, d + 1 });
					if(ns == R || ns == B || ns == G) { flag = true; break; }
				}
			}
			if(flag) break;
		}

		int ans = 1e9;
		if(m.count(R)) ans = min(ans, m[R]);
		if(m.count(B)) ans = min(ans, m[B]);
		if(m.count(G)) ans = min(ans, m[G]);
		if(ans != 1e9) {
			cout << ans << endl;
		}
		else {
			cout << "NA" << endl;
		}
	}
}

C++: #include <iostream>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cstdlib>
#include <cassert>
using namespace std;

char rgb(char a, char b) {
    assert(a != b);
    if (a == 'r') {
        if (b == 'g') return 'b';
        else if (b == 'b') return 'g';
    } else if (a == 'g') {
        if (b == 'r') return 'b';
        else if (b = 'b') return 'r';
    } else {
        if (b == 'r') return 'g';
        else if (b == 'g') return 'r';
    }
    exit(1);
}

struct state_t {
    string worm;
    int count;
    state_t(string worm, int count) : worm(worm), count(count) {}
};

bool same_characters(string s) {
    assert(!s.empty());
    char p = s[0];
    for (int i = 0; i < s.size(); i++) {
        if (p != s[i]) return false;
    }
    return true;
}

int main() {
    string s;
    while (getline(cin, s)) {
        if (s == "0") break;

        queue<state_t> q;
        set<string> cache;

        q.push(state_t(s, 0));

        bool found = false;
        while (!q.empty()) {
            state_t p = q.front(); q.pop();
            string u = p.worm;
//            cout << u << endl;
            if (cache.count(u)) continue;
            cache.insert(u);
            if (same_characters(u)) {
                cout << p.count << endl;
                found = true;
                break;
            }
            for (int i = 0; i < u.size()-1; i++) {
                if (!(u[i] == u[i+1])) {
                    char c = rgb(u[i], u[i+1]);
                    string t = u; t[i] = t[i+1] = c;
                    q.push(state_t(t, p.count+1));
                }
            }
        }

        if (!found) cout << "NA" << endl;

//        for (set<string>::iterator si = cache.begin();
//                si != cache.end(); si++) {
//            cout << *si << ' ';
//        }
//        cout << endl;
    }

    return 0;
}