# AOJ 1155: How can I satisfy thee? Let me count
# Python3 2018.7.17 bal4u

def term(idx):
    x, idx = factor(idx)
    op = buf[idx]
    y, idx = factor(idx+1);
    if op == '*':
        if x == 2 and y == 2: x = 2
        elif x == 0 or y == 0: x = 0
        else: x = 1
    else:
        if x == 0 and y == 0: x = 0
        elif x == 2 or y == 2: x = 2
        else: x = 1
    return [x, idx]

def factor(idx):
    global p, q, r
    if buf[idx] == '(': x, idx = term(idx+1)
    elif buf[idx] == '-':
        x, idx = factor(idx+1)
        x = 2-x; idx -= 1
    elif '0'<= buf[idx] <='9': x = int(buf[idx])
    elif buf[idx] == 'P': x = p
    elif buf[idx] == 'Q': x = q
    else: x = r
    return [x, idx+1]

while True:
    buf = list(input())
    if buf[0] == '.': break
    ans = 0
    for p in range(3):
        for q in range(3):
            for r in range(3):
                if factor(0)[0] == 2: ans += 1
    print(ans)

