#include <bits/stdc++.h>
using namespace std;
string str;
void a() {
  for (int i = 0; i < str.length(); ++i) {
    if (str[i] != '-') continue;
    int j;
    for (j = i + 1; j < str.length() && str[j] == '-'; ++j)
      ;
    if (j == i + 1) continue;
    if ((j - i) % 2 == 0)
      str = str.substr(0, i) + str.substr(j, str.length() - j);
    else
      str = str.substr(0, i + 1) + str.substr(j, str.length() - j);
  }
}
void b(string &s) {
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == '-') {
      s = s.substr(0, i) + s.substr(i + 1, s.length() - (i + 1));
      s[i] = '2' - (s[i] - '0');
    }
  }
}
int rec(string &, int);
int rec2(string &, int);
int rec3(string &, int);
int pos;
int rec2(string &s, int ans) {
  int tmp = pos++;
  if (tmp < s.length()) {
    if (s[tmp] == '*')
      return min(ans, rec(s, 0));
    else if (s[tmp] == '+')
      return max(ans, rec(s, 0));
  }
  return ans;
}
int rec(string &s, int ans) {
  int tmp = pos++;
  if (tmp < s.length() && s[tmp] == '(') {
    int aaa = rec(s, ans);
    return rec2(s, aaa);
  }
  return rec2(s, s[tmp] - '0');
}
int solve() {
  int cnt = 0;
  a();
  for (char p = '0'; p <= '2'; ++p) {
    for (char q = '0'; q <= '2'; ++q) {
      for (char r = '0'; r <= '2'; ++r) {
        string tmp = str;
        for (int i = 0; i < tmp.length(); ++i) {
          if (tmp[i] == 'P') tmp[i] = p;
          if (tmp[i] == 'Q') tmp[i] = q;
          if (tmp[i] == 'R') tmp[i] = r;
        }
        pos = 0;
        b(tmp);
        if (rec(tmp, 0) == 2) {
          cnt++;
        }
      }
    }
  }
  return cnt;
}
int main() {
  while ((cin >> str) && str != ".") {
    cout << solve() << endl;
  }
}
