class N:
    def __init__(self, a):
        self.a = a

    def __mul__(self, b):
        if self.a == 0:
            return N(0)
        elif self.a == 1:
            return N((b.a >= 1) * 1)
        else:
            return b

    def __add__(self, b):
        if self.a == 0:
            return b
        elif self.a == 1:
            return N((b.a > 1) + 1)
        else:
            return N(2)

    def __neg__(self):
        if self.a == 2:
            return N(0)
        elif self.a == 1:
            return N(1)
        else:
            return N(2)

while True:
    s = input().replace("-", "-").replace("0", "N(0)").replace("1", "N(1)").replace("2", "N(2)")
    N(1) + N(2)
    if s == ".": break

    result = []
    for P in map(N, range(3)):
        for Q in map(N, range(3)):
            for R in map(N, range(3)):
                result.append(eval(s).a)
    print(result.count(2))