// #define DEBUGGING
#include <bits/stdc++.h>
#define endl '\n'
#define ALL(V) begin(V), end(V)
#define ALLR(V) rbegin(V), rend(V)
using ll = std::int64_t;
using ull = std::uint64_t;
using PLL = std::pair<ll, ll>;
using TLL = std::tuple<ll, ll, ll>;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
#define mv_rec make_v(init, tail...)
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, size_t s, Tail... tail) { return V<decltype(mv_rec)>(s, mv_rec); }
#undef mv_rec
using namespace std;

#ifdef DEBUGGING
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

const ssize_t MAX_V = 4;
const ssize_t MAX_ALLOC = 1 << MAX_V;
const ssize_t MAX_S = 1 << MAX_ALLOC;
    
V<ll> rdp(MAX_S, 17);
VV<ll> dp(17);


bool parse(const string &s, ll &idx, ll alloc) {
    char c = s[idx++];
    if ('a' <= c && c <= 'd') {
        ll offset = c - 'a';
        return !!(alloc & (1 << offset));
    } else if (c == '(') {
        bool l = parse(s, idx, alloc);
        bool op = (s[idx++] == '*');
        bool r = parse(s, idx, alloc);
        // assert(s[idx] == ')');
        idx++;
        return (op ? l && r : l ^ r);
    } else if (c == '0') {
        return false;
    } else if (c == '1') {
        return true;
    } else if (c == '-') {
        return !parse(s, idx, alloc);
    } else {
        assert(false);
    }
}

bool parse(const string &s, ll alloc) {
    ll idx = 0;
    return parse(s, idx, alloc);
}

void init_dp() {
    auto upd = [&](ll idx, ll len) {
        if (rdp[idx] <= len) return;
        dp[len].push_back(idx);
        rdp[idx] = len;
    };

    upd(0, 1);
    upd(MAX_S - 1, 1);
    for (ll i = 0; i < 4; i++) {
        ll idx = 0;
        ll mask = (1 << i);
        for (ll S = 0; S < MAX_ALLOC; S++) if (S & mask) idx |= (1 << S);
        upd(idx, 1);
    }

    auto flip = [&](ll idx) { return idx ^ (MAX_S - 1); };

    for (ll len = 2; len <= 16; len++) {
        for (ll S : dp[len - 1]) upd(flip(S), len);
        if (len < 5) continue;
        for (ll l = 1; l <= len - 4; l++) {
            ll r = len - 3 - l;
            if (r < l) break;
            for (ll sl : dp[l]) for (ll sr : dp[r]) {
                upd(sl & sr, len);
                upd(sl ^ sr, len);
            }
        }
    }
}

bool solve() {
    string s;
    cin >> s;
    if (s == ".") return false;

    const ll result = [&] {
        ll ret = 0;
        for (ll S = 0; S < MAX_ALLOC; S++) ret |= (parse(s, S) << S);
        return ret;
    }();

    cout << rdp[result] << endl << flush;
    return true;
}

int main() {
    init_dp();
    while (solve());
    return 0;
}



Measured memory_bytes: 3560