Problem: Boolean Expression Compressor

You are asked to build a compressor for Boolean expressions that transforms expressions to the shortest form keeping their meaning.

The grammar of the Boolean expressions has terminals `0` `1` `a` `b` `c` `d` `-` `^` `*` `(` `)`, start symbol <E> and the following production rule:

> <E>  ::=  `0`  |  `1`  |  `a`  |  `b`  |  `c`  |  `d`  |  `-`<E>  |  `(`<E>`^`<E>`)`  |  `(`<E>`*`<E>`)`

Letters `a`, `b`, `c` and `d` represent Boolean variables that have values of either `0` or `1`. Operators are evaluated as shown in the Table below. In other words, `-` means negation (NOT), `^` means exclusive disjunction (XOR), and `*` means logical conjunction (AND).

Table: Evaluations of operators
<image>

Write a program that calculates the length of the shortest expression that evaluates equal to the given expression with whatever values of the four variables.

For example, `0`, that is the first expression in the sample input, cannot be shortened further. Therefore the shortest length for this expression is 1.

For another example, `(a*(1*b))`, the second in the sample input, always evaluates equal to `(a*b)` and `(b*a)`, which are the shortest. The output for this expression, thus, should be `5`.

Input

The input consists of multiple datasets. A dataset consists of one line, containing an expression conforming to the grammar described above. The length of the expression is less than or equal to 16 characters.

The end of the input is indicated by a line containing one `.` (period). The number of datasets in the input is at most 200.

Output

For each dataset, output a single line containing an integer which is the length of the shortest expression that has the same value as the given expression for all combinations of values in the variables.

Sample Input


0
(a*(1*b))
(1^a)
(-(-a*-b)*a)
(a^(b^(c^d)))
.


Output for the Sample Input


1
5
2
1
13






Example

Input

0
(a*(1*b))
(1^a)
(-(-a*-b)*a)
(a^(b^(c^d)))
.


Output

1
5
2
1
13

Solution (in CPP):

Solution:
            #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;
    }