#include <bits/stdc++.h>
using namespace std;
double EPS = 1e-10;
int INF = 1e9;
int MOD = 1000000007;
int _and[3][3] = {0, 0, 0, 0, 1, 1, 0, 1, 2};
int _or[3][3] = {0, 1, 2, 1, 1, 2, 2, 2, 2};
int _not[3] = {2, 1, 0};
char pyon(string s, int p, int q, int r) {
  int cnt = 0, f;
  vector<int> V;
  for (int i = 0; i < (s.size()); i++) {
    if (s[i] == '*')
      f = 0;
    else if (s[i] == '+')
      f = 1;
    else if (s[i] == '-')
      cnt++;
    else {
      int mt = s[i] == 'P' ? p : s[i] == 'Q' ? q : s[i] == 'R' ? r : s[i] - '0';
      if (cnt % 2 == 1) {
        mt = _not[mt];
        cnt = 0;
      }
      V.push_back(mt);
    }
  }
  if (f == 0)
    return (char)(_and[V[0]][V[1]] + '0');
  else
    return (char)(_or[V[0]][V[1]] + '0');
}
int rec(string s, int p, int q, int r) {
  stack<int> pr;
  while (s.size() > 1) {
    for (int i = 0; i < (s.size()); i++) {
      if (s[i] == '(') pr.push(i);
      if (s[i] == ')') {
        int top = pr.top();
        pr.pop();
        string t;
        t.assign(s, top + 1, i - top - 1);
        char tt = pyon(t, p, q, r);
        s.erase(top, i - top + 1);
        s.insert(s.begin() + top, tt);
        i = top - 1;
      }
    }
  }
  return (s[0] - '0');
}
int main() {
  string s;
  while (cin >> s && s != ".") {
    int cnt = 0;
    for (int i = 0; i < (3); i++)
      for (int j = 0; j < (3); j++)
        for (int k = 0; k < (3); k++) {
          int res = rec(s, i, j, k);
          if (res == 2) cnt++;
        }
    cout << cnt << endl;
  }
}
