#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
#include <queue>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}
typedef pair<string ,int> P;

int bfs(string s){
  queue<P> que;
  que.push(P(s,0));
  
  map<string,int> d;
  while(!que.empty()){
    P p=que.front();
    que.pop();
    
    if(d[p.first])continue;
    string st = p.first;
    
    bool f = false;
    rep(i,st.size()-1){
      if(st[i]!=st[i+1]){ // find different words
	f = true;
	break;
      }
    }

    if(!f)return p.second;
    
    d[p.first] = second;
    rep(i,st.size()-1){
      if(st[i] != st[i+1]){
	char t;
	string nx;
      }
    }
    
  }
  
}

int main(void){
  string s;
  while(cin>>s,s!="0"){
    int ret = bfs(s);
    if(ret>=0)cout<<ret<<endl;
    else cout<<"NA"<<endl;
  }
}