#include <bits/stdc++.h>
using namespace std;
int nottable[3] = {2, 1, 0};
int andtable[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int ortable[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
char tochar(int num) {
  if (num == 0) {
    return '0';
  } else if (num == 1) {
    return '1';
  } else {
    return '2';
  }
}
int solve(string::const_iterator iter) {
  int ret;
  if (*iter == '0') {
    ret = 0;
  } else if (*iter == '1') {
    ret = 1;
  } else if (*iter == '2') {
    ret = 2;
  } else if (*iter == '-') {
    iter++;
    return nottable[solve(iter)];
  } else if (*iter == '(') {
    iter++;
    ret = solve(iter);
    if (*iter == '+') {
      iter++;
      ret = ortable[ret][solve(iter)];
    } else if (*iter == '*') {
      iter++;
      ret = andtable[ret][solve(iter)];
    }
  }
  iter++;
  return ret;
}
int main() {
  while (true) {
    string str;
    cin >> str;
    if (str == ".") {
      break;
    }
    int ans = 0;
    for (int i = 0; i <= 2; i++) {
      for (int j = 0; j <= 2; j++) {
        for (int k = 0; k <= 2; k++) {
          string temp = str;
          for (int l = 0; l < temp.size(); l++) {
            if (temp[l] == 'P') {
              temp[l] = tochar(i);
            } else if (temp[l] == 'Q') {
              temp[l] = tochar(j);
            } else if (temp[l] == 'R') {
              temp[l] = tochar(k);
            }
          }
          if (solve(temp.begin()) == 2) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
