#include <bits/stdc++.h>
using namespace std;
int p, q, r;
int minus_(int a) {
  if (a == 0)
    return 2;
  else if (a == 2)
    return 0;
  else
    return 1;
}
int multiply(int a, int b) { return a & b; }
int plus_(int a, int b) {
  if ((a == 1 && b == 2) || (a == 2 && b == 1)) return 2;
  return a | b;
}
int solve(const string& f) {
  stack<char> c;
  stack<int> si;
  for (int i = 0; i < f.size(); ++i) {
    if ('0' <= f[i] && f[i] <= '2')
      si.push(f[i] - '0');
    else if (f[i] == 'P')
      si.push(p);
    else if (f[i] == 'Q')
      si.push(q);
    else if (f[i] == 'R')
      si.push(r);
    else if (f[i] == '-') {
      i++;
      if ('0' <= f[i] && f[i] <= '2')
        si.push(minus_(f[i] - '0'));
      else if (f[i] == 'P')
        si.push(minus_(p));
      else if (f[i] == 'Q')
        si.push(minus_(q));
      else if (f[i] == 'R')
        si.push(minus_(r));
    } else if (f[i] == ')') {
      int a = si.top();
      si.pop();
      int b = si.top();
      si.pop();
      char ope = c.top();
      c.pop();
      c.pop();
      if (ope == '*')
        si.push(multiply(a, b));
      else
        si.push(plus_(a, b));
    } else {
      c.push(f[i]);
    }
  }
  return si.top();
}
int main() {
  string f;
  while (true) {
    cin >> f;
    if (f == ".") break;
    int ans = 0;
    for (p = 0; p <= 2; ++p) {
      for (q = 0; q <= 2; ++q) {
        for (r = 0; r <= 2; ++r) {
          if (solve(f) == 2) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return EXIT_SUCCESS;
}
