#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));

    bool done = false;
    while (q.size()) {
      string s = q.front()._1;
      int r = q.front()._2;
      q.pop();
      if (r > 20) break;

      bool ok = true;
      rep(i, N) if (s[i] != s[0]) ok = false;
      if (ok) {
        done = true;
        cout << r << "\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));
      }
    }
    if (!done) {
      cout << "NA\n";
    }
  }
  return 0;
}