#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++) v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};
const int wa[3][3]{{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
const int se[3][3]{{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int calc(string& s, int& idx);
int brac(string& s, int& idx);
int number(string& s, int& idx);
int calc(string& s, int& idx) {
  int val1 = brac(s, idx);
  while (s[idx] == '*' || s[idx] == '+') {
    idx++;
    if (s[idx - 1] == '+') {
      int val2 = brac(s, idx);
      val1 = wa[val1][val2];
    } else {
      int val2 = brac(s, idx);
      val1 = se[val1][val2];
    }
  }
  return val1;
}
int brac(string& s, int& idx) {
  if (isdigit(s[idx]) || s[idx] == '-') return number(s, idx);
  idx++;
  int val = calc(s, idx);
  idx++;
  return val;
}
int number(string& s, int& idx) {
  int re = 0;
  while (s[idx] == '-') {
    re++;
    idx++;
  }
  int val = s[idx] - '0';
  idx++;
  if (re % 2 == 1)
    if (val == 0)
      val = 2;
    else if (val == 2)
      val = 0;
  return val;
}
int main() {
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    for (int i = 0; i < (int)(3); ++i) {
      for (int j = 0; j < (int)(3); ++j) {
        for (int k = 0; k < (int)(3); ++k) {
          string s2 = s;
          replace(s2.begin(), s2.end(), 'P', (char)('0' + i));
          replace(s2.begin(), s2.end(), 'Q', (char)('0' + j));
          replace(s2.begin(), s2.end(), 'R', (char)('0' + k));
          int num = 0;
          if (calc(s2, num) == 2) ans++;
        }
      }
    }
    cout << ans << endl;
  }
}
