#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000000
using namespace std;

string s,t;
int a,p;

int A(int x){return x==2?0:x==1?1:2;}
int B(int x,int y){return x==0||y==0?0:x==1||y==1?1:2;}
int C(int x,int y){return x==2||y==2?2:x==1||y==1?1:0;}
int E(){
	while(s[p]==')')p++;
	if(s[p]!='('){
		int q=p;
		while(s[p]=='-')p++;
		q=(p-q)%2;
		if(s[p]=='(') return q?A(E()):E();
		else return q?A(s[p]-'0'):s[p]-'0';
	}
	int x,y;char z;
	p++;x=E();p++;z=s[p];p++;y=E();p++;
	return z!='+'?B(x,y):C(x,y);
}

int main(){
	while(cin>>t,t!="."){
		a=0;
		rep(i,3)rep(j,3)rep(k,3){
			p=0;
			s=t;
			rep(v,s.size())s[v]=s[v]=='P'?'0'+i:s[v]=='Q'?'0'+j:s[v]=='R'?'0'+k:s[v];
			if(E()==2)a++;
		}
		cout<<a<<endl;
	}
}