#include <bits/stdc++.h>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

using namespace std;

string S;
size_t cur=0;

int val[3];

int mul[3][3]={{0,0,0},{0,1,1},{0,1,2}};
int add[3][3]={{0,1,2},{1,1,2},{2,2,2}};


int formula(){
	if(isdigit(S[cur]))
		return S[cur++]-'0';
	if(isupper(S[cur]))
		return val[S[cur++]-'P'];

	int res=0;
	if(S[cur]=='-'){
		cur++;
		res=2-formula();
	}else{
		if(S[cur]=='(') cur++;
		int a=formula();
		char op=S[cur++];
		int b=formula();
		if(S[cur]==')') cur++;
		if(op=='*')
		res=mul[a][b];
		if(op=='+')
		res=add[a][b];
	}
	return res;
}

int calc(){
	int res=0;
	rep(i,3)rep(j,3)rep(k,3){
		cur=0;
		val[0]=i,val[1]=j,val[2]=k;
		res+=(2==formula());
	}
	return res;
}

int main(void){
	while(cin >> S){
		if(S==".") break;
		cout << calc() << endl;
	}
	return 0;
}