#include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define bFOR(i,n1,n2) for(int i=n1;i>=n2;i--)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<int,int> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

string s;
int c1[3]={2,1,0};
int c2[3][3]={{0,0,0},{0,1,1},{0,1,2}};
int c3[3][3]={{0,1,2},{1,1,2},{2,2,2}};
int t=0;
int ret=0;
int p,q,r;
int form(){
	if(s[t]=='P')return p;
	if(s[t]=='Q')return q;
	if(s[t]=='R')return r;
	if(s[t]=='-'){
		t++;
		return c1[form()];
	}
	if(s[t]=='('){
		t++;
		int t1=form();
		t++;
		char t2=s[t];
		t++;
		int t3=form();
		t++;
		if(t2=='*'){
			return c2[t1][t3];
		}else if(t2=='+'){
			return c3[t1][t3];
		}
	}
	if(s[t]==')');
	if(s[t]=='0')return 0;
	if(s[t]=='1')return 1;
	if(s[t]=='2')return 2;
}
int main(){
	while(1){
		ret=0;
		cin>>s;
		if(s==".")break;
		for(p=0;p<3;p++){
			for(q=0;q<3;q++){
				for(r=0;r<3;r++){
					t=0;
					if(form()==2)ret++;
				}
			}
		}
		cout<<ret<<endl;
	}
	return 0;
}
		
