#include <bits/stdc++.h>
using namespace std;
map<char, int> gensi;
int formular(string&, int&);
int factor(string&, int&);
int NOT[3] = {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 formular(string& str, int& p) {
  int res = factor(str, p);
  while (str[p] == '-' || str[p] == '*' || str[p] == '+') {
    if (str[p] == '-') {
      p++;
      res = NOT[formular(str, p)];
    } else if (str[p] == '*') {
      p++;
      res = AND[res][formular(str, p)];
    } else if (str[p] == '+') {
      p++;
      res = OR[res][formular(str, p)];
    }
  }
  return res;
}
int factor(string& str, int& p) {
  int res;
  if (str[p] == '(') {
    while (str[p] != ')') {
      p++;
      res = formular(str, p);
    }
  }
  if (str[p] == '0' || str[p] == '1' || str[p] == '2') {
    res = str[p] - '0';
    p++;
  } else if (str[p] == 'P' || str[p] == 'Q' || str[p] == 'R') {
    res = gensi[str[p]];
    p++;
  }
  return res;
}
int main(void) {
  string str;
  while (cin >> str, str != ".") {
    int ans = 0;
    for (gensi['P'] = 0; gensi['P'] < 3; gensi['P']++) {
      for (gensi['Q'] = 0; gensi['Q'] < 3; gensi['Q']++) {
        for (gensi['R'] = 0; gensi['R'] < 3; gensi['R']++) {
          int p = 0;
          if (formular(str, p) == 2) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
