#include<iostream>
#include<string>

#define fr first
#define sc second

using namespace std;

typedef pair<const char*,int> parsed;

const int retmns[]     = {2,1,0};
const int retmul3[][3] = {{0,0,0},{0,1,1},{0,1,2}}; 
const int retadd3[][3] = {{0,1,2},{1,1,2},{2,2,2}};

parsed fomula(parsed p){
  if(isdigit(*(p.fr))){
    return parsed(p.fr+1, *(p.fr)-'0');
  }
  if(*(p.fr) == '('){
    parsed a = fomula(make_pair(p.fr+1,0));
    parsed b = fomula(make_pair(a.fr+1,0));
    if( *(a.fr) == '*' ) return parsed( b.fr+1, retmul3[a.sc][b.sc] );
    else return parsed(b.fr+1, retadd3[a.sc][b.sc]);
  }
  if(*(p.fr) == '-'){
    parsed a = fomula(make_pair(p.fr+1,0));
    return parsed(a.fr, retmns[a.sc] );
  }
}

int main(){
  while(true){
    int ans = 0;
    string s;
    cin >> s;
    if(s==".")break;
    for(int p = 0; p < 3; ++p){
      for(int q = 0; q < 3; ++q){
	for(int r = 0; r < 3; ++r){
	  string t=s;
	  for(int l = 0; l < t.length(); ++l){
	    if(t[l]=='P')t[l]='0'+p;
	    if(t[l]=='Q')t[l]='0'+q;
	    if(t[l]=='R')t[l]='0'+r;
	  }
	  int ret = fomula( parsed(t.c_str(),0) ).sc;
	  //cout << "EXPRESSION : " <<  t << " = " << ret << endl;
	  if( ret == 2 ) ++ans;
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}