#include <iostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

int formula(istringstream& iss)
{
	char c;
	iss >> c;
	if('0' <= c && c <= '2')
		return c - '0';
	if(c == '-')
		return 2 - formula(iss);

	int tmp1 = formula(iss);
	iss >> c;
	int tmp2 = formula(iss);
	if(c == '*'){
		if(tmp1 != 2 || tmp2 != 2){
			if(tmp1 == 0 || tmp2 == 0)
				tmp1 = 0;
			else
				tmp1 = 1;
		}
	}else{
		if(tmp1 != 0 || tmp2 != 0){
			if(tmp1 == 2 || tmp2 == 2)
				tmp1 = 2;
			else
				tmp1 = 1;
		}
	}
	iss >> c;

	return tmp1;
}

void solve(string s)
{
	int answer = 0;
	for(int p=0; p<=2; ++p){
		for(int q=0; q<=2; ++q){
			for(int r=0; r<=2; ++r){
				string tmp = s;
				for(unsigned i=0; i<s.size(); ++i){
					if(tmp[i] == 'P')
						tmp[i] = p + '0';
					else if(tmp[i] == 'Q')
						tmp[i] = q + '0';
					else if(tmp[i] == 'R')
						tmp[i] = r + '0';
				}
				istringstream iss(tmp);
				if(formula(iss) == 2)
					++ answer;
			}
		}
	}
	cout << answer << endl;
}

int main()
{
	for(;;){
		string s;
		cin >> s;
		if(s == ".")
			break;

		solve(s);
	}
}