class String
	def del_neg
		s, t = self, nil
		loop {
			t = s.gsub(/-\d/){|x| (2+x.to_i).to_s}
			break if s == t
			s = t	
		}
		t
	end

	def del_or
		gsub(/(\d)\+(\d)/){|x| [$1, $2].max.to_s}
	end

	def del_and
		gsub(/(\d)\*(\d)/){|x| [$1, $2].min.to_s}
	end

	def del_par
		gsub(/\((\d)\)/){|x| $1.to_s}
	end

	def calc
		s = dup
		until s.size == 1
			s = s.del_neg
			s = s.del_or
			s = s.del_and
			s = s.del_par
		end
		s.to_i
	end
end


loop {
	s = gets.chomp
	break if s == ?.
	p (0..2).to_a.repeated_permutation(3).count{|a|
		exp = s.tr("PQR", a.join)
		exp.calc == 2
	}
}