#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define rep(i,n) REP(i, 0, n)
typedef function<int(vector<int>)> node_t;
size_t cur; string s;
node_t parse() {
    char c = s[cur++];
    if(isdigit(c))
      return [=](vector<int> arg) { return c - '0';};
    if(isalpha(c))
      return [=](vector<int> arg) { return arg[c - 'P'];};
    node_t left = parse();
    if(c == '-')
      return [=](vector<int> arg) { return 2 - left(arg);};
    assert(c == '(');
    char op = s[cur++];
    node_t right = parse();
    ++cur;
    if(op == '*')
      return [=](vector<int> arg) {
          int l = left(arg), r = right(arg);
          return !l || !r ? 0 : (l == 1 || r == 1 ? 1 : 2);
      };
    if(op == '+')
      return [=](vector<int> arg) {
          int l = left(arg), r = right(arg);
          return l == 2 || r == 2 ? 2 : (!l && !r ? 0 : 1);
      };
}
      
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> s && s != ".") {
        int count = 0;
        auto tree = parse();
        cur = 0;
        rep(p, 3) rep(q, 3) rep(r, 3) {
            vector<int> arg = {p, q, r};
            if(tree(arg) == 2) ++count;
        }
        cout << count << endl;
    }
    return 0;
}