#include <bits/stdc++.h>
using namespace std;
int check(char op, int x, int y) {
  switch (op) {
    case '*':
      if (x == 2 && y == 2) return 2;
      return x & y;
    case '+':
      if (x == 2 || y == 2) return 2;
      return x | y;
  }
  assert(0);
}
int formula(string& form, int& idx) {
  switch (form[idx]) {
    case '0':
    case '1':
    case '2':
      ++idx;
      return form[idx - 1] - '0';
    case '-':
      ++idx;
      return 2 - formula(form, idx);
    case '(':
      ++idx;
      int x = formula(form, idx);
      char op = form[idx];
      ++idx;
      int y = formula(form, idx);
      ++idx;
      return check(op, x, y);
  }
  assert(0);
}
int main() {
  while (1) {
    string line;
    getline(cin, line);
    if (line == ".") {
      break;
    }
    int n = 0;
    for (int p = 0; p < (int)(3); ++p)
      for (int q = 0; q < (int)(3); ++q)
        for (int r = 0; r < (int)(3); ++r) {
          string tmp = line;
          for (int i = 0; i < (int)(tmp.length()); ++i) {
            switch (tmp[i]) {
              case 'P':
                tmp[i] = p + '0';
                break;
              case 'Q':
                tmp[i] = q + '0';
                break;
              case 'R':
                tmp[i] = r + '0';
                break;
            }
          }
          int idx = 0;
          if (formula(tmp, idx) == 2) {
            n++;
          }
        }
    cout << n << endl;
  }
}
