#include <bits/stdc++.h>
using namespace std;
string s, t;
int a, p;
int A(int x) { return x == 2 ? 0 : x == 1 ? 1 : 2; }
int B(int x, int y) { return x == 0 || y == 0 ? 0 : x == 1 || y == 1 ? 1 : 2; }
int C(int x, int y) { return x == 2 || y == 2 ? 2 : x == 1 || y == 1 ? 1 : 0; }
int E() {
  while (s[p] == ')') p++;
  if (s[p] != '(') {
    int q = p;
    while (s[p] == '-') p++;
    q = (p - q) % 2;
    if (s[p] == '(')
      q ? A(E()) : E();
    else
      return q ? A(s[p] - '0') : s[p] - '0';
  }
  int x, y;
  char z;
  p++;
  x = E();
  p++;
  z = s[p];
  p++;
  y = E();
  p++;
  return z != '+' ? B(x, y) : C(x, y);
}
int main() {
  while (cin >> t, t != ".") {
    a = 0;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        for (int k = 0; k < 3; k++) {
          p = 0;
          s = t;
          for (int v = 0; v < s.size(); v++)
            s[v] = s[v] == 'P'   ? '0' + i
                   : s[v] == 'Q' ? '0' + j
                   : s[v] == 'R' ? '0' + k
                                 : s[v];
          if (E() == 2) a++;
        }
    cout << a << endl;
  }
}
