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

#define int long long
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)

using State = string::const_iterator;

bool expression(State &begin);
bool term(State &begin);
bool factor(State &begin);
bool number(State &begin);
void consume(State &begin, char expected) {
    if (*begin == expected)
        begin++;
    else {
        cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
        cerr << "Rest string is '";
        while (*begin) {
            cerr << *begin++;
        }
        cerr << "'" << endl;
        exit(1);
    }
}
bool expression(State &begin) {
    bool ret = term(begin);
    return ret;
}
bool term(State &begin) {
    bool ret = factor(begin);
    for (;;) {
        if (*begin == '*') {
            consume(begin, '*');
            ret &= factor(begin);
        } else if (*begin == '^') {
            consume(begin, '^');
            ret ^= factor(begin);
        } else
            break;
    }
    return ret;
}
bool factor(State &begin) {
    if (*begin == '-') {
        consume(begin, '-');
        return !factor(begin);
    } else if (*begin == '(') {
        consume(begin, '(');
        bool ret = expression(begin);
        consume(begin, ')');
        return ret;
    } else
        return number(begin);
}
bool number(State &begin) {
    if (!isdigit(*begin)) {
        cerr << "Expected digit"
             << " but got '" << *begin << "'" << endl;
        cerr << "Rest string is '";
        while (*begin) {
            cerr << *begin++;
        }
        cerr << "'" << endl;
        exit(1);
    }
    bool ret = false;
    int res = *begin - '0';
    ret = (bool)res;
    begin++;
    return ret;
}

bool dp[20][1 << 16];

int calc(string S) {
    int res = 0;
    int N = S.size();
    REP(bit, 1 << 4) {
        int a = 0, b = 0, c = 0, d = 0;
        if (bit & (1 << 0)) a = 1;
        if (bit & (1 << 1)) b = 1;
        if (bit & (1 << 2)) c = 1;
        if (bit & (1 << 3)) d = 1;
        string tmp = S;
        REP(i, N) {
            if (tmp[i] == 'a') tmp[i] = (char)('0' + a);
            if (tmp[i] == 'b') tmp[i] = (char)('0' + b);
            if (tmp[i] == 'c') tmp[i] = (char)('0' + c);
            if (tmp[i] == 'd') tmp[i] = (char)('0' + d);
        }
        State begin = tmp.begin();
        bool ret = expression(begin);
        if (ret) res |= (1 << bit);
    }
    return res;
}
set<int> canMake[17];
bitset<16> tobit(int x) {
    bitset<16> res;
    REP(i, 16) {
        if (x & (1 << i))
            res[i] = true;
        else
            res[i] = false;
    }
    return res;
}
int to_int(bitset<16> b) { return (int)b.to_ulong(); }
void init() {
    REP(i, 17) { REP(bit, 1 << 16) dp[i][bit] = false; }
    canMake[1].insert(calc("0"));
    // cout << "0:" << calc("0") << endl;
    canMake[1].insert(calc("1"));
    // cout << tobit(calc("1")) << endl;
    canMake[1].insert(calc("a"));
    // cout << tobit(calc("a")) << endl;
    canMake[1].insert(calc("b"));
    // cout << tobit(calc("b")) << endl;
    canMake[1].insert(calc("c"));
    canMake[1].insert(calc("d"));
    FOR(i, 1, 16) {
        FOR(j, 1, 16) {
            for (auto a : canMake[i]) {
                bitset<16> A = tobit(a);
                if (i + 1 <= 16) {
                    canMake[i + 1].insert(to_int(A.flip()));
                }
                for (auto b : canMake[j]) {
                    if (i + j + 3 <= 16) {
                        canMake[i + j + 3].insert((a ^ b));
                        canMake[i + j + 3].insert((a & b));
                    }
                }
            }
        }
    }
}

bool solve() {
    string S;
    cin >> S;
    if (S == ".") return false;
    int N = S.size();
    int res = calc(S);
    // cerr << "res:" << tobit(res) << endl;
    FOR(i, 1, 17) {
        if (canMake[i].count(res)) {
            cout << i << endl;
            return true;
        }
    }
    return true;
}
signed main() {
    init();
    while (solve())
        ;
}


Measured memory_bytes: 4532