#!/usr/bin/ruby
class Ternary
	def initialize(n)
		@n=n
	end
	def to_i
		@n
	end
	def coerce(o)
		[Ternary.new(o<0 ? 2-o : o),self]
	end
	def -@
		Ternary.new(2-to_i)
	end
	def *(o)
		Ternary.new([to_i,o.to_i].min)
	end
	def +(o)
		Ternary.new([to_i,o.to_i].max)
	end
end
R=[*0..2]
loop{
	s=gets.chomp
	break if s=='.'
	r=0
	R.each{|a|R.each{|b|R.each{|c|
		r+=1 if eval(s.gsub('P','Ternary.new(%d)'%a).gsub('Q','Ternary.new(%d)'%b).gsub('R','Ternary.new(%d)'%c)).to_i==2
	}}}
	p r
}

