#include <iostream>
#include <algorithm>
using namespace std;

string s;
int p;
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[p] == '-') {
    ++p;
    return 2-form();
  } else if(s[p] == '(') {
    ++p;
    int x = form();
    char c = s[p++];
    int y = form();
    ++p;
    if(c == '*') { // *
      return multiply(x,y);
    } else { // +
      return add(x,y);
    }
  } else {
    return value[s[p++]];
  }
}

int main() {
  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;
          p = 0;
          res += form() == 2;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}