#include<iostream>

using namespace std;

string str;

int deny[3] = {2,1,0};
int mult[3][3] = {{0,0,0},
		  {0,1,1},
		  {0,1,2}};

int add[3][3] = {{0,1,2},
		 {1,1,2},
		 {2,2,2}};

string make(string s, int p, int q, int r){
  for(int i = 0; i < s.length(); i++){
    if(s[i] == 'P') s[i] = p+'0';
    if(s[i] == 'Q') s[i] = q+'0';
    if(s[i] == 'R') s[i] = r+'0';
  }

  return s;
}

int solve(string s){
  if(s.length() == 1) return s[0]-'0';

  //cout << s << endl;

  if(s[0] == '-'){
    string next = "";
    for(int i = 1; i < s.length(); i++)
      next+=s[i];
    return deny[solve(next)];
  }

  if(s[0] == '('){
    string a = "", b = "";
    int pos = 1;

    int par = 0;

    while(s[pos] != '*' && s[pos] != '+' || par){
      a += s[pos];
      if(s[pos] == '(') par++;
      if(s[pos] == ')') par--;
      pos++;
    }

    int ismul = false;
    if(s[pos++] == '*') ismul = true;

    while(pos < s.length()-1){
      b += s[pos++];
    }
   
    if(ismul) return mult[solve(a)][solve(b)];
    else return add[solve(a)][solve(b)];
  }
}

int main(){
  while(cin >> str && str != "."){
    int ans = 0;
   
    for(int P = 0; P <= 2; P++)
      for(int Q = 0; Q <= 2; Q++)
	for(int R = 0; R <= 2; R++){
	  string s = make(str,P,Q,R);
	  if(solve(s) == 2) ans++;
	}

    cout << ans << endl;
  }
  return 0;
}