#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <cstdio>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;



typedef string::const_iterator Cursol;
int P, Q, R;
int tmp1[3] = { 2, 1, 0}, tmp2[3][3] = {{0,0,0},{0,1,1},{0,1,2}};
int tmp3[3][3] = {{0,1,2},{1,1,2},{2,2,2}};
 
int formula(Cursol &c){
  int ret ;
  if(*c == 'P') ret = P;
  else if(*c == 'Q') ret = Q;
  else if(*c == 'R') ret = R;
  else if(isdigit(*c)) ret = *c - '0';
  else if(*c == '-') return tmp1[formula(++c)];
  else if(*c == '('){
    ret = formula(++c);
    if(*c == '*') ret = tmp2[ret][formula(++c)];
    else if(*c == '+') ret = tmp3[ret][formula(++c)];
  }
  c++;
  return ret;
}
int solve2(string s){
  Cursol c;
 
  // while( cin >> s, s != "."){
    int ret = 0;
    for(P = 0 ; P < 3 ; P++ ){
      for(Q = 0 ; Q < 3 ; Q++ ){
        for(R = 0 ; R < 3 ; R++ ){
          c = s.begin();
          if( 2 == formula(c)) ret++;
        }
      }
    }
    return ret;
    // cout << ret << endl;
  // }
}



template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}
string s,tmp;
char aa[]={'P','Q','R'};

string schange(string st,char a,char b){
	// cout<<a<<" "<<b<<endl;
	rep(i,st.size()){
		if(st[i]==a){
			st[i]=b;
		}
	}
	return st;
}

string NOT(string t){
	if(t[1]=='1') return "1";
	else if(t[1]=='2') return "0";
	return "2";
}
string AND(string t){
	int a,b;
	char r;
	stringstream ss1;
	ss1<<t;
	ss1>>a;
	ss1>>r;
	ss1>>b;
	if(a==0||b==0) return "0";
	else if(a==1||b==1) return "1";
	return "2";
}
string OR(string t){
	int a,b;
	char r;
	stringstream ss1;
	ss1<<t;
	ss1>>a;
	ss1>>r;
	ss1>>b;
	if(a==2||b==2) return "2";
	else if(a==1||b==1) return "1";
	return "0";
}
bool isnum(char t){
	if(t>='0'&&t<='2') return true;
	return false;
}

int calc(string tmp){
	// cout<<tmp<<endl;
	if(tmp.size()==1&&tmp[0]=='2') return 2;
	else if(tmp.size()==1) return 0;
	int tmps=tmp.size();
	bool f=false;
	while(1){
		rep(i,tmp.size()){
				if(tmp[i]=='('){
					if(tmp[i+2]==')'){
						// cout<<"before "<<tmp<<endl;
						tmp=tmp.substr(0,i)+tmp.substr(i+1,1)+tmp.substr(i+3);
						// cout<<"after "<<tmp<<endl;
	
					}
				}
				else if(tmp[i]=='-'){
					if(isnum(tmp[i+1])){
						// cout<<"before "<<tmp<<endl;
						tmp=tmp.substr(0,i)+NOT(tmp.substr(i,2))+tmp.substr(i+2);
						// cout<<"after "<<tmp<<endl;
	
					}
				}
				else if(tmp[i]=='*'&&f){
					if(isnum(tmp[i-1])&&isnum(tmp[i+1])){
						// cout<<"before "<<tmp<<endl;
	
						tmp=tmp.substr(0,i-1)+AND(tmp.substr(i-1,3))+tmp.substr(i+2);
						// cout<<"after "<<tmp<<endl;
	
				}
			}
			else if(tmp[i]=='+'&&f){
				if(isnum(tmp[i-1])&&isnum(tmp[i+1])){
					// cout<<"before "<<tmp<<endl;
					
					tmp=tmp.substr(0,i-1)+OR(tmp.substr(i-1,3))+tmp.substr(i+2);
					// cout<<"after "<<tmp<<endl;
				}
			}
		}
		if(f) break;
		if(tmps!=tmp.size()) break;
		else f=true;
	}
	// cout<<tmp<<endl;
	return calc(tmp);
}

int solve(string s){
	int ans=0;
	int c=0;
	rep(i,3){
		if(s.find(aa[i])!=-1){
			// cout<<"before "<<s<<endl;
			s = schange(s,aa[i],'a'+c);
			c++;
			// cout<<"after "<<s<<endl;
		}
	}
	// cout<<aa[0]<<endl;
	// cout<<s<<endl;
	rep(i,3){
		rep(j,3){
			rep(k,3){
				tmp=s;
				// cout<<tmp<<endl;
				tmp = schange(tmp,'a',i+'0');
				tmp = schange(tmp,'b',j+'0');
				tmp = schange(tmp,'c',k+'0');
				// cout<<tmp<<endl;
				if(calc(tmp)==2){
					ans++;
					//cout<<"ans "<<ans<<endl;
				}
			}
		}
	}
	return ans;

}


int main(){
	while(cin>>s,s[0]!='.'){
		// cout<<s<<endl;
		// cout<<solve2(s)<<endl;
		// if(solve(s)!=solve2(s)){
			// cout<<s<<endl;
			cout<<solve(s)<<endl;//" "<<solve2(s)<<endl;
		// }
	}
	return 0;
}

Measured memory_bytes: 1248