#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using namespace std;
#define INF ((1 << 30) - 1)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
vector<int> table;
bitset<16> zero(0);
bitset<16> one("1111111111111111");
bitset<16> a("1111111100000000");
bitset<16> b("1111000011110000");
bitset<16> c("1100110011001100");
bitset<16> d("1010101010101010");

void make_table() {
    table.resize(1 << 16, INF);
    table[zero.to_ulong()] = 1;
    table[one.to_ulong()] = 1;
    table[a.to_ulong()] = 1;
    table[b.to_ulong()] = 1;
    table[c.to_ulong()] = 1;
    table[d.to_ulong()] = 1;

    using P = pair<int, int>;
    priority_queue<P, vector<P>, greater<P> > pq;
    pq.emplace(1, zero.to_ulong());
    pq.emplace(1, one.to_ulong());
    pq.emplace(1, a.to_ulong());
    pq.emplace(1, b.to_ulong());
    pq.emplace(1, c.to_ulong());
    pq.emplace(1, d.to_ulong());

    while (!pq.empty()) {
//        cerr << pq.size() << endl;
        P top = pq.top();
        bitset<16> bit(static_cast<unsigned long long int>(top.second));
        auto flipped = bit;
        flipped.flip();
        pq.pop();
        if (top.first > table[bit.to_ulong()]) continue;

        //反転させたほうが短い場合
        if (table[flipped.to_ulong()] > 1 + table[bit.to_ulong()] &&
            !(bit.to_ulong() == INF)) {
            table[flipped.to_ulong()] = 1 + table[bit.to_ulong()];
            pq.emplace(table[flipped.to_ulong()], flipped.to_ulong());
        }

        for (int obit = 0; obit < 1 << 16; ++obit) {
            bitset<16> obitset(static_cast<unsigned long long int>(obit));

            //何かしらの値で積を取りたい場合
            bitset<16> anded = bit & obitset;
            if (table[anded.to_ulong()] > table[bit.to_ulong()] + table[obit] + 3 &&
                !(table[obit] == INF && bit.to_ulong() == INF)) {
                table[anded.to_ulong()] = table[bit.to_ulong()] + table[obit] + 3;
                pq.emplace(table[anded.to_ulong()], anded.to_ulong());
            }

            //何かしらの値でxorを取りたい場合
            bitset<16> xored = bit ^obitset;
            if (table[xored.to_ulong()] > table[bit.to_ulong()] + table[obit] + 3 &&
                !(table[obit] == INF && bit.to_ulong() == INF)) {
                table[xored.to_ulong()] = table[bit.to_ulong()] + table[obit] + 3;
                pq.emplace(table[xored.to_ulong()], xored.to_ulong());
            }
        }
    }


}

using State = string::const_iterator;

class Solve {
public:

    bitset<16> E(State &state) {
        if (*state == '0') {
            state++;
            return zero;
        } else if (*state == '1') {
            state++;
            return one;
        } else if (*state == 'a') {
            state++;
            return a;
        } else if (*state == 'b') {
            state++;
            return b;
        } else if (*state == 'c') {
            state++;
            return c;
        } else if (*state == 'd') {
            state++;
            return d;
        } else if (*state == '-') {
            state++;
            bitset<16> uku = E(state);
            return uku.flip();
        } else if (*state == '(') {
            state++;
            bitset<16> left = E(state);
            char op = *state;
            state++;
            bitset<16> right = E(state);
            state++;
            if (op == '*') {
                return left & right;
            } else if (op == '^') {
                return left ^ right;
            } else {
//                assert(false);
            }
        } else {
//            assert(false);
        }
    }

    bool solve() {
        string s;
        cin >> s;
        if (s == ".") return false;
        State begin = s.begin();
        auto value = E(begin);
        int ans = table[value.to_ulong()];
        cout << ans << endl;


        return true;
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    make_table();
    while (Solve().solve());

    return 0;
}



Measured memory_bytes: 7116