#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
vector<string>hoge[25];
vector<int>val[25];
unordered_map<string,int>M;
int rec(string str){
     
    if(str.size() == 1){
        return str[0]-'0';
    }
    if(str.size()<=14){
        if(M.find(str) != M.end()) return M[str];
    }
    if(str[0] == '-'){
        string hoge = str;
        reverse(str.begin(),str.end());
        str.resize(str.size()-1);
        reverse(str.begin(),str.end());
        int res = rec(str);
        if(hoge.size()<=14) return M[hoge] = 1-res;
        else return 1-res;
    }
    else{
        string p="",q="";
        bool ty;
        int cur = 0;
        for(int i=1;;i++){
            if(cur == 0 && (str[i] == '^' || str[i] == '*')){
                if(str[i] == '^') ty = true;
                else ty = false;
                for(int j=i+1;j<str.size()-1;j++){
                    q.pb(str[j]);
                }
                int res1 = rec(p);
                int res2 = rec(q);
                if(ty){
                    if(str.size()<=14) return M[str] = (res1^res2);
                    else return (res1^res2);
                }
                else{
                    if(str.size()<=14) return M[str] = (res1&res2);
                    else return (res1&res2);
                }
            }
            p.pb(str[i]);
            if(str[i] == '(') cur++;
            else if(str[i] == ')') cur--;
        }
    }
}
int main(){
    hoge[1].pb("0");
    hoge[1].pb("1");
    hoge[1].pb("a");
    hoge[1].pb("b");
    hoge[1].pb("c");
    hoge[1].pb("d");
    for(int i=2;i<=15;i++){
        for(int j=0;j<hoge[i-1].size();j++){
            string nw = "-"+hoge[i-1][j];
            hoge[i].pb(nw);
        }
        if(i>=5){
            for(int len=1;len<=i-4;len++){
                int len2 = i-3-len;
                for(int j=0;j<hoge[len].size();j++){
                    for(int k=0;k<hoge[len2].size();k++){
                        string nw = "("+hoge[len][j]+"^"+hoge[len2][k]+")";
                        hoge[i].pb(nw);
                        nw = "("+hoge[len][j]+"*"+hoge[len2][k]+")";
                        hoge[i].pb(nw);
                    }
                }
            }
        }
        SORT(hoge[i]);
        ERASE(hoge[i]);
    }
    for(int i=1;i<=15;i++){
        for(int j=0;j<hoge[i].size();j++){
            int ky = 0;
            int ans[16];
            for(int m=0;m<16;m++){
                int a = (((m>>0)&1));
                int b = (((m>>1)&1));
                int c = (((m>>2)&1));
                int d = (((m>>3)&1));
                string x = hoge[i][j];
                bool A=0,B=0,C=0,D=0;
                rep(q,x.size()){
                    if(x[q] == 'a') x[q] = '0'+a,A = 1;
                    if(x[q] == 'b') x[q] = '0'+b,B = 1;
                    if(x[q] == 'c') x[q] = '0'+c,C = 1;
                    if(x[q] == 'd') x[q] = '0'+d,D = 1;
                }
                int res = 0;
                if(!A && a) res = ans[m-1];
                else if(!B && b) res = ans[m-2];
                else if(!C && c) res = ans[m-4];
                else if(!D && d) res = ans[m-8];
                else res = rec(x);
                ky += (1<<m)*res;
                ans[m] = res;
            }
            val[i].pb(ky);
        }
    }
    while(1){
        string str; cin>>str;
        if(str[0] == '.') return 0;
        int ky = 0;
                for(int i=0;i<16;i++){
                    int a = (((i>>0)&1));
                    int b = (((i>>1)&1));
                    int c = (((i>>2)&1));
                    int d = (((i>>3)&1));
                    string x = str;
                    rep(q,x.size()){
                        if(x[q] == 'a') x[q] = '0'+a;
                        if(x[q] == 'b') x[q] = '0'+b;
                        if(x[q] == 'c') x[q] = '0'+c;
                        if(x[q] == 'd') x[q] = '0'+d;
                    }
                    int res = rec(x);
                    ky += (1<<i)*res;
                }
        for(int j=1;j<=min(15,(int)(str.size()));j++){
            for(int k=0;k<hoge[j].size();k++){
                if(ky == val[j][k]){
                    cout<<j<<endl; goto nxt;
                }
            }
        }
        cout<<16<<endl;
        nxt:;
    }
}


Measured memory_bytes: 199132