#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T = ll>
inline T in() {
  T ret;
  cin >> ret;
  return ret;
}
using state = string::const_iterator;
ll expr(state &);
ll term(state &);
ll fuct(state &);
ll expr(state &s) {
  ll ret = term(s);
  while (true) {
    char opr = *(s++);
    ll arg = term(s);
    if (opr == '+') {
      if (ret == 2 || arg == 2)
        ret = 2;
      else if (ret == 1 || arg == 1)
        ret = 1;
      else
        ret = 0;
    } else if (opr == '*') {
      if (ret == 0 || arg == 0)
        ret = 0;
      else if (ret == 1 || arg == 1)
        ret = 1;
      else
        ret = 2;
    } else {
      break;
    }
  }
  return ret;
}
ll term(state &s) {
  if (*s == '-') {
    ++s;
    return 2 - term(s);
  } else {
    return fuct(s);
  }
}
ll fuct(state &s) {
  if (*s == '(') {
    ++s;
    ll ret = expr(s);
    return ret;
  } else if (isdigit(*s)) {
    ll ret = *s - '0';
    ++s;
    return ret;
  }
}
int main() {
  while (true) {
    if (string inp = in<string>(); inp[0] != '.') {
      ll ans = 0;
      for (char p = '0'; p <= '2'; ++p) {
        for (char q = '0'; q <= '2'; ++q) {
          for (char r = '0'; r <= '2'; ++r) {
            string exstr = inp;
            replace((exstr).begin(), (exstr).end(), 'P', p);
            replace((exstr).begin(), (exstr).end(), 'Q', q);
            replace((exstr).begin(), (exstr).end(), 'R', r);
            auto s = exstr.cbegin();
            ans += expr(s) == 2;
          }
        }
      }
      cout << ans << endl;
    } else {
      break;
    }
  }
}
