#include <bits/stdc++.h>
using namespace std;
int pqr[3];
int fom(string h, bool f);
int main() {
  string h;
  while (getline(cin, h)) {
    if (h == ".") {
      break;
    }
    int ans = 0;
    for (int i = 0; i < 27; i++) {
      pqr[0] = i % 3;
      pqr[1] = (i / 3) % 3;
      pqr[2] = (i / 9) % 3;
      if (fom(h, 0) == 2) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
int fom(string h, bool f) {
  int ans, one, two, dow, an;
  ans = one = two = dow = an = -1;
  for (int i = 0; i < h.size(); i++) {
    if (h[i] == '0') {
      dow = 0;
    } else if (h[i] == '1') {
      dow = 1;
    } else if (h[i] == '2') {
      dow = 2;
    } else if (h[i] == 'P') {
      dow = pqr[0];
    } else if (h[i] == 'Q') {
      dow = pqr[1];
    } else if (h[i] == 'R') {
      dow = pqr[2];
    } else if (h[i] == '-') {
      f = (f + 1) % 2;
    } else if (h[i] == '+') {
      an = 0;
    } else if (h[i] == '*') {
      an = 1;
    } else if (h[i] == '(') {
      int j;
      for (j = 1; j + i < h.size(); j++) {
        if (h[j + i] == ')') {
          break;
        }
      }
      dow = fom(h.substr(i + 1, j - 1), f);
      i = i + j;
    }
    if (dow != -1) {
      if (f) {
        dow = 2 - dow;
      }
      if (one == -1) {
        one = dow;
      } else {
        two = dow;
      }
      dow = -1;
      f = 0;
    }
    if (one != -1 && two != -1) {
      if (an == 0) {
        one = max(one, two);
      }
      if (an == 1) {
        one = min(one, two);
      }
      an = -1;
      two = -1;
    }
  }
  if (one != -1 && two == -1) {
    ans = one;
  }
  return ans;
}
