#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<stack>
#include<queue>
#include<sstream>
#include<string>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i);

using namespace std;

const int AND[3][3]={{0,0,0},
                    {0,1,1},
                    {0,1,2}};

const int OR[3][3]={{0,1,2},
                    {1,1,2},
                    {2,2,2}};

const int NOT[3]={2,1,0};


string str;
int wp=-1;
int p,q,r;

int parse(){ 
  wp++;
  if(str[wp]=='0') return 0;
  else if(str[wp]=='1') return 1;
  else if(str[wp]=='2') return 2;
  else if(str[wp]=='-') return NOT[parse()];
  else if(str[wp]=='P') return p;
  else if(str[wp]=='Q') return q;
  else if(str[wp]=='R') return r;
  else if(str[wp]=='('){
    int ans,tmp = parse();     
    wp++;
    if(str[wp]=='+'){
      ans = OR[tmp][parse()]; 
    }else if(str[wp]=='*'){
      ans = AND[tmp][parse()];
    }
  
    wp++;
    return ans;
  }
  return 0;
}

int main(){

  while(1){
    cin >> str;
    if(str=="."){
      break;
    }

    int count=0;
    rep(i,3) rep(j,3) rep(k,3){
      p=i;q=j;r=k;
      //cout << i << j << k << endl;
      if(parse()==2) count++;
      wp=-1;
    }
    cout << count << endl;
  }
}