def calc(s):

    s = s.replace("-", "-~-")

    res = 0

    for bit in range(16):

        a, b, c, d = list(map(int, format(bit, '04b')))

        if eval(s):

            res += 1 << bit

    return res

base = "abcd10"

L = {}

for e in base:

    state = calc(e)

    L[state] = 1

ALL = 2**16-1

MAX = 10**18

for i in range(6):

    R = dict(L)

    for p in R:

        if R[p] < 16:

            L[p ^ ALL] = min(L.get(p ^ ALL, MAX), R[p] + 1)

        if R[p]+3 < 16:

            for q in R:

                if R[p] + R[q] + 3 <= 16:

                    L[p & q] = min(L.get(p & q, MAX), R[p] + R[q] + 3)

                    L[p ^ q] = min(L.get(p ^ q, MAX), R[p] + R[q] + 3)

while 1:

    s = eval(input())

    if s == '.':

        break

    print((L[calc(s)]))
a=65280; b=61680; c=52428; d=43690; e=65535

def calc(s):

    res = eval(s.replace("-", "~").replace("*", "&").replace("1", "e"))

    return e ^ ~res if res < 0 else res

base = [a, b, c, d, e, 0]

L = {el: 1 for el in base}

for i in range(6):

    R = dict(L)

    for p in R:

        if R[p] < 16:

            L[p ^ e] = min(L.get(p ^ e, 16), R[p] + 1)

        if R[p]+3 < 16:

            for q in R:

                if R[p] + R[q] + 3 <= 16:

                    L[p & q] = min(L.get(p & q, 16), R[p] + R[q] + 3)

                    L[p ^ q] = min(L.get(p ^ q, 16), R[p] + R[q] + 3)

while 1:

    s = eval(input())

    if s == '.':

        break

    print((L[calc(s)]))
def calc(s):
    s = s.replace("-", "-~-")
    res = 0
    for bit in range(16):
        a, b, c, d = list(map(int, format(bit, "04b")))
        if eval(s):
            res += 1 << bit
    return res


base = "abcd10"
L = {}
for e in base:
    state = calc(e)
    L[state] = 1
ALL = 2**16 - 1
MAX = 10**18
for i in range(6):
    R = dict(L)
    for p in R:
        if R[p] < 16:
            L[p ^ ALL] = min(L.get(p ^ ALL, MAX), R[p] + 1)
        if R[p] + 3 < 16:
            for q in R:
                if R[p] + R[q] + 3 <= 16:
                    L[p & q] = min(L.get(p & q, MAX), R[p] + R[q] + 3)
                    L[p ^ q] = min(L.get(p ^ q, MAX), R[p] + R[q] + 3)
while 1:
    s = eval(input())
    if s == ".":
        break
    print((L[calc(s)]))

a = 65280
b = 61680
c = 52428
d = 43690
e = 65535


def calc(s):
    res = eval(s.replace("-", "~").replace("*", "&").replace("1", "e"))
    return e ^ ~res if res < 0 else res


base = [a, b, c, d, e, 0]
L = {el: 1 for el in base}
for i in range(6):
    R = dict(L)
    for p in R:
        if R[p] < 16:
            L[p ^ e] = min(L.get(p ^ e, 16), R[p] + 1)
        if R[p] + 3 < 16:
            for q in R:
                if R[p] + R[q] + 3 <= 16:
                    L[p & q] = min(L.get(p & q, 16), R[p] + R[q] + 3)
                    L[p ^ q] = min(L.get(p ^ q, 16), R[p] + R[q] + 3)
while 1:
    s = eval(input())
    if s == ".":
        break
    print((L[calc(s)]))

-def calc(s): -    s = s.replace("-", "-~-") -    res = 0 -    for bit in range(16): -        a, b, c, d = list(map(int, format(bit, "04b"))) -        if eval(s): -            res += 1 << bit -    return res +a = 65280 +b = 61680 +c = 52428 +d = 43690 +e = 65535 -base = "abcd10" -L = {} -for e in base: -    state = calc(e) -    L[state] = 1 -ALL = 2**16 - 1 -MAX = 10**18 +def calc(s): +    res = eval(s.replace("-", "~").replace("*", "&").replace("1", "e")) +    return e ^ ~res if res < 0 else res + + +base = [a, b, c, d, e, 0] +L = {el: 1 for el in base} -            L[p ^ ALL] = min(L.get(p ^ ALL, MAX), R[p] + 1) +            L[p ^ e] = min(L.get(p ^ e, 16), R[p] + 1) -                    L[p & q] = min(L.get(p & q, MAX), R[p] + R[q] + 3) -                    L[p ^ q] = min(L.get(p ^ q, MAX), R[p] + R[q] + 3) +                    L[p & q] = min(L.get(p & q, 16), R[p] + R[q] + 3) +                    L[p ^ q] = min(L.get(p ^ q, 16), R[p] + R[q] + 3)