#include <bits/stdc++.h>
using namespace std;
void PQR_change(string& s, char p, char q, char r) {
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'P') s[i] = p;
    if (s[i] == 'Q') s[i] = q;
    if (s[i] == 'R') s[i] = r;
  }
}
void minus_erace(string& s) {
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == '-' && s[i - 1] == '-') {
      s.erase(i - 1, 2);
      i = 0;
    }
  }
}
void minus_calc(string& s) {
  for (int i = 1; i < s.size(); i++) {
    if (s[i - 1] == '-') {
      if (s[i] == '0') {
        s.replace(i - 1, 2, "2");
      } else if (s[i] == '1') {
        s.replace(i - 1, 2, "1");
      } else if (s[i] == '2') {
        s.replace(i - 1, 2, "0");
      }
      i = 1;
    }
  }
}
void plus_calc(string& s) {
  string ex[9] = {"(0+0)", "(0+1)", "(0+2)", "(1+0)", "(1+1)",
                  "(1+2)", "(2+0)", "(2+1)", "(2+2)"};
  string result[9] = {"0", "1", "2", "1", "1", "2", "2", "2", "2"};
  for (int i = 0; i + 4 < s.size(); i++) {
    for (int j = 0; j < 9; j++) {
      if (s.substr(i, 5) == ex[j]) {
        s.replace(i, 5, result[j]);
        i = -1;
        break;
      }
    }
  }
}
void mult_calc(string& s) {
  string ex[9] = {"(0*0)", "(0*1)", "(0*2)", "(1*0)", "(1*1)",
                  "(1*2)", "(2*0)", "(2*1)", "(2*2)"};
  string result[9] = {"0", "0", "0", "0", "1", "1", "0", "1", "2"};
  for (int i = 0; i + 4 < s.size(); i++) {
    for (int j = 0; j < 9; j++) {
      if (s.substr(i, 5) == ex[j]) {
        s.replace(i, 5, result[j]);
        i = -1;
        break;
      }
    }
  }
}
int main() {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    for (char p = '0'; p <= '2'; p++) {
      for (char q = '0'; q <= '2'; q++) {
        for (char r = '0'; r <= '2'; r++) {
          string s_ = s;
          PQR_change(s_, p, q, r);
          minus_erace(s_);
          minus_calc(s_);
          while (s_.size() != 1) {
            plus_calc(s_);
            mult_calc(s_);
          }
          if (s_ == "2") {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
