#include <bits/stdc++.h>
using namespace std;
string s;
int Times[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int Plus[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
int gyaku[3][2] = {{0, 2}, {1, 1}, {2, 0}};
int calc_RPN(string rpn, int P, int Q, int R) {
  stack<int> st;
  int f = 0;
  for (int idx = 0; idx < rpn.size(); idx++) {
    if (rpn[idx] == '-')
      f++;
    else if (rpn[idx] == '0' || rpn[idx] == '1' || rpn[idx] == '2') {
      st.push(gyaku[rpn[idx] - '0'][(rpn[idx] - '0' + f) % 2]);
      f = 0;
    } else if (rpn[idx] == '*') {
      int b = st.top();
      st.pop();
      int a = st.top();
      st.pop();
      st.push(Times[a][b]);
    } else if (rpn[idx] == '+') {
      int b = st.top();
      st.pop();
      int a = st.top();
      st.pop();
      st.push(Plus[a][b]);
    }
  }
  return st.top();
}
int solve(int P, int Q, int R) {
  string RPN;
  stack<char> sign_st;
  int idx = 0;
  while (idx < s.size()) {
    if (s[idx] == '0' || s[idx] == '1' || s[idx] == '2') {
      RPN += s[idx];
    } else if (s[idx] == 'P' || s[idx] == 'Q' || s[idx] == 'R') {
      if (s[idx] == 'P') {
        RPN += ('0' + P);
      }
      if (s[idx] == 'Q') {
        RPN += ('0' + Q);
      }
      if (s[idx] == 'R') {
        RPN += ('0' + R);
      }
    } else if (s[idx] == '(') {
      sign_st.push('(');
    } else if (s[idx] == '-') {
      RPN += '-';
    } else if (s[idx] == '*') {
      sign_st.push('*');
    } else if (s[idx] == '+') {
      sign_st.push('+');
    } else if (s[idx] == ')') {
      while (sign_st.top() != '(') {
        char tmp = sign_st.top();
        sign_st.pop();
        RPN += tmp;
      }
      sign_st.pop();
    }
    idx++;
  }
  while (sign_st.size()) {
    RPN += sign_st.top();
    sign_st.pop();
  }
  return calc_RPN(RPN, P, Q, R);
}
int main() {
  while (cin >> s, s != ".") {
    int ans = 0;
    for (int P = 0; P <= 2; P++) {
      for (int Q = 0; Q <= 2; Q++) {
        for (int R = 0; R <= 2; R++) {
          if (solve(P, Q, R) == 2) ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
