#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':
      return form[idx] - '0';
    case '-':
      return 2 - formula(form, ++idx);
    case '(':
      int x = formula(form, ++idx);
      char op = form[++idx];
      int y = formula(form, ++idx);
      ++idx;
      return check(op, x, y);
  }
  assert(0);
}
int solve(string line, int p, int q, int r) {
  for (int i = 0; i < (int)(line.length()); ++i) {
    switch (line[i]) {
      case 'P':
        line[i] = p + '0';
        break;
      case 'Q':
        line[i] = q + '0';
        break;
      case 'R':
        line[i] = r + '0';
        break;
    }
  }
  int idx = 0;
  return formula(line, idx);
}
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) {
          if (solve(line, p, q, r) == 2) {
            n++;
          }
        }
    cout << n << endl;
  }
}
