#include <iostream>
#include <cassert>
#include <string>
#include <functional>

using namespace std;

typedef function<int(int[3])> node_t;

string S;
size_t cur;

int andtable[3][3] = { {0,0,0},{0,1,1},{0,1,2} };
int ortable[3][3] = { {0,1,2},{1,1,2},{2,2,2} };

node_t parse(){
  char c = S[cur++];
  if(isdigit(c))
    return [=](int[3]){ return c-'0'; };
  if(isalpha(c))
    return [=](int a[3]){ return a[c-'P']; };

  node_t left = parse();
  if(c == '-')
    return [=](int a[3]){ return 2-left(a);};

  assert(c == '(');
  char op = S[cur++];
  node_t right = parse();
  ++cur;

  if(op == '*'){
    return [=](int a[3]){
      return andtable[right(a)][left(a)];
    };
  }else{
    return [=](int a[3]){
      return ortable[right(a)][left(a)];
    };
  }
}

int solve(){
  cur = 0;
  auto tree = parse();
  int count = 0;
  for(int p:{0,1,2}){
    for(int q:{0,1,2}){
      for(int r:{0,1,2}){
        int a[] = {p,q,r};
        if(tree(a)==2) ++ count;
      }
    }
  }
  return count;
}
int main(){

  while(cin>>S&&S[0]!='.')
    cout << solve() << endl;
  return 0;
}