#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

pair<int,int> calc(const string &s, int i) {
  if (s[i] == '-') {
    auto p = calc(s, i+1);
    p.first = 2 - p.first;
    return p;
  } else if (s[i] == '(') {
    auto lhs = calc(s, i+1);
    auto rhs = calc(s, lhs.second + 1);
    if (s[lhs.second] == '*') {
      return make_pair(min(lhs.first, rhs.first), rhs.second+1);
    } else {
      return make_pair(max(lhs.first, rhs.first), rhs.second+1);
    }
  } else {
    return make_pair(s[i] - '0', i+1);
  }
}

int main() {
  while(1){
    string s;
    cin>>s;
    if (s == ".") break;
    int cnt = 0;
    REP(i,27) {
      int v[3];
      int p = i;
      REP(j,3) {
        v[j] = p % 3;
        p /= 3;
      }
      string cp = s;
      for (char &c : cp) {
        if ('P' <= c && c <= 'R') {
          c = v[c - 'P'] + '0';
        }
      }
      if (calc(cp, 0).first == 2) ++cnt;
    }
    cout << cnt << endl;
  }
  return 0;
}