#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define fi first
#define se second
using ii = pair<int, int>;
vector<ii> dirs = {
  {1, 0}, {0, 1}, {-1, 0}, {0, -1},  // 4方向
  {1, 1}, {-1, 1}, {-1, -1}, {1, -1},  // 斜め
  {0, 0},  // 自身
};
template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template <class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }

// debug
template <class T> ostream& operator<<(ostream& s, vector<T>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; }
template <class T> ostream& operator<<(ostream& s, vector<vector<T>>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; }
template <class T, class S> ostream& operator<<(ostream& s, pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T, class S> ostream& operator<<(ostream& s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
#ifdef _MY_DEBUG
  #define dump(...) cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), cerr << "*/\n\n";
#else
  #define dump(...)
  // #define endl "\n"
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }

struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr int MOD = 1000000007;
// *************** TEMPLATE END *************** 

vector<unordered_set<string>> ss(2);
void init_ss() {
  ss[0].insert("x");
  ss[1] = ss[0];
  REP (i, 6) {
    for (string s1: ss[0]) {
      if (s1.size() > 15) continue;
      if (s1[0] != '-') ss[1].insert("-" + s1);
      if (s1.size() > 12) continue;
      for (string s2: ss[0]) {
        if (s1.size() + s2.size() > 13) continue;
        ss[1].insert("(" + s1 + "^" + s2 + ")");
        ss[1].insert("(" + s1 + "*" + s2 + ")");
      }
    }
    ss[0] = ss[1];
  }
  ss[0].insert("0");
  ss[0].insert("1");
}

using Pt = string::const_iterator;
int expr(Pt& pt) {
  if (*pt == '(') {
    int r1 = expr(++pt);
    char op = *pt;
    int r2 = expr(++pt);
    pt++; // )
    if (op == '^') {
      return r1 ^ r2;
    } else if (op == '*') {
      return r1 * r2;
    } else assert(0);
  } else if (*pt == '-') {
    return !expr(++pt);
  } else {
    assert(isdigit(*pt));
    return *(pt++) - '0';
  }
}
int eval(const string& s) {
  Pt pt = s.begin();
  int ret = expr(pt);
  assert(ret == 1 || ret == 0);
  return ret;
}

int get_hash(string& s) {
  int ret = 0;
  REP (a, 2) {
    REP (b, 2) {
      REP (c, 2) {
        REP (d, 2) {
          string tmp = s;
          for (auto& x: tmp) {
            if (x == 'a') x = '0' + a;
            if (x == 'b') x = '0' + b;
            if (x == 'c') x = '0' + c;
            if (x == 'd') x = '0' + d;
          }
          int shift = (a << 3) + (b << 2) + (c << 1) + (d << 0);
          ret += eval(tmp) << shift;
        }
      }
    }
  }
  return ret;
}

vector<int> tbl(1 << 16, 100);
void init_tbl() {
  vector<string> transs;
  for (char x1 = 'a'; x1 <= 'd'; x1++) {
    for (char x2 = 'a'; x2 <= 'd'; x2++) {
      for (char x3 = 'a'; x3 <= 'd'; x3++) {
        for (char x4 = 'a'; x4 <= 'd'; x4++) {
          string tmp = ""s + x1 + x2 + x3 + x4;
          transs.push_back(tmp);
        }
      }
    }
  }
  for (auto& str: ss[0]) {
    for (auto& trans: transs) {
      string s = str;
      int num = 0;
      for (auto& c: s) {
        if (c == 'x') c = trans[num++];
      }
      chmin(tbl[get_hash(s)], (int)s.size());
    }
  }
}

void solve() {
  string s; cin >> s;
  if (s == ".") exit(0);
  cout << tbl[get_hash(s)] << endl;
}
signed main() {
  init_ss();
  init_tbl();
  while (1) solve();
}



Measured memory_bytes: 3868