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

string s, s2;
size_t cur;

string change_string(const string &s, int i, int j, int k){
  int size = s.size();
  string s1;
  for(int l = 0; l < size; l++){
    if(s[l] == 'P'){
      s1.push_back(char('0' + i));
    }else if(s[l] == 'Q'){
      s1.push_back(char('0' + j));
    }else if(s[l] == 'R'){
      s1.push_back(char('0' + k));
    }else{
      s1.push_back(s[l]);
    }
  }
  return s1;
}

int formula(){
  if(isdigit(s2[cur])){
    return s2[cur++] - '0';
  }else if(s2[cur] == '-'){
    cur++;
    assert(cur < s2.size());
    return 2 - formula();
  }else{
    assert(s2[cur] == '(');
    cur++;
    int a = formula();
    char op = s2[cur++];
    assert(cur < s2.size());
    int b = formula();
    cur++;
    if(op == '+'){
      return max(a, b);
    }else{
      return min(a, b);
    }
  }
}
    

int main(){
  while(cin >> s && s != "."){
    int count = 0;
    for(int i = 0; i <= 2; i++){
      for(int j = 0; j <= 2; j++){
	for(int k = 0; k <= 2; k++){
	  cur = 0;
	  s2 = change_string(s, i, j, k);
	  if(formula() == 2) count++;
	}
      }
    }
    cout << count << endl;
  }
  return 0;
}

    