Language: C++

import java.util.*;
public class Main{
    static Scanner kbd = new Scanner(System.in);
    public static void main(String[] args){
	while(kbd.hasNext()){
	    String s = kbd.nextLine();
	    if(!s.equals("0")) solve(s);
	}
    }

    static void solve(String s){
	//String[] pattern = new String[60000];
	HashSet<String> set = new HashSet<String>();
	int pcnt = 0;
	Stack stack = new Stack(s);
	String a = s;
	set.add(s);
	//pattern[pcnt++] = a;
	int t = 0;
	while(!stack.isEmpty() && !color(a)){
	    //System.out.println(a+" "+color(a));
	    Worm w = (Worm)stack.pop();
	    a = w.getbody();
	    t = w.getTime();
	    for(int i=0; i<a.length()-1; i++){
		if(!a.substring(i, i+1).equals(a.substring(i+1, i+2))){
		    String b = change(a, i, i+1);  
		    /*
		    int j=0;
		    while(j<pcnt && !pattern[j].equals(b)) j++;
		    if(pattern[j]==null){
			//System.out.println(b);
			pattern[j] = b;
			stack.in(b, t+1);
			pcnt++;
		    }
		    */
		    if(set.contains(b)) continue;
		    else{
			stack.in(b, t+1);
			set.add(b);
		    }
		}
	    }
	}
	if(color(a))
	    System.out.println(t);
	else
	    System.out.println("NA");
    }

    static boolean color(String x){
	String a = x.substring(0, 1);
	boolean ans = true;
	for(int i=0; i<x.length(); i++){
	    String y = x.substring(i, i+1);
	    ans = ans && a.equals(y);
	}
	return ans;
    }

    static String change(String x, int s, int e){
	String ans = "";
	for(int i=0; i<x.length(); i++){
	    if(i==s){
		String a = x.substring(i, i+1);
		i++;
		String b = x.substring(i, i+1);
		a = select(a, b);
		ans += a+a;
	    }
	    else{
		ans += x.substring(i, i+1);
	    }
	}
	return ans;
    }
	
    static String select(String x, String y){
	if(x.equals("r") || y.equals("r")){
	    if(y.equals("b") || x.equals("b")){
		return "g";
	    }
	    else return "b";
	}
	else return "r";
    }
}

class Stack {
    Object[] stack = new Object[60000];
    int sp;
    int out;
    public Stack(String s){
	sp = 0;
	out = 0;
	this.in(s, 0);
    }

    public  void in(String s, int t){
	Worm w = new Worm(s, t);
	stack[sp++] = w;
    }
    public Object pop(){
	return stack[out++];
    }
    /*
    public String getb(){
	Worm a = (Worm)stack[sp-1];
	return a.getbody();
    }
    public  int gett(){
	Worm a = (Worm)stack[sp-1];
	sp--;
	return a.getTime();
    }
    */
    public boolean isEmpty(){
	return sp==out;
    }
}

class Worm{
    String body;
    int time;
    public  Worm(String s, int t){
	body = s;
	time = t;
    }
    public  String getbody(){
	return body;
    }
    public int getTime(){
	return time;
    }
}

Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.util.*;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:1: error: expected unqualified-id before 'public'
    2 | public class Main{
      | ^~~~~~
a.cc:91:5: error: 'Object' does not name a type
   91 |     Object[] stack = new Object[60000];
      |     ^~~~~~
a.cc:94:11: error: expected ':' before 'Stack'
   94 |     public Stack(String s){
      |           ^~~~~~
      |           :
a.cc:94:24: error: expected ')' before 's'
   94 |     public Stack(String s){
      |                 ~      ^~
      |                        )
a.cc:100:11: error: expected ':' before 'void'
  100 |     public  void in(String s, int t){
      |           ^ ~~~~
      |           :
a.cc:100:21: error: 'String' has not been declared
  100 |     public  void in(String s, int t){
      |                     ^~~~~~
a.cc:104:11: error: expected ':' before 'Object'
  104 |     public Object pop(){
      |           ^~~~~~~
      |           :
a.cc:104:12: error: 'Object' does not name a type
  104 |     public Object pop(){
      |            ^~~~~~
a.cc:118:11: error: expected ':' before 'boolean'
  118 |     public boolean isEmpty(){
      |           ^~~~~~~~
      |           :
a.cc:118:12: error: 'boolean' does not name a type; did you mean 'bool'?
  118 |     public boolean isEmpty(){
      |            ^~~~~~~
      |            bool
a.cc:121:2: error: expected ';' after class definition
  121 | }
      |  ^
      |  ;
a.cc: In member function 'void Stack::in(int, int)':
a.cc:101:9: error: 'Worm' was not declared in this scope
  101 |         Worm w = new Worm(s, t);
      |         ^~~~
a.cc:102:9: error: 'stack' was not declared in this scope; did you mean 'Stack'?
  102 |         stack[sp++] = w;
      |         ^~~~~
      |         Stack
a.cc:102:23: error: 'w' was not declared in this scope
  102 |         stack[sp++] = w;
      |                       ^
a.cc: At global scope:
a.cc:124:5: error: 'String' does not name a type
  124 |     String body;
      |     ^~~~~~
a.cc:126:11: error: expected ':' before 'Worm'
  126 |     public  Worm(String s, int t){
      |           ^ ~~~~
      |           :
a.cc:126:24: error: expected ')' before 's'
  126 |     public  Worm(String s, int t){
      |                 ~      ^~
      |                        )
a.cc:130:11: error: expected ':' before 'String'
  130 |     public  String getbody(){
      |           ^ ~~~~~~
      |           :
a.cc:130:13: error: 'String' does not name a type
  130 |     public  String getbody(){
      |             ^~~~~~
a.cc:133:11: error: expected ':' before 'int'
  133 |     public int getTime(){
      |           ^~~~
      |           :
a.cc:136:2: error: expected ';' after class definition
  136 | }
      |  ^
      |  ;