#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

string s;
int n;
int value[128];

int multiply(int x,int y){
  if(x==2 && y==2) return 2;
  else if(min(x,y) == 0) return 0;
  else return 1;
}

int add(int x,int y){
  if(x==0 && y==0) return 0;
  else if(max(x,y) == 2) return 2;
  else return 1;
}

int form(){
  if(s[n] == '-'){
    n++;
    return 2-form();
  }else if(s[n] =='('){
    n++;
    int x = form();
    char c = s[n++];
    int y = form();
    n++;
    if(c == '*'){ // *
      return multiply(x,y);
    }else{ // +
      return add(x,y);
    }
  }else{
    return value[s[n++]];
  }
}
int main(void){
  value['0'] = 0;
  value['1'] = 1;
  value['2'] = 2;
  
  while(cin>>s,s!="."){
    
    s += "$";
    int res = 0;
    for(int P = 0 ; P <= 2 ; P ++){
      for(int Q = 0 ; Q <= 2 ; Q ++){
	for(int R = 0 ; R <= 2 ; R ++){
	  value['P'] = P;
	  value['Q'] = Q;
	  value['R'] = R;
	  n = 0;
	  res += (form() == 2);
	}
      }
    }
    cout<<res<<endl;
  }
   
  return 0;
}