#include <bits/stdc++.h>
using namespace std;
string repr(string s, int p, int q, int r) {
  int tmp;
  string s_tmp = s;
  while ((tmp = s_tmp.find("P")) >= 0) {
    s_tmp.replace(s_tmp.find("P"), 1, to_string(p));
  }
  while ((tmp = s_tmp.find("Q")) >= 0) {
    s_tmp.replace(s_tmp.find("Q"), 1, to_string(q));
  }
  while ((tmp = s_tmp.find("R")) >= 0) {
    s_tmp.replace(s_tmp.find("R"), 1, to_string(r));
  }
  return s_tmp;
}
string op_plus(string left, string right) {
  int l, r;
  if (left[0] == '-') {
    l = 2 - (left[1] - '0');
  } else {
    l = left[0] - '0';
  }
  if (right[0] == '-') {
    r = 2 - (right[1] - '0');
  } else {
    r = right[0] - '0';
  }
  return to_string(max(l, r));
}
string times(string left, string right) {
  int l, r;
  if (left[0] == '-') {
    l = 2 - (left[1] - '0');
  } else {
    l = left[0] - '0';
  }
  if (right[0] == '-') {
    r = 2 - (right[1] - '0');
  } else {
    r = right[0] - '0';
  }
  return to_string(min(l, r));
}
string solve(string str) {
  while (str.substr(0, 2) == "--") {
    str.erase(str.begin(), str.begin() + 2);
  }
  if (str.length() == 1 || str.length() == 2) {
    if (str == "0") {
      return "0";
    } else if (str == "1") {
      return "1";
    } else if (str == "2") {
      return "2";
    } else if (str == "-0") {
      return "2";
    } else if (str == "-1") {
      return "1";
    } else if (str == "-2") {
      return "0";
    } else {
      cout << str << " :Error" << endl;
      return "-1";
    }
  }
  if (str[0] == '(') {
    str.erase(str.begin());
    str.pop_back();
  }
  int brcount = 0;
  int ind = 0;
  while (ind < str.length()) {
    if (str[ind] == '(') {
      ++brcount;
    }
    if (str[ind] == ')') {
      --brcount;
    }
    if ((brcount == 0) && (str[ind] == '+' || str[ind] == '*')) break;
    ++ind;
  }
  if (ind == str.length()) {
    return to_string(2 - (solve(str.substr(1, str.length() - 1))[0] - '0'));
  }
  string left, right;
  cout << str.substr(0, ind) << endl;
  cout << str.substr(ind + 1, str.length() - (ind + 1)) << endl;
  left = solve(str.substr(0, ind));
  right = solve(str.substr(ind + 1, str.length() - (ind + 1)));
  if (str[ind] == '+') {
    return op_plus(left, right);
  }
  if (str[ind] == '*') {
    return times(left, right);
  }
}
int main() {
  string s, s_tmp;
  int sum;
  while (1) {
    cin >> s;
    if (s == ".") {
      break;
    }
    sum = 0;
    for (int p = 0; p < 3; p++) {
      for (int q = 0; q < 3; q++) {
        for (int r = 0; r < 3; r++) {
          s_tmp = repr(s, p, q, r);
          if (solve(s_tmp) == "2") {
            ++sum;
          }
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}
