#include <bits/stdc++.h>
            #include<iostream>
            #include<cstdio>
            #include<vector>
            #include<queue>
            #include<map>
            #include<cstring>
            #include<string>
            #include <math.h>
            #include<algorithm>
        //    #include <boost/multiprecision/cpp_int.hpp>
            #include<functional>
  //   #define int long long
            #define mod  1000000009
            #define pa pair<int,int>
            #define ll long long
            #define pal pair<double,int>
            #define ppa pair<int,pa>
            #define ssa pair<string,int>
            #define  mp make_pair
            #define  pb push_back
            #define EPS (1e-10)
            #define equals(a,b) (fabs((a)-(b))<EPS)
     
            using namespace std;
     //priority_queue<int, vector<int>, greater<int> > que;
            class Point{
            	public:
            	double x,y;
            	Point(double x=0,double y=0):x(x),y(y) {}
            	Point operator + (Point p) {return Point(x+p.x,y+p.y);}
            	Point operator - (Point p) {return Point(x-p.x,y-p.y);}
            	Point operator * (double a) {return Point(x*a,y*a);}
            	Point operator / (double a) {return Point(x/a,y/a);}
            	double absv() {return sqrt(norm());}
            	double norm() {return x*x+y*y;}
            	bool operator < (const Point &p) const{
            		return x != p.x ? x<p.x: y<p.y;
            	}
            	bool operator == (const Point &p) const{
            		return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
            	}
            };
            typedef Point Vector;
     
            struct Segment{
            Point p1,p2;
            };
     
        double hen(Vector a){
        if(fabs(a.x)<EPS && a.y>0) return acos(0);
        else if(fabs(a.x)<EPS && a.y<0) return 3*acos(0);
        else if(fabs(a.y)<EPS && a.x<0) return 2*acos(0);
        else if(fabs(a.y)<EPS && a.x>0) return 0.0;
        else if(a.y>0) return acos(a.x/a.absv());
        else return 2*acos(0)+acos(-a.x/a.absv());
     
        }
     
string itos( int i ) {
ostringstream s ;
s << i ;
return s.str() ;
}

int gcd(int v,int b){
	if(v>b) return gcd(b,v);
	if(v==b) return b;
	if(b%v==0) return v;
	return gcd(v,b%v);
}
            double dot(Vector a,Vector b){
            	return a.x*b.x+a.y*b.y;
            }
            double cross(Vector a,Vector b){
            	return a.x*b.y-a.y*b.x;
            }
        
double distans(double x1,double y1,double x2,double y2){
	double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
	return sqrt(rr);
	
}
            //----------------kokomade tenpure------------


int a[20],b[20];

vector<int> G[17];
int  sumi[67000]={0};



int sol(string s,int hi,int mi,int A,int B,int C,int D){
	if(hi==mi){
		if(s[hi]=='0') return 0;
		else if(s[hi]=='1') return 1;
		else if(s[hi]=='a') return A;
		else if(s[hi]=='b') return B;
		else if(s[hi]=='c') return C;
		else if(s[hi]=='d') return D;
	
	}
	
	if(s[hi]=='-') return 1-sol(s,hi+1,mi,A,B,C,D);
	
	else{
		if(s[b[hi]]=='^') return sol(s,hi+1,b[hi]-1,A,B,C,D)^sol(s,b[hi]+1,mi-1,A,B,C,D);
		else return sol(s,hi+1,b[hi]-1,A,B,C,D)&sol(s,b[hi]+1,mi-1,A,B,C,D);
		
		
	}
}


int va(string s){
	int l=s.length();
	int d=0;
	for(int A=0;A<2;A++)for(int B=0;B<2;B++)for(int C=0;C<2;C++)for(int D=0;D<2;D++){
		
		d=2*d+sol(s,0,l-1,A,B,C,D);
		
		
		
		
	}
	//cout << d << "\n";
	return d;
}


void te(string s){
	stack<int> st;
	for(int i=0;i<20;i++){
		a[i]=-1;
		b[i]=-1;
	}
	for(int i=0;i<s.length();i++){
		if(s[i]=='(') st.push(i);
		else if(s[i]==')'){
			int r=st.top();
			st.pop();
			a[r]=i;
		}
		else if(s[i]=='^' ||s[i]=='*' ){
			int r=st.top();
			b[r]=i;
		}
	}
	
	cout<<sumi[va(s)]<<endl;
	
}

void yaru(){
	G[1].pb(0);
	G[1].pb(65535);
	G[1].pb(255);
	G[1].pb(3855);
	G[1].pb(21845);
	G[1].pb(13107);
	for(int i=0;i<6;i++)sumi[G[1][i]]=1;
	
	for(int l=2;l<17;l++){
		
		for(int i=0;i<G[l-1].size();i++){
			if(sumi[G[l-1][i]^65535]==0){
				G[l].pb(G[l-1][i]^65535);
				sumi[G[l-1][i]^65535]=l;
			}
		}
		
		
		if(l>=5){
			
			for(int d=1;d<=(l-3)/2;d++){
				int e=l-3-d;
				
				for(int i=0;i<G[d].size();i++)for(int j=0;j<G[e].size();j++){
					if(sumi[G[d][i]&G[e][j]]==0){
							G[l].pb(G[d][i]&G[e][j]);
							sumi[G[d][i]&G[e][j]]=l;
					}
					if(sumi[G[d][i]^G[e][j]]==0){
							G[l].pb(G[d][i]^G[e][j]);
							sumi[G[d][i]^G[e][j]]=l;
					}
				}
				
				
			}
			
			
			
		}
		
		
		
	}



}



    signed  main(){
    	yaru();
    string s;
    	while(1){
    	cin>>s;
    		if(s==".")break;
    	te(s);
    	}
  //  	printf("%.10f\n",ans[n-1]);
    	return 0;
    }

Measured memory_bytes: 3420