#include <bits/stdc++.h>
using namespace std;
string str;
int P, Q, R;
int Minus[] = {2, 1, 0};
int Prod[][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int Sum[][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
pair<int, int> formula(int i) {
  if (str[i] == '-') {
    pair<int, int> res = formula(i + 1);
    return pair<int, int>(Minus[res.first], res.second);
  } else if (str[i] == '(') {
    pair<int, int> lhs = formula(i + 1);
    i = lhs.second;
    if (str[i] == '+') {
      pair<int, int> rhs = formula(i + 1);
      assert(str[rhs.second] == ')');
      return pair<int, int>(Sum[lhs.first][rhs.first], rhs.second + 1);
    } else if (str[i] == '*') {
      pair<int, int> rhs = formula(i + 1);
      assert(str[rhs.second] == ')');
      return pair<int, int>(Prod[lhs.first][rhs.first], rhs.second + 1);
    }
    assert(true);
  } else if (str[i] == 'P') {
    return pair<int, int>(P, i + 1);
  } else if (str[i] == 'Q') {
    return pair<int, int>(Q, i + 1);
  } else if (str[i] == 'R') {
    return pair<int, int>(R, i + 1);
  } else if (str[i] == '0') {
    return pair<int, int>(0, i + 1);
  } else if (str[i] == '1') {
    return pair<int, int>(1, i + 1);
  } else if (str[i] == '2') {
    return pair<int, int>(2, i + 1);
  }
  assert(false);
}
void solve() {
  int cnt;
  for (P = 0; P < 3; P++) {
    for (Q = 0; Q < 3; Q++) {
      for (R = 0; R < 3; R++) {
        pair<int, int> res = formula(0);
        if (res.first == 2) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
}
int main() {
  while (true) {
    assert(false);
    getline(cin, str);
    if (str == ".") {
      return 0;
    }
    solve();
  }
}
