#include <bits/stdc++.h>
using namespace std;

string s;
int id = 0;
int memo[3] = {0};

int recall();
int solve();

int main() {
  while(1) {
    cin >> s;
    if(s == ".") break;
    cout << solve() << endl;
  }
  return 0;
}

int solve() {
  int ans = 0;
  for(int i = 0; i < 3; ++i)
    for(int j = 0; j < 3; ++j)
      for(int k = 0; k < 3; ++k) {
        id = -1;
        memo[0] = i;
        memo[1] = j;
        memo[2] = k;
        if(recall() == 2) ++ans;
      }
  return ans;
}

int recall() {
  int now = 0, nextn = 0;
  bool option = 0;
  ++id;
  if(s[id] >= '0' && s[id] <= '2') return s[id] - '0';
  if(s[id] >= 'P' && s[id] <= 'R') return memo[s[id] - 'P'];
  if(s[id] == '-') return 2 - recall();
  now = recall();
  if(s[++id] == '+') option = 1;
  nextn = recall();
  ++id;
  if(option) {
    if(now == 2 || nextn == 2) return 2;
    return now + nextn > 0;
  }
  if(now == 0 || nextn == 0) return 0;
  return 1 + (now * nextn == 4);
}
