#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B>
bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
const double PI = acos(-1);
const double EPS = 1e-9;
long long inf = sizeof(long long) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
string s;
vector<long long> alpha(3);
int t;
int formula(int a) {
  int out = -1;
  int co = 0;
  if (s[t] == '(')
    ++t, out = formula(0);
  else if (s[t] == '-')
    ++t, out = 2 - formula(1);
  else if (isdigit(s[t]))
    out = s[t] - '0';
  else if (isalpha(s[t]))
    out = alpha[s[t] - 'P'];
  if (a) return out;
  t++;
  if (s[t] == '*') {
    t++;
    out = min(out, formula(0));
  } else if (s[t] == '+') {
    t++;
    out = max(out, formula(0));
  }
  if (co % 2) out = 2 - out;
  return out;
}
void f() {
  int co = 0;
  for (int P = 0; P < 3; P++)
    for (int Q = 0; Q < 3; Q++)
      for (int R = 0; R < 3; R++) {
        t = 0;
        alpha[0] = P;
        alpha[1] = Q;
        alpha[2] = R;
        if (formula(0) == 2) co++;
      }
  cout << co << endl;
}
int main() {
  while (cin >> s, s != ".") f();
}
