#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;

int idx = 0;
vector<int> val(4, 0);
string eval_str = "";
bool exec_str() {
    bool ret;
    if (eval_str[idx] == '-') {
        idx++;
        bool v = exec_str();
        ret = !v;
    } else if (eval_str[idx] == '(') {
        idx++;
        bool left = exec_str();
        char op = eval_str[idx++];
        bool right = exec_str();
        if (op == '*') ret = left & right;
        else ret = left ^ right;
        idx++;
    } else if (eval_str[idx] == '0' || eval_str[idx] == '1') {
        ret = eval_str[idx++] - '0';
    } else {
        int pos = 0;
        for (int i = 0; i < 4; i++) {
            if ("abcd"[i] == eval_str[idx]) {
                pos = i;
                break;
            }
        }
        ret = val[pos];
        idx++;
    }
    return ret;
}

int calc_str(string s, bool debug = false) {
    int ret = 0;
    eval_str = s;
    for (int i = 0; i < (1 << 4); i++) {
        idx = 0;
        for (int j = 0; j < 4; j++) {
            val[j] = (bool)(i & (1 << j));
            if (debug) cout << val[j] << " ";
        }
        bool flag = exec_str();
        if (debug) cout << "= " << flag << endl;
        if(flag) {
            ret = ret | (1 << i);
        }
    }
    return ret;
}

int main() {
    string s;
    while (cin >> s, s != ".") {
        int corr_val = calc_str(s, false);

        vector<string> exp[50];
        exp[1] = {"0", "1", "a", "b", "c", "d"};
        map<int, string> done;
        for (auto x : exp[1]) {
            int v = calc_str(x);
            done[v] = x;
        }
        for (int i = 1; i <= 16; i++) {
            for (auto left : exp[i]) {
                // not <E>
                string not_str = "-" + left;
                int not_val = calc_str(not_str);
                if (!done.count(not_val) || i + 1 < done[not_val].size()) {
                    done[not_val] = not_str;
                    exp[i + 1].push_back(not_str);
                }
                for (int j = 1; j <= i; j++) {
                    if (i + j + 3 > 16) continue;
                    for (auto right : exp[j]) {
                        // (<E> ^ <E>)
                        string xor_str = "(" +left + "^" + right + ")";
                        int xor_val = calc_str(xor_str);
                        if (!done.count(xor_val) || i + j + 3 < done[xor_val].size()) {
                            done[xor_val] = xor_str;
                            exp[i + j + 3].push_back(xor_str);
                        }
                        // (<E> * <E>)
                        string and_str = "(" +left + "*" + right + ")";
                        int and_val = calc_str(and_str);
                        if (!done.count(and_val) || i + j + 3 < done[and_val].size()) {
                            done[and_val] = and_str;
                            exp[i + j + 3].push_back(and_str);
                        }
                    }
                }
            }
        }
        cout << done[corr_val].size() << endl;
    }
}
