#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)

int ans=0;

typedef string::const_iterator State;


ll andop(ll l,ll r){
	if(l==0||r==0)return 0;
	if(l==1||r==1)return 1;
	return 2;
}

ll orop(ll l,ll r){
	if(l==2||r==2)return 2;
	if(l==1||r==1)return 1;
	return 0;
}

ll notop(ll l){
	if(l==0)return 2;
	if(l==2)return 0;
	return 1;
}

ll exp(State &begin){
	if(*begin=='('){
		begin++;
		ll left = exp(begin);
		char op = *begin;
		begin++;
		ll right= exp(begin);
		begin++;	//read ')'
		if(op=='*'){
			return andop(left,right);
		}else if(op=='+'){
			return orop(left,right);
		}
	}else if(*begin=='-'){
		begin++;
		return notop(exp(begin));
	}else{
		assert(isdigit(*begin));
		ll ret = *begin-'0';
		begin++;
		return ret;
	}
	
	assert(1);
	return -1;
}


int main(){
	string s;
	while(cin>>s&&s!="."){
		ans=0;
		rep(i,3){
			rep(j,3){
				rep(k,3){
					string tmp = s;
					rep(l,tmp.size()){
						if(tmp[l]=='P')tmp[l]='0'+i;
						if(tmp[l]=='Q')tmp[l]='0'+j;
						if(tmp[l]=='R')tmp[l]='0'+k;
					}
					State begin = tmp.begin();
					if(exp(begin)==2)ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
}