#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int cur,p,q,r;
string s;

int form(){
	if(isdigit(s[cur]))return s[cur++]-'0';
	if(s[cur]=='P'){cur++;return p;}
	if(s[cur]=='Q'){cur++;return q;}
	if(s[cur]=='R'){cur++;return r;}
	if(s[cur]=='-'){cur++;return 2-form();}
	if(s[cur]=='('){
		cur++;
		int a=form();
		char op=s[cur++];
		int b=form();
		if(op=='+')a=max(a,b);
		else a=min(a,b);
		assert(s[cur]==')');
		cur++;
		return a;
	}
} 

int main() {
	while(cin>>s){
		if(s==".")return 0;
		int ans=0;
		for(int i=0;i<27;i++){
			p=i/9;q=(i/3)%3;r=i%3;
			cur=0;
			if(form()==2)ans++;
		}
		cout<<ans<<endl;
	}
} 