def parse(s,p,q,r)
    case s[0]
    when "0"
        return 0
    when "1"
        return 1
    when "2"
        return 2
    when "P"
        return p
    when "Q"
        return q
    when "R"
        return r
    when "-"
        return 2 - parse(s[1...s.length],p,q,r)
    when "("
        index = 1
        level = 0
        while index < s.length
            if s[index] == "("
                level += 1
            elsif s[index] == ")"
                level -= 1
            elsif s[index] == "*" && level == 0
                a = parse(s[1...index],p,q,r)
                b = parse(s[(index+1)...s.length],p,q,r)
                return 0 if a == 0 || b == 0
                return 2 if a == 2 && b == 2
                return 1
            elsif s[index] == "+" && level == 0
                a = parse(s[1...index],p,q,r)
                b = parse(s[(index+1)...s.length],p,q,r)
                return 0 if a == 0 && b == 0
                return 2 if a == 2 || b == 2
                return 1
            end
            index += 1
        end
        return -1
    end
end
loop do
    s = gets.chomp
    break if s == "."
    ans = 0
    3.times do |p|
        3.times do |q|
            3.times do |r|
                if parse(s,p,q,r) == 2
                    ans += 1
                end
            end
        end
    end
    puts ans
end