#include <bits/stdc++.h>
using namespace std;
map<char, int> gensi;
int NOT[3] = {2, 1, 0};
int AND[3][3] = {{0, 0, 0},
	       {0, 1, 1},
	       {0, 1, 2},};
int OR[3][3]  = {{0, 1, 2},
	       {1, 1, 2},
	       {2, 2, 2},};
int formular(string&, int&);
int factor(string&, int&);
int term(string&, int&);

int formular(string& str, int& p){
  int res = term(str, p);
  while(str[p] == '*' || str[p] == '+'){
    if(str[p] == '*'){
      p++;
      return(AND[res][term(str, p)]);
    }else{
      p++;
      return(OR[res][term(str, p)]);
    }
  }
  return res;
}

int term(string& str, int& p){
  int res;
  while(str[p] == '('){
    while(str[p] != ')'){
      p++;
      res = formular(str, p);
      p++;
      return res;
    }
  }
  while(str[p] == '-'){
    p++;
    return(NOT[term(str, p)]);
  }
  while(isdigit(str[p])){
    res = str[p] - '0';
    p++;
    return res;
  }
  while(str[p] == 'P' || str[p] == 'Q' || str[p] == 'R'){
    res = gensi[str[p]];
    p++;
    return res;
  }
}

int main(void){
  string str;
  while(cin >> str, str != "."){
    int ans = 0;
    for(gensi['P'] = 0; gensi['P'] < 3; gensi['P']++){
      for(gensi['Q'] = 0; gensi['Q'] < 3; gensi['Q']++){
        for(gensi['R'] = 0; gensi['R'] < 3; gensi['R']++){
          int p = 0;
          if(formular(str, p) == 2){
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}