#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <functional>
#include <sstream>
#include <complex>
#include <climits>
using namespace std;

#define REP(i,a,n) for(int i=(a);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define EPS 1e-8

int kake(int x, int y){ return min(x,y); }
int pulas(int x, int y){ return max(x,y); }
int inverse(int x){ return 2-x; }


int parse(char *s, int p, int q, int r){
	//printf("%c\n",*s);
	switch(*s){
		case '\0' : return 0;
		case 'P' : return p;
		case 'Q' : return q;
		case 'R' : return r;
		case '0' : case '1' : case '2' : return *s-'0';
		
		case '-' :
			return inverse(parse(s+1,p,q,r));
		case '(' :
			int x=1;
			char *tmp = s+1;
			do{
				if( *tmp=='(' )x++;
				else if( *tmp==')' )x--;
				if( x==1 ){
					if( *tmp=='*' || *tmp=='+' ) break;
				}
				//printf("x:%d , %c\n",x,*tmp);
				tmp++;
			}while(x);
			
			//printf("e? : %c\n",*tmp);
			if( *tmp=='*' ){
				return kake(parse(s+1,p,q,r),parse(tmp+1,p,q,r));
			}else if( *tmp=='+' ){
				return pulas(parse(s+1,p,q,r),parse(tmp+1,p,q,r));
			}
			puts("error");
	}
	return 0;
}
int main(){
	char in[128];
	int ret;
	while(scanf("%s",in),strcmp(in,".")){
		ret = 0;
		rep(p,3)rep(q,3)rep(r,3)if( parse(in,p,q,r)==2 )ret++;
		printf("%d\n",ret);
	}
	return 0;
}