#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int INF = (1 << 30);
string substitute(string s, int p, int q, int r) {
  for (int((i)) = (0); ((i)) < ((s.size())); ((i))++) {
    switch (s[i]) {
      case 'P':
        s[i] = '0' + p;
        break;
      case 'Q':
        s[i] = '0' + q;
        break;
      case 'R':
        s[i] = '0' + r;
        break;
    }
  }
  return s;
}
void apply_inverse(stack<char>& info) {
  char t = info.top();
  if (t < '0' || '2' < t) return;
  info.pop();
  while (!info.empty() && info.top() == '-') {
    info.pop();
    if (t == '2')
      t = '0';
    else if (t == '0')
      t = '2';
  }
  info.push(t);
}
void apply_operator(stack<char>& info) {
  char r = info.top();
  info.pop();
  char ope = info.top();
  info.pop();
  char l = info.top();
  info.pop();
  char err = info.top();
  info.pop();
  if (err != '(') {
    exit(1);
  }
  l -= '0';
  r -= '0';
  char res;
  switch (ope) {
    case '+':
      if (max(l, r) == 2)
        res = '2';
      else if (l + r == 0)
        res = '0';
      else
        res = '1';
      break;
    case '*':
      if (min(l, r) == 0)
        res = '0';
      else if (l + r == 4)
        res = '2';
      else
        res = '1';
      break;
    default:
      exit(1);
  }
  info.push(res);
}
void de(stack<char> s) {
  string tmp;
  while (!s.empty()) {
    tmp += s.top();
    s.pop();
  }
  reverse(tmp.begin(), tmp.end());
  cout << tmp << endl;
}
bool play(string s) {
  stack<char> info;
  for (char c : s) {
    switch (c) {
      case '0':
      case '1':
      case '2':
        info.push(c);
        apply_inverse(info);
        break;
      case ')':
        apply_operator(info);
        apply_inverse(info);
        break;
      default:
        info.push(c);
        break;
    }
    de(info);
  }
  if (info.size() != 1) {
    exit(1);
  } else {
    return info.top() == '2';
  }
}
void calc(string formula) {
  int res = 0;
  for (int((p)) = (0); ((p)) < ((3)); ((p))++)
    for (int((q)) = (0); ((q)) < ((3)); ((q))++)
      for (int((r)) = (0); ((r)) < ((3)); ((r))++) {
        string target = substitute(formula, p, q, r);
        if (play(target)) {
          res++;
        }
      }
  cout << res << endl;
}
void debug() {
  int N;
  cin >> N;
}
int main() {
  while (true) {
    string formula;
    cin >> formula;
    if (formula == ".") break;
    calc(formula);
  }
  return 0;
}
