#include <bits/stdc++.h>
using namespace std;
char tmp[100];
int place;
int m_minus(int num) { return 2 - num; }
int m_op(int a, int b, char c) {
  if (c == '*')
    return a & b;
  else
    return a | b;
}
int dfs() {
  if (tmp[place] == '-') {
    place++;
    return m_minus(dfs());
  } else if (tmp[place] == '(') {
    place++;
    int a = dfs();
    char c = tmp[place];
    place++;
    int b = dfs();
    place++;
    return m_op(a, b, c);
  } else {
    int ret = tmp[place] - '0';
    place++;
    return ret;
  }
}
int main() {
  string str;
  while (cin >> str, str[0] != '.') {
    int ans = 0;
    for (long long(i) = (0); (i) < (3); ++i) {
      for (long long(j) = (0); (j) < (3); ++j) {
        for (long long(k) = (0); (k) < (3); ++k) {
          for (long long(l) = (0); (l) < (str.size()); ++l) {
            if (str[l] == 'P') {
              tmp[l] = '0' + i;
            } else if (str[l] == 'Q') {
              tmp[l] = '0' + j;
            } else if (str[l] == 'R') {
              tmp[l] = '0' + k;
            } else
              tmp[l] = str[l];
          }
          place = 0;
          if (dfs() == 2) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
