#include<bits/stdc++.h>
using namespace std;

using Val=bitset<(1<<4)>;

Val Primitive(int i){
    Val x;
    for(int j=0;j<(1<<4);j++){
        if((j>>i)&1) x[j]=true;
    }
    return x;
}
Val Exp(int &p,string s){
    if(s[p]=='-'){
        p++;
        return ~Exp(p,s);
    }
    if(s[p]=='('){
        p++;
        Val lhs=Exp(p,s);
        char op=s[p++];
        Val rhs=Exp(p,s);
        assert(s[p++]==')');
        if(op=='*') return lhs&rhs;
        else return lhs^rhs;
    }
    if(isdigit(s[p])){
        Val res;
        for(int i=0;i<(1<<4);i++) res[i]=s[p]-'0';
        p++;
        return res;
    }
    return Primitive(s[p++]-'a');
}
struct P{
    int len;
    Val v;
};
bool operator<(P lhs,P rhs){
    return lhs.len>rhs.len;
}
int solve(string s){
    int p=0;
    Val org=Exp(p,s);
    priority_queue<P> que;
    que.push(P{1,0});
    que.push(P{1,(1<<(1<<4))-1});
    for(int i=0;i<4;i++) que.push(P{1,Primitive(i)});
    unordered_map<Val,int> mp;
    while(!que.empty()){
        auto tmp=que.top(); que.pop();
        Val v=tmp.v;
        int len=tmp.len;
        if(mp.count(v)) continue;
        if(org==v) return len;
        mp[v]=len;
        que.push(P{len+1,~v});
        for(auto &e:mp){
            que.push(P{len+e.second+3,v&e.first});
            que.push(P{len+e.second+3,v^e.first});
        }
    }
    assert(false);
}
int main(){
    string s;
    while(cin>>s,s!="."){
        cout<<solve(s)<<endl;
    }
    return 0;
}
