#include <bits/stdc++.h>
using namespace std;
string str;
int idx;
int getn() {
  char c = str[idx];
  idx++;
  if (c == '-') {
    return 2 - getn();
  } else {
    return c - '0';
  }
}
int calc() {
  char c = str[idx];
  if (c == '(') {
    idx++;
    int r1 = calc();
    char cc = str[idx];
    idx++;
    int r2 = calc();
    idx++;
    if (cc == '*') {
      return min(r1, r2);
    } else {
      return max(r1, r2);
    }
  } else {
    return getn();
  }
}
int main() {
  while (true) {
    string s;
    cin >> s;
    if (s == ".") break;
    int res = 0;
    for (int p = 0; p < 3; p++) {
      for (int q = 0; q < 3; q++) {
        for (int r = 0; r < 3; r++) {
          str = "";
          for (int i = 0; i < s.size(); i++) {
            switch (s[i]) {
              case 'P':
                str += to_string(p);
                break;
              case 'Q':
                str += to_string(q);
                break;
              case 'R':
                str += to_string(r);
                break;
              default:
                str += s[i];
                break;
            }
          }
          str += '=';
          idx = 0;
          cerr << str << endl;
          if (calc() == 2) {
            cerr << "asdkfjf" << endl;
            res++;
          }
        }
      }
    }
    cout << res << endl;
  }
}
