#include <bits/stdc++.h>
using namespace std;
string s;
int p, q, r, i;
int dfs() {
  int t[2] = {}, c = 0, m = 0, w = 0, k = 0, a = 0, pp = 0;
  t[0] = t[1] = -100;
  if (s[i] == '(') i++;
  for (; i < s.size(); i++) {
    pp = 0;
    if (s[i] == '(')
      t[c++] = dfs(), a++, pp++;
    else if (s[i] >= '0' && s[i] <= '2')
      t[c++] = s[i] - '0', a++;
    else if (s[i] == 'P')
      t[c++] = p, a++;
    else if (s[i] == 'Q')
      t[c++] = q, a++;
    else if (s[i] == 'R')
      t[c++] = r, a++;
    else if (s[i] == '-')
      m++;
    else if (s[i] == '+')
      w++, a = 0;
    else if (s[i] == '*')
      k++, a = 0;
    if (m % 2 && a && !pp) {
      t[c - 1] = abs(t[c - 1] - 2);
      a = m = 0;
    }
    if (s[i] == ')' && !pp) {
      if (c == 1) return t[0];
      if (w) return max(t[0], t[1]);
      if (k) return min(t[0], t[1]);
    }
  }
  return t[0];
}
int main() {
  while (cin >> s, s != ".") {
    int res = 0;
    for (int x = 0; x < 3; x++)
      for (int y = 0; y < 3; y++)
        for (int z = 0; z < 3; z++) {
          p = x;
          q = y;
          r = z;
          i = 0;
          if (dfs() == 2) res++;
        }
    cout << res << endl;
  }
}
