#include <bits/stdc++.h>
using namespace std;
int cal(char a, int d1, int d2) {
  if (a == '*') {
    return min(d1, d2);
  } else {
    return max(d1, d2);
  }
}
int ret(int a, int minus) {
  if (minus % 2)
    return 2 - a;
  else
    return a;
}
int solve(vector<char> formula, int p, int q, int r) {
  stack<int> ans;
  int minus = 0;
  for (int i = 0; i < (int)formula.size(); i++) {
    char get = formula[i];
    if (get == '0' || get == '1' || get == '2') {
      ans.push(ret((int)get - '0', minus));
    } else if (get == 'P')
      ans.push(ret(p, minus));
    else if (get == 'Q')
      ans.push(ret(q, minus));
    else if (get == 'R')
      ans.push(ret(r, minus));
    else if (get == '-')
      minus++;
    else {
      int d1, d2;
      d1 = ans.top();
      ans.pop();
      d2 = ans.top();
      ans.pop();
      ans.push(ret(cal(get, d1, d2), minus));
    }
    if (get != '-') minus = 0;
  }
  if (ans.top() == 2)
    return 1;
  else
    return 0;
}
int main(void) {
  while (1) {
    string s;
    cin >> s;
    if (s == ".") break;
    stack<char> keep;
    vector<char> formula;
    for (int i = 0; i < (int)s.length(); i++) {
      if (s[i] == '+' || s[i] == '(' || s[i] == '*')
        keep.push(s[i]);
      else if (s[i] == ')') {
        while (1) {
          char get = keep.top();
          keep.pop();
          if (get == '(') break;
          formula.push_back(get);
        }
      } else {
        formula.push_back(s[i]);
      }
    }
    int num = 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++) num += solve(formula, p, q, r);
    cout << num << endl;
  }
  return 0;
}
