Language: C++

#include <cstdio>
#include <queue>
#include <map>

using namespace std;

int main()
{
	char str[256];
	char col[3] = "rgb";
	
	while (scanf("%s", str) && str[0] !='0'){
		map<string, int> minTime;
		queue<string> q;
		string s = str;
		
		minTime[s] = 0;
		
		bool ng = true;
		for (q.push(s); !q.empty(); q.pop()){
			string look = q.front();
			
			bool flag = true;
			for (int i = 0; i < look.size() - 1; i++){
				if (look[i + 1] != look[i]) flag = false;
			}
			
			if (flag){
				printf("%d\n", minTime[look]);
				ng = false;
				break;
			}
			
			for (int c = 0; c < 3; c++){
				for (int i = 0; i < look.size() - 1; i++){
					if (look[i + 1] != col[c] && look[i] != col[c] && look[i + 1] != look[i]){
						string next = look;
						next[i + 1] = next[i] = col[c];
						if (minTime.count(next) > 0) continue;
						minTime[next] = minTime[look] + 1;
						q.push(next);
					}
				}
			}
		}
		if (ng) printf("NA\n");
	}
	
	return (0);
}

Compiler output:
a.cc: In function 'int main()':
a.cc:10:23: error: initializer-string for 'char [3]' is too long [-fpermissive]
   10 |         char col[3] = "rgb";
      |                       ^~~~~
a.cc:13:21: error: 'string' was not declared in this scope
   13 |                 map<string, int> minTime;
      |                     ^~~~~~
a.cc:4:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
    3 | #include <map>
  +++ |+#include <string>
    4 | 
a.cc:13:32: error: template argument 1 is invalid
   13 |                 map<string, int> minTime;
      |                                ^
a.cc:13:32: error: template argument 3 is invalid
a.cc:13:32: error: template argument 4 is invalid
a.cc:14:29: error: template argument 2 is invalid
   14 |                 queue<string> q;
      |                             ^
a.cc:15:23: error: expected ';' before 's'
   15 |                 string s = str;
      |                       ^~
      |                       ;
a.cc:17:25: error: 's' was not declared in this scope
   17 |                 minTime[s] = 0;
      |                         ^
a.cc:20:24: error: request for member 'push' in 'q', which is of non-class type 'int'
   20 |                 for (q.push(s); !q.empty(); q.pop()){
      |                        ^~~~
a.cc:20:36: error: request for member 'empty' in 'q', which is of non-class type 'int'
   20 |                 for (q.push(s); !q.empty(); q.pop()){
      |                                    ^~~~~
a.cc:20:47: error: request for member 'pop' in 'q', which is of non-class type 'int'
   20 |                 for (q.push(s); !q.empty(); q.pop()){
      |                                               ^~~
a.cc:21:31: error: expected ';' before 'look'
   21 |                         string look = q.front();
      |                               ^~~~~
      |                               ;
a.cc:24:45: error: 'look' was not declared in this scope
   24 |                         for (int i = 0; i < look.size() - 1; i++){
      |                                             ^~~~
a.cc:29:56: error: 'look' was not declared in this scope
   29 |                                 printf("%d\n", minTime[look]);
      |                                                        ^~~~
a.cc:35:53: error: 'look' was not declared in this scope
   35 |                                 for (int i = 0; i < look.size() - 1; i++){
      |                                                     ^~~~
a.cc:37:55: error: expected ';' before 'next'
   37 |                                                 string next = look;
      |                                                       ^~~~~
      |                                                       ;
a.cc:38:53: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   38 |                                                 next[i + 1] = next[i] = col[c];
      |                                                     ^
a.cc:38:67: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   38 |                                                 next[i + 1] = next[i] = col[c];
      |                                                                   ^
a.cc:39:61: error: request for member 'count' in 'minTime', which is of non-class type 'int'
   39 |                                                 if (minTime.count(next) > 0) continue;
      |                                                             ^~~~~
a.cc:40:56: error: invalid types 'int[<unresolved overloaded function type>]' for array subscript
   40 |                                                 minTime[next] = minTime[look] + 1;
      |                                                        ^
a.cc:41:51: error: request for member 'push' in 'q', which is of non-class type 'int'
   41 |                                                 q.push(next);
      |                                                   ^~~~