#include <iostream>
using namespace std;

string str;
int p;

int negate_(int f) { return 2 - f; }

int add(int a, int b) {
  if (a == 2 || b == 2) {
    return 2;
  } else if (a == 0 && b == 0) {
    return 0;
  } else {
    return 1;
  }
}
int mult(int a, int b) {
  if (a * b == 0) {
    return 0;
  } else if (a * b == 4) {
    return 2;
  } else {
    return 1;
  }
}

int P, Q, R;

int term(char c) {
  switch (c) {
    case 'P':
     return P;
    case 'Q':
      return Q;
    case 'R':
      return R;
  }
}

int formula() {
  if (str[p] == '-') {
    p++; // read -
    int f = formula();
    return negate_(f);
  } else if (isdigit(str[p])) {
    int f = str[p] - '0';
    p++; // read [0-2]
    return f;
  } else if (isalpha(str[p])) {
    int f = term(str[p]);
    p++; // read [P-R]
    return f;
  } else {
    p++; // read (
    int a = formula();
    if (str[p] == '*') {
      p++; // read *
      int b = formula();
      p++; // read )
      return mult(a, b);
    } else {
      p++; // read +
      int b = formula();
      p++; // read )
      return add(a, b);
    }
  }
}

int main() {
  while (cin >> str and str != ".") {
    p = 0;
    int ans = 0;
    for (P = 0; P < 3; P++) 
      for (Q = 0; Q < 3; Q++)
        for (R = 0; R < 3; R++)
          if (p = 0, formula() == 2) 
            ans++;
    cout << ans << endl;
  }
}