def mul(x, y)
  if x == '2' && y == '2' then '2'
  elsif x == '0' || y == '0' then '0'
  else '1'
  end
end

def plus(x, y)
  if x == '0' && y == '0' then '0'
  elsif x == '2' || y == '2' then '2'
  else '1'
  end
end

def to_num(s)
  ['0', '1', '2'].repeated_permutation(3).map { |p, q, r|
    s.gsub(/P|Q|R/, 'P' => p, 'Q' => q, 'R' => r)
  }
end

def calc(expr)
  while expr.match(/-/)
    expr.gsub!(/-(\d)/) {
      x = $1
      (2 - x.to_i).to_s
    }
  end
  while expr.size > 1
    expr.gsub!(/\((\d)\+(\d)\)/) {
      x, y = $1, $2
      plus($1, $2)
    }
    expr.gsub!(/\((\d)\*(\d)\)/) {
      x, y = $1, $2
      mul($1, $2)
    }
  end
  if expr == '2' then return true
  else return false
  end
end

while s = gets.chomp
  break if s == "."
  puts to_num(s).count{ |expr| calc(expr) }
end