#include <bits/stdc++.h>
using namespace std;
char tmp[20000];
int cnt = 0;
int mminus(int num) {
  if (num == 1)
    return 1;
  else if (num == 2)
    return 0;
  else
    return 2;
}
int dfs() {
  if (tmp[cnt] == '(') {
    cnt++;
    int le = dfs();
    if (tmp[cnt] == '*') {
      cnt++;
      int ri = dfs();
      cnt++;
      if (le == 2 && ri == 2) {
        return 2;
      } else if (le != 0 && ri != 0) {
        return 1;
      } else
        return 0;
    } else if (tmp[cnt] == '+') {
      cnt++;
      int ri = dfs();
      cnt++;
      if (le == 0 && ri == 0)
        return 0;
      else if (le == 2 || ri == 2)
        return 2;
      else
        return 1;
    }
  } else if (tmp[cnt] == '-') {
    cnt++;
    mminus(dfs());
  } else {
    return tmp[cnt++] - '0';
  }
}
int main() {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          int tm = 0;
          cnt = 0;
          for (int l = 0; l < s.size(); l++) {
            if (s[l] == 'P') {
              tmp[tm++] = i + '0';
            } else if (s[l] == 'Q') {
              tmp[tm++] = j + '0';
            } else if (s[l] == 'R') {
              tmp[tm++] = k + '0';
            } else {
              tmp[tm++] = s[l];
            }
          }
          if (dfs() == 2) ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
