#include <bits/stdc++.h>
using namespace std;
int expr(string& s, int& i);
int term(string& s, int& i);
int fact(string& s, int& i);
int numb(string& s, int& i);
int expr(string& s, int& i) {
  int val = term(s, i);
  while (s[i] == '+') {
    i++;
    val = max(val, term(s, i));
  }
  return val;
}
int term(string& s, int& i) {
  int val = fact(s, i);
  while (s[i] == '*') {
    i++;
    int x = fact(s, i);
    if (min(x, val) == 0)
      val = 0;
    else if (min(x, val) == 1)
      val = 1;
    else
      val = 2;
  }
  return val;
}
int invert(string& s, int& i) {
  if (isdigit(s[i])) return fact(s, i);
  while (s[i] == '-') {
    i++;
    int val2 = invert(s, i);
    if (val2 == 2)
      return 0;
    else if (val2 == 0)
      return 2;
    return 1;
  }
  return 0;
}
int fact(string& s, int& i) {
  if (s[i] == '-') return invert(s, i);
  if (isdigit(s[i])) return numb(s, i);
  i++;
  int ret = expr(s, i);
  i++;
  return ret;
}
int numb(string& s, int& i) {
  int n = s[i++] - '0';
  while (isdigit(s[i])) n = n * 10 + s[i++] - '0';
  return n;
}
int main() {
  string S;
  while (cin >> S, S != ".") {
    int res = 0;
    for (int(i) = (0); ((i)) < ((3)); ++(i))
      for (int(j) = (0); ((j)) < ((3)); ++(j))
        for (int(k) = (0); ((k)) < ((3)); ++(k)) {
          string s = S;
          for (int(l) = (0); ((l)) < ((s.size())); ++(l)) {
            if (s[l] == 'P') s[l] = i + '0';
            if (s[l] == 'Q') s[l] = j + '0';
            if (s[l] == 'R') s[l] = k + '0';
          }
          int l = 0;
          if (expr(s, l) == 2) res++;
        }
    cout << res << endl;
  }
  return 0;
}
