// AOJ 1620 Boolean Expression Compressor
// 2018.2.12 bal4u

#include <stdio.h>
#include <string.h>

typedef struct { char w; int p; } TBL;
TBL tbl[1 << 16];
char w[3000], exp[3000][17]; int top, end;
char buf[20], *p;

int eval()
{
    char c = *p; if (*p) p++;
	if      (c == '0') return 0;
	else if (c == '1') return 0xffff;
	else if (c == 'a') return 0xff00;
	else if (c == 'b') return 0xf0f0;
	else if (c == 'c') return 0xcccc;
	else if (c == 'd') return 0xaaaa;
	else if (c == '-') return (~eval()) & 0xffff;
	else if (c == '(') {
		int y, x = eval();
		if (*p++ == '^') { y = eval(); p++;	return x ^ y; }
		else             { y = eval(); p++;	return x & y; }
	}
	return 0;
}

void insert(char *s, int sz)
{
    int id;

	if (sz > 16) return;
	p = s, id = eval();
	if (sz < tbl[id].w) w[tbl[id].p] = 0, tbl[id].w = 0;
    if (tbl[id].w == 0) tbl[id].w = sz, tbl[id].p = end;
	else return;
	w[end] = sz, memcpy(exp[end], s, sz), end++;
}

void mktbl()
{
	int i, _top, __top, _end;
    char buf[100], *p, *q;

    insert("0", 1), insert("1", 1);
	insert("a", 1), insert("b", 1), insert("c", 1), insert("d", 1);

    while (top < end) {
		__top = top, _end = end;
		for (_top = __top; _top < _end; _top++) {
			if (w[_top] == 0 || w[_top] > 15 || *exp[_top] == '-') continue;
			p = exp[_top], q = buf;
			*q++ = '-'; while (*p) *q++ = *p++; *q = 0;
			insert(buf, q-buf);
		}

		for (_top = __top; _top < _end; _top++) {
			if (w[_top] == 0 || w[_top] > 13) continue;
			for (i = 0; i < _end; i++) {
				if (w[i] == 0 || w[i] > 13) continue;

				q = buf;
				*q++ = '(', p = exp[_top]; while (*p) *q++ = *p++;
				*q++ = '^', p = exp[i];    while (*p) *q++ = *p++;
				*q++ = ')', *q = 0;
                insert(buf, q-buf);

				q = buf;
				*q++ = '(', p = exp[_top]; while (*p) *q++ = *p++;
				*q++ = '*', p = exp[i];    while (*p) *q++ = *p++;
				*q++ = ')', *q = 0;
                insert(buf, q-buf);
			}
		}
		top = _end;
    }
}    

int main()
{
	mktbl();

	while (fgets(p=buf, sizeof(buf), stdin) && *p != '.') {
		printf("%d\n", tbl[eval()].w);
	}
    return 0;
}

