#include <bits/stdc++.h>
using namespace std;
int p, q, r, res = 0, pt;
string str;
int solve_rec() {
  if (str[pt] == '0')
    return 0;
  else if (str[pt] == '1')
    return 1;
  else if (str[pt] == '2')
    return 2;
  else if (str[pt] == 'P')
    return p;
  else if (str[pt] == 'Q')
    return q;
  else if (str[pt] == 'R')
    return r;
  else if (str[pt] == '-') {
    pt++;
    int a = solve_rec();
    return 2 - a;
  } else if (str[pt] == '(') {
    pt++;
    int a = solve_rec();
    pt++;
    char op = str[pt];
    pt++;
    int b = solve_rec();
    pt++;
    if (op == '*') {
      int c = a * b;
      if (c == 2)
        return 1;
      else
        return c / 2;
    } else if (op == '+')
      return max(a, b);
  } else {
    pt++;
  }
}
void solve() {
  for (p = 0; p <= 2; p++)
    for (q = 0; q <= 2; q++)
      for (r = 0; r <= 2; r++) {
        pt = 0;
        if (solve_rec() == 2) res++;
      }
  return;
}
int main() {
  while (cin >> str, str[0] != '.') {
    res = 0;
    solve();
    cout << res << endl;
  }
  return 0;
}
