#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

string s;
int i;
int P, Q, R;

int formula() {
  if (s[i] == '(') {
    i++;
    int A = formula();
    char op = s[i++];
    int B = formula();
    int tmp;
    if (op == '*') {
      if (A == 0 || B == 0) tmp = 0;
      else if (A == 1 || B == 1) tmp = 1;
      else tmp = 2;
    } else {
      if (A == 2 || B == 2) tmp = 2;
      else if (A == 1 || B == 1) tmp = 1;
      else tmp = 0;
    }
    i++;
    return tmp;
  } else if (s[i] == '-') {
    i++;
    int tmp = formula();
    if (tmp == 0) return 2;
    if (tmp == 1) return 1;
    if (tmp == 2) return 0;
  } else {
    char ch = s[i];
    i++;
    if (ch == 'P') return P;
    if (ch == 'Q') return Q;
    if (ch == 'R') return R;
    return ch - '0';
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  while (cin >> s, s != ".") {
    int ans = 0;
    for (P = 0; P <= 2; P++) {
      for (Q = 0; Q <= 2; Q++) {
        for (R = 0; R <= 2; R++) {
          i = 0;
          int tmp = formula();
          ans += tmp == 2;
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}
