#include <bits/stdc++.h>
using namespace std;
int I;
int dfs(string s, int ad) {
  int ret = 0;
  for (I = ad; I < s.size(); I++) {
    if (s[I] == '(') {
      ret = dfs(s, I + 1);
    } else if (s[I] == ')') {
      return ret;
    } else if (s[I] == '0') {
      ret = 0;
    } else if (s[I] == '1') {
      ret = 1;
    } else if (s[I] == '2') {
      ret = 2;
    } else if (s[I] == '-') {
      if (s[I + 1] == '(')
        ret = 2 - dfs(s, I + 2);
      else
        ret = 2 - dfs(s, I + 1);
    } else if (s[I] == '*') {
      ret = min(ret, dfs(s, I + 1));
    } else if (s[I] == '+') {
      ret = max(ret, dfs(s, I + 1));
    }
  }
  return ret;
}
int main() {
  string str;
  while (cin >> str, str != ".") {
    for (int i = 1; i < str.size(); i++) {
      if (str[i - 1] == '-' && str[i] == '-') {
        for (int j = i + 1; j < str.size(); j++) {
          str[j - 2] = str[j];
        }
        string tmp = "";
        for (int i = 0; i < str.size() - 2; i++) tmp += str[i];
        str = tmp;
      }
    }
    string t = "(";
    for (int i = 0; i < str.size(); i++) {
      if (isdigit(str[i]))
        t += "(", t += str[i], t += ")";
      else if (str[i] == 'P')
        t += "(", t += str[i], t += ")";
      else if (str[i] == 'Q')
        t += "(", t += str[i], t += ")";
      else if (str[i] == 'R')
        t += "(", t += str[i], t += ")";
      else
        t += str[i];
    }
    t += ")";
    str = t;
    int ans = 0;
    for (int p = 0; p < 3; p++) {
      for (int q = 0; q < 3; q++) {
        for (int r = 0; r < 3; r++) {
          string s = str;
          for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'P') s[i] = p + '0';
            if (s[i] == 'Q') s[i] = q + '0';
            if (s[i] == 'R') s[i] = r + '0';
          }
          int d = dfs(s, 0);
          if (d == 2) ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
