#include <iostream>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

string S;
int t;

int and3(int n1, int n2) {
  if(n1 == 2 && n2 == 2) return 2;
  if(n1 == 0 || n2 == 0) return 0;
  return 1;
}

int or3(int n1, int n2) {
  if(n1 == 0 && n2 == 0) return 0;
  if(n1 == 2 || n2 == 2) return 2;
  return 1;
}

int dfs(int p, int q, int r) {
  if(S[t] == '0') {
    t++;
    return 0;
  }
  if(S[t] == '1') {
    t++;
    return 1;
  }
  if(S[t] == '2') {
    t++;
    return 2;
  }
  if(S[t] == 'P') {
    t++;
    return p;
  }
  if(S[t] == 'Q') {
    t++;
    return q;
  }
  if(S[t] == 'R') {
    t++;
    return r;
  }
  if(S[t] == '-') {
    t++;
    return 2 - dfs(p, q, r);
  }
  if(S[t] == '(') {
    t++;
    int n1 = dfs(p, q, r);
    char op = S[t++];
    int n2 = dfs(p, q, r);
    int ret;
    if(op == '*') ret = and3(n1, n2);
    if(op == '+') ret = or3(n1, n2);
    // cout << n1 << " " << op << " " << n2 << " = " << ret << endl;
    t++;
    return ret;
  }
}

int main(void) {
  while(cin >> S, S != ".") {
    int ans = 0;
    REP(i, 0, 3) REP(j, 0, 3) REP(k, 0, 3) {
      t = 0;
      if(dfs(i, j, k) == 2) ans++;
    }
    cout << ans << endl;
  }
}