#include <bits/stdc++.h>
using namespace std;
string str, s;
int pos;
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);
    else
      str = str.substr(0, i + 1) + str.substr(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[i] = '2' - (s[i] - '0');
    }
  }
}
int exp();
int factor() {
  int x;
  if (s[pos] == '(') {
    ++pos;
    x = exp();
    ++pos;
  } else {
    x = s[pos++] - '0';
  }
  return x;
}
int term() {
  int x = factor();
  while (s[pos] == '*') {
    ++pos;
    x = min(x, factor());
  }
  return x;
}
int exp() {
  int x = term();
  while (s[pos] == '+') {
    ++pos;
    x = max(x, term());
  }
  return x;
}
int solve() {
  int ans = 0;
  a();
  for (char p = '0'; p <= '2'; ++p) {
    for (char q = '0'; q <= '2'; ++q) {
      for (char r = '0'; r <= '2'; ++r) {
        s = str;
        for (int i = 0; i < s.length(); ++i) {
          if (s[i] == 'P') s[i] = p;
          if (s[i] == 'Q') s[i] = q;
          if (s[i] == 'R') s[i] = r;
        }
        b(s);
        pos = 0;
        if (exp() == 2) ++ans;
      }
    }
  }
  return ans;
}
int main() {
  while ((cin >> str) && str != ".") {
    cout << solve() << endl;
  }
}
