#include <bits/stdc++.h>
using namespace std;
int rev[3] = {2, 1, 0};
int seki[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int wa[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
int f(const string &s, int b, int e) {
  if (s[b] == '(' && s[e] == ')') {
    return f(s, b + 1, e - 1);
  }
  vector<int> v;
  int ind = -1;
  int nest = 0;
  for (int i = b; i < (e); i++) {
    if (s[i] == '(') nest++;
    if (s[i] == ')') nest--;
    if ((s[i] == '*' || s[i] == '+') && nest == 0) ind = i;
  }
  int ret = -1;
  if (ind < 0) {
    int t = 0;
    for (int i = b; i < (e); i++)
      if (s[i] == '-') t++;
    if (t % 2) {
      ret = rev[s[e] - '0'];
    } else {
      ret = s[e] - '0';
    }
    return ret;
  } else {
    ret = f(s, b, ind - 1);
    if (s[ind] == '+')
      ret = wa[ret][f(s, ind + 1, e)];
    else
      ret = seki[ret][f(s, ind + 1, e)];
    return ret;
  }
}
int solve(string s, int a, int b, int c) {
  int n = s.size();
  for (int i = 0; i < (n); i++) {
    if (s[i] == 'P') s[i] = '0' + a;
    if (s[i] == 'Q') s[i] = '0' + b;
    if (s[i] == 'R') s[i] = '0' + c;
  }
  int ret = f(s, 0, n - 1);
  return ret;
}
int main(void) {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    for (int i = 0; i < (3); i++)
      for (int j = 0; j < (3); j++)
        for (int k = 0; k < (3); k++) {
          if (solve(s, i, j, k) == 2) ans++;
        }
    cout << ans << endl;
  }
}
