

class Fixnum
  def !@
    -1*self+2
  end

  def &(n)
    [self,n].min
  end

  def |(n)
    [self,n].max
  end
end

def expr(idx)
  c = $a[idx]
  if c == "-"
    e,idx = expr(idx+1)
    return [!e,idx]
  elsif c=="("
    e1,idx = expr(idx+1)
    idx+=1
    op=$a[idx]
    e2,idx = expr(idx+1)
    idx+=1
    if op == "+"
      return [e1|e2,idx]
    else 
      return [e1&e2,idx]
    end
  else
    return [c.to_i,idx]
  end
end

def calc
  expr(0)
end

$a = Array.new
loop do
  line = gets.chomp
  break if line =="."
  ans=0
  3.times do |p|
    l1 = line.gsub(/P/,p.to_s)
    3.times do |q|
      l2 = l1.gsub(/Q/,q.to_s)
      3.times do |r|
        $a = l2.gsub(/R/,r.to_s).split("")
        result,i = calc
        ans+=1 if result == 2
      end
    end
  end
  puts ans
end