#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<unordered_map>
#include<map>
#define P pair<string,int>
#define p pair<char,char>
using namespace std;

int main()
{
	map<p, char>M;
	M[p('b', 'g')] = 'r';
	M[p('g', 'b')] = 'r';
	M[p('b', 'r')] = 'g';
	M[p('r', 'b')] = 'g';
	M[p('r', 'g')] = 'b';
	M[p('g', 'r')] = 'b';
	string a;
	while (cin >> a, a != "0") {
		unordered_map<string, int>U;
		queue<P>Q;
		Q.push(P(a, 1));
		while (Q.size()) {
			P c = Q.front(); Q.pop();
			bool d = true;
			for (int e = 1; e < c.first.length(); e++)if (c.first[e - 1] != c.first[e])d = false;
			if (d) { cout << c.second - 1 << endl; break; }
			for (int e = 1; e < c.first.length(); e++) {
				if (c.first[e - 1] != c.first[e]) {
					string copy = c.first;
					copy[e - 1] = copy[e] = M[p(c.first[e - 1], c.first[e])];
					if (U[copy] == 0) { U[copy] = c.second + 1; Q.push(P(copy,U[copy])); }
				}
			}
		}
	}
}