#include <bits/stdc++.h>

using namespace std;

string formula;
int idx;

int notop[3]    = {2, 1, 0};
int andop[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int orop[3][3]  = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};

int solve()
{
  int ret; char x = formula[idx];
  if(x == '(') {
    idx++, ret = solve();
    char op = formula[idx];
    if(op == '*') idx++, ret = andop[ret][solve()];
    else if(op == '+') idx++, ret = orop[ret][solve()];
    idx++;
  }
  else if(x == ')') idx++;
  else if(x == '-') idx++, ret = notop[solve()];  
  else if(isdigit(x)) ret = x - '0', idx++;
  return ret;
}

int main()
{
  while(cin >> formula, formula != ".") {
    int ans = 0;
    for(int P = '0'; P < '3'; P++) {
      for(int Q = '0'; Q < '3'; Q++) {
	for(int R = '0'; R < '3'; R++) {
	  string temp = formula;
	  replace(formula.begin(), formula.end(), 'P', (char)P);
	  replace(formula.begin(), formula.end(), 'Q', (char)Q);
	  replace(formula.begin(), formula.end(), 'R', (char)R);
	  idx = 0; if(solve() == 2) ans++;
	  formula = temp;
	}
      }
    }
    cout << ans << endl;
  }
  return 0;
}