#include <bits/stdc++.h>
using namespace std;
const long long INF = 114514810;
const long long MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int to, cost;
  bool operator<(const edge& e) const { return cost < e.cost; }
  bool operator>(const edge& e) const { return cost > e.cost; }
};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
string s;
int p;
int P, Q, R;
int term();
int elem() {
  int tmp;
  if (s[p] == '(') {
    p++;
    tmp = term();
    p++;
    return tmp;
  }
  int cnt = 0;
  while (s[p] == '-') {
    cnt++;
    p++;
  }
  if (isdigit(s[p]))
    tmp = s[p] - '0';
  else if (s[p] == 'P')
    tmp = P;
  else if (s[p] == 'Q')
    tmp = Q;
  else if (s[p] == 'R')
    tmp = R;
  p++;
  return (cnt % 2 ? 2 - tmp : tmp);
}
int term() {
  int tmp = elem();
  while (s[p] == '+' || s[p] == '*') {
    char c = s[p];
    p++;
    int a = elem();
    if (c == '+') {
      tmp = max(tmp, a);
    } else {
      tmp = min(tmp, a);
    }
  }
  return tmp;
}
int main() {
  while (getline(cin, s)) {
    if (s == ".") break;
    p = 0;
    int cnt = 0;
    for (int i = (0); i < (3); i++)
      for (int j = (0); j < (3); j++)
        for (int k = (0); k < (3); k++) {
          p = 0;
          P = i, Q = j, R = k;
          if (term() == 2) cnt++;
        }
    cout << cnt << endl;
  }
  return 0;
}
