#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

vector<string> var{"0","1","a","b","c","d","-a","-b","-c","-d"};
vector<string> op{"*","^"};
int min_length[POW(16)];

bool parse(string s, int &i, vector<bool> &v) {
  if ('0' <= s[i] && s[i] <= '1') {
    int val = s[i] - '0';
    i++;
    return val;
  } else if ('a' <= s[i] && s[i] <= 'd') {
    int id = s[i] - 'a';
    i++;
    return v[id];
  } else if (s[i] == '-') {
    i++;
    return !parse(s,i,v);
  } else if (s[i] == '(') {
    i++;
    bool a = parse(s,i,v);
    char op = s[i++];
    bool b = parse(s,i,v);
    i++;
    if (op == '*') return a & b;
    else return a ^ b;
  }
  return false;
}

int logical_value(string s) {
  int res = 0;
  REP(ms,POW(4)) {
    vector<bool> v;
    REP(i,4) v.push_back(ms >> i & 1);
    int idx = 0;
    res |= parse(s,idx,v) << ms;
  }
  return res;
}

void set_min_length(string s) {
  if (s.size() > 16) return;
  CHMIN(min_length[logical_value(s)],(int)s.size());
}

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

  MINF(min_length);
  REP(i,var.size()) {
    set_min_length(var[i]);
  }
  REP(i,var.size()) REP(j,var.size()) REP(p,op.size()) {
    set_min_length("(" + var[i] + op[p] + var[j] + ")");
    set_min_length("-(" + var[i] + op[p] + var[j] + ")");
  }
  REP(i,var.size()) REP(j,var.size()) REP(k,var.size()) REP(p,op.size()) REP(q,op.size()) {
    set_min_length("((" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")");
    set_min_length("(-(" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")");
    set_min_length("-((" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")");
    set_min_length("-(-(" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")");
  }
  REP(i,var.size()) REP(j,var.size()) REP(k,var.size()) REP(l,var.size()) REP(p,op.size()) REP(q,op.size()) REP(r,op.size()) {
    set_min_length("(((" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("((-(" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("(-((" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("(-(-(" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("-(((" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("-((-(" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("-(-((" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("-(-(-(" + var[i] + op[p] + var[j] + ")" + op[q] + var[k] + ")" + op[r] + var[l] + ")");
    set_min_length("((" + var[i] + op[p] + var[j] + ")" + op[q] + "(" + var[k] + op[r] + var[l] + "))");
    set_min_length("((" + var[i] + op[p] + var[j] + ")" + op[q] + "-(" + var[k] + op[r] + var[l] + "))");
    set_min_length("(-(" + var[i] + op[p] + var[j] + ")" + op[q] + "(" + var[k] + op[r] + var[l] + "))");
    set_min_length("(-(" + var[i] + op[p] + var[j] + ")" + op[q] + "-(" + var[k] + op[r] + var[l] + "))");
    set_min_length("-((" + var[i] + op[p] + var[j] + ")" + op[q] + "(" + var[k] + op[r] + var[l] + "))");
    set_min_length("-((" + var[i] + op[p] + var[j] + ")" + op[q] + "-(" + var[k] + op[r] + var[l] + "))");
    set_min_length("-(-(" + var[i] + op[p] + var[j] + ")" + op[q] + "(" + var[k] + op[r] + var[l] + "))");
    set_min_length("-(-(" + var[i] + op[p] + var[j] + ")" + op[q] + "-(" + var[k] + op[r] + var[l] + "))");
  }
  string s;
  while (cin >> s, s != ".") {
    cout << min_length[logical_value(s)] << endl;
  }

  return 0;
}


Measured memory_bytes: 3684