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 <vector>
#include <set>
#include <queue>
#include <map>

using namespace std;

typedef pair<string, int> psi;

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

char change_color(char a, char b) {
    char c;
    if (a == 'r') {
        if (b == 'g') c = 'b';
        else c = 'g';
    }
    if (a == 'g') {
        if (b == 'r') c = 'b';
        else c = 'r';
    }
    if (a == 'b') {
        if (b == 'r') c = 'g';
        else c = 'r';
    }
    return c;
}

int solve(string worm) {
    int ans = -1;
    map<string, int> memo;
    queue<psi> Q;

    Q.push(make_pair(worm, 0));
    while(!Q.empty()) {
        psi now = Q.front(); Q.pop();
        if (check(now.first)) {
            return now.second;
        }
        if (memo[now.first]) continue;
        memo[now.first] = now.second;

        for (int i = 1; i < now.first.size(); i++) {
            if (now.first[i] == now.first[i-1]) continue;
            char color = change_color(now.first[i-1], now.first[i]);
            string next = now.first;
            next[i-1] = next[i] = color;

            Q.push(make_pair(next, now.second + 1));
        }
    }
    return ans;
}


int main() {
    string worm;
    while(cin >> worm) {
        if (worm == "0") break;
        int ans = solve(worm);
        if (ans == -1)
            cout << "NA" << endl;
        else
            cout << ans << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

string S;
map<string, bool> seen;

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> S) {
    if (S == "0") break;
    int N = S.length();
    rep(i, N) {
      if (S[i] == 'r') S[i] = 1;
      else if (S[i] == 'g') S[i] = 2;
      else S[i] = 3;
    }
    queue< pair<string, int> > q;
    seen.clear();
    seen[S] = true;
    q.push(make_pair(S, 0));

    while (q.size()) {
      string s = q.front()._1;
      int r = q.front()._2;
      q.pop();

      bool ok = true;
      rep(i, N) if (s[i] != s[0]) ok = false;
      if (ok) {
        cout << r << "\n";
        break;
      }
      if (r > 20) {
        cout << "NA\n";
        break;
      }
      rep(i, N-1) {
        if (s[i] == s[i+1]) continue;
        string t(s);
        t[i] = t[i+1] = s[i]^s[i+1];
        if (seen[t]) continue;
        seen[t] = true;
        q.push(make_pair(t, r+1));
      }
    }
  }
  return 0;
}

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

//#define int long long
#define DEBUG 0
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) if(DEBUG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DEBUG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
using pii = pair<int, int>; using vi = vector<int>;
static const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
static const int MOD = (int)(1e9 + 7);

bool f(string s) {
	rep(i, 0, s.size() - 1)
		if (s[i] != s[i + 1])return false;
	return true;
}

signed main() {
	for (string s; cin >> s&&s != "0";) {
		map<string, int> m;
		m[s] = 0;
		queue<string> q;
		q.emplace(s);
		while (!q.empty()) {
			string t = q.front(); q.pop();
			rep(i, 0, t.size() - 1) {
				if (t[i] == t[i + 1])continue;
				string tmp = t;
				if (t[i] + t[i + 1] == 'r' + 'g')tmp[i] = tmp[i + 1] = 'b';
				if (t[i] + t[i + 1] == 'g' + 'b')tmp[i] = tmp[i + 1] = 'r';
				if (t[i] + t[i + 1] == 'b' + 'r')tmp[i] = tmp[i + 1] = 'g';

				if (m.find(tmp) == m.end()) {
					q.emplace(tmp);
					m[tmp] = m[t] + 1;
				}
			}
		}

		int ans = INF;
		for (auto itr = m.begin(); itr != m.end(); itr++) {
			if (f(itr->first))ans=min(ans,itr->second);
		}
		if (ans == INF)cout << "NA" << endl;
		else cout << ans << endl;
	}
	return 0;
}