#include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
typedef string::const_iterator State;
string s;
int formula(State& be){
	if('0'<=*be && *be<='2'){
		int ret=*be-'0';
		be++;
		return ret;
	}
	if(*be=='-'){
		be++;
		return 2-formula(be);
	}
	if(*be=='('){
		be++;
		int a=formula(be),b;
		if(*be=='*'){
			be++;
			b=formula(be);
			be++;
			return min(a,b);
		}
		if(*be=='+'){
			be++;
			b=formula(be);
			be++;
			return max(a,b);
		}
	}
}
int main(){
	while(true){
		cin >> s;
		if(s[0]=='.') break;
		vector<int> p,q,r;
		int n=s.size(),cnt=0;
		rep(i,n){
			if(s[i]=='P') p.pb(i);
			if(s[i]=='Q') q.pb(i);
			if(s[i]=='R') r.pb(i);
		}
		rep(i,3) rep(j,3) rep(k,3){
			rep(l,p.size()) s[p[l]]='0'+i;
			rep(l,q.size()) s[q[l]]='0'+j;
			rep(l,r.size()) s[r[l]]='0'+k;
//			cout << s << " ";
			State be=s.begin();
			int ans=formula(be);
			if(ans==2) cnt++;
//			cout << ans << endl;
		}
		printf("%d\n",cnt);
/*		State be=s.begin();
		int ans=formula(be);
		cout << ans << endl;*/
	}
	return 0;
}