#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string str, s;
int pos;

void a() {
  for(int i = 0; i < str.length(); ++i) {
    if(str[i] != '-') continue;
    int j;
    for(j = i+1; j < str.length() && str[j] == '-'; ++j);
    if(j == i+1) continue;
    if((j-i) % 2 == 0)
      str = str.substr(0, i) + str.substr(j);
    else
      str = str.substr(0, i+1) + str.substr(j);
  }
}

int exp();

int factor2() {
  int x;
  if(s[pos] == '(') {
    ++pos;
    x = exp();
    ++pos;
  } else {
    x = s[pos++] - '0';
  }
  return x;
}

int factor() {
  int x;
  if(s[pos] == '-') {
    ++pos;
    x = 2-factor();
  } else {
    x = factor2();
  }
  return x;
}

int term() {
  int x = factor();
  while(s[pos] == '*') {
    ++pos;
    x = min(x, factor());
  }
  return x;
}

int exp() {
  int x = term();
  while(s[pos] == '+') {
    ++pos;
    x = max(x, term());
  }
  return x;
}

int solve() {
  int ans = 0;
  //a();
  for(char p = '0'; p <= '2'; ++p) {
    for(char q = '0'; q <= '2'; ++q) {
      for(char r = '0'; r <= '2'; ++r) {
	s = str;
	for(int i = 0; i < s.length(); ++i) {
	  if(s[i] == 'P') s[i] = p;
	  if(s[i] == 'Q') s[i] = q;
	  if(s[i] == 'R') s[i] = r;
	}
	pos = 0;
	if(exp() == 2) ++ans;
      }
    }
  }
  return ans;
}

int main() {
  while((cin >> str) && str != ".") {
    cout << solve() << endl;
  }
}