#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

string s;
vector<int> p,q,r;
int x;

int formula(){
	int ans;
	if(s[x] == '0' || s[x] == '1' || s[x] == '2'){
		stringstream ss;
		ss << s[x];
		ss >> ans;
		return ans;
	}
	else if(s[x] == '-'){
		x++;
		return 2 - formula();
	}
	else if(s[x] == '('){
		x++;
		int a = formula();
		x++;
		if(s[x] == '+'){
			x++;
			int b = formula();
			x++;
			return max(a,b);
		}
		else if(s[x] == '*'){
			x++;
			int b = formula();
			x++;
			return min(a,b);
		}
	}
}

int main(){
	int ans;
	while(1){
		cin >> s;
		if(s[0] == '.')break;
		ans = 0;
		p.clear();
		q.clear();
		r.clear();
		for(int i = 0;i < s.size();i++){
			if(s[i] == 'P')p.push_back(i);
			else if(s[i] == 'Q')q.push_back(i);
			else if(s[i] == 'R')r.push_back(i);
		}
		for(int i = 0;i < 3;i++){
			for(int j = 0;j < 3;j++){
				for(int k = 0;k < 3;k++){
					for(int l = 0;l < p.size();l++)s[p[l]] = '0' + i;
					for(int l = 0;l < q.size();l++)s[q[l]] = '0' + j;
					for(int l = 0;l < r.size();l++)s[r[l]] = '0' + k;
					x = 0;
					if(formula() == 2)ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}