#include <bits/stdc++.h>
using namespace std;
string formula;
int parse(int P, int Q, int R, int &i) {
  int a, b;
  switch (formula[i]) {
    case '0':
      a = 0;
      i++;
      break;
    case '1':
      a = 1;
      i++;
      break;
    case '2':
      a = 2;
      i++;
      break;
    case 'P':
      a = P;
      i++;
      break;
    case 'Q':
      a = Q;
      i++;
      break;
    case 'R':
      a = R;
      i++;
      break;
    case '-':
      i++;
      a = parse(P, Q, R, i);
      a = 2 - a;
      break;
    case '(':
      i++;
      a = parse(P, Q, R, i);
      if (formula[i] == '*') {
        i++;
        b = parse(P, Q, R, i);
        i++;
        a = (a * b) / 2;
      } else {
        i++;
        b = parse(P, Q, R, i);
        i++;
        if (a == 1 && b == 1)
          a = 1;
        else
          a = 2 - (((2 - a) * (2 - b)) / 2);
      }
  }
  return a;
}
int main() {
  while (1) {
    cin >> formula;
    if (formula == ".") break;
    int count = 0;
    for (int i = 0; i <= 2; i++) {
      for (int j = 0; j <= 2; j++) {
        for (int k = 0; k <= 2; k++) {
          int index = 0;
          if (parse(i, j, k, index) == 2) count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
