// Slower C++ solution:
#include <iostream>

#include <string>

using namespace std;



void replace_all(string& s,string from,string to){

	string::size_type p = 0;

	while(p=s.find(from,p), p!=string::npos){

		s.replace(p,from.length(),to);

		p+=to.length();

	}

}



int main(void){

	int i,j,k,l,count;

	string s,ts,num[3]={"0","1","2"};

	string to0[6]={"(0*0)","(0*1)","(0*2)","(1*0)","(2*0)","(0+0)"};

	string to1[6]={"(1*1)","(1*2)","(2*1)","(0+1)","(1+0)","(1+1)"};

	string to2[6]={"(2*2)","(0+2)","(1+2)","(2+0)","(2+1)","(2+2)"};



	while(cin>>ts && ts!="."){

		count = 0;

		replace_all(ts,"--","");



		for(i=0;i<=2;i++){

			for(j=0;j<=2;j++){

				for(k=0;k<=2;k++){

					s = ts;

					replace_all(s,"P",num[i]);

					replace_all(s,"Q",num[j]);

					replace_all(s,"R",num[k]);



					while(s.length() != 1){

						replace_all(s,"-1","1");

						replace_all(s,"-2","0");

						replace_all(s,"-0","2");



						for(l=0;l<6;l++){

							replace_all(s,to0[l],"0");

							replace_all(s,to1[l],"1");

							replace_all(s,to2[l],"2");

						}

					}

					if(s=="2") count++;

				}

			}

		}



		cout<<count<<endl;

	}



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2283404921431882x):
#include<iostream>

#include<algorithm>

#include<set>

#include<vector>

#include<queue>

#include<deque>

#include<map>

#include<cstdio>

#include<cstring>

#include<string>

#include<cstdlib>

#include<cmath>

using namespace std;



#define REP(i,a,n) for(int i = a; i < n ; i++)

#define rep(i,n) REP(i,0,n)



typedef long long ll;



string ts;

string s;

int idx;



int NOT[] = {2,1,0};

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

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



int solve();



int getDigit(){

  int cnt = 0;

  while(s[idx] == '-'){

    cnt++;

    idx++;

  }



  int digit = -1;

  if(s[idx] == '('){

    idx++;

    digit = solve();

  }

  else{

    digit = s[idx] - '0';

    idx++;

  }



  if(cnt % 2 == 1){

    return NOT[digit];

  }

  return digit;

}



int solve(){

  int res = 0;



  //cout<<":"<<idx<<endl;



  int left = -1;

  int right = -1;



  if(s[idx] == '('){

    idx++;

    left = solve();

  }

  else{

    left = getDigit();

  }



  if(idx < s.length()){

    if(s[idx] == '*'){

      idx++;

      if(s[idx] == '('){

        idx++;

        right = solve();

      }

      else{

        right = getDigit();

      }



      res = AND[left][right];

    }

    else if(s[idx] == '+'){

      idx++;

      if(s[idx] == '('){

        idx++;

        right = solve();

      }

      else{

        right = getDigit();

      }



      res = OR[left][right];

    }

  }

  idx++;



  if(right == -1){

    return left;

  }

  else{

    return res;

  }

}



void replace(int R,int P,int Q){

  s = "";

  rep(i,ts.size()){

    if(ts[i] == 'R') s += (char)('0' + R);

    else if(ts[i] == 'P') s += (char)('0' + P);

    else if(ts[i] == 'Q') s += (char)('0' + Q);

    else{

      s += ts[i];

    }

  }

}



int main(){

  while(cin>>ts,ts!="."){

    int ans = 0;

    rep(R,3) rep(P,3) rep(Q,3){

      replace(R,P,Q);

      idx = 0;

      if(solve() == 2) ans++;

    }

    cout<<ans<<endl;

  }

}