#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
string s;
char now;
string ans;
char op1 = '*', op2 = '+', op3 = '-';
bool isSymbol(char c) {
  return c == '0' || c == '1' || c == '2' || c == 'P' || c == 'Q' || c == 'R';
}
void replaceAll(string &s, char c, int num) {
  for (int i = 0; i < (int)s.size(); ++i) {
    if (s[i] == c) s[i] = (char)(num + '0');
  }
}
void formula() {
  now = s[cnt];
  if (now == '(') {
    ++cnt;
    formula();
    if (s[cnt] == op1) {
      cnt++;
      formula();
      ++cnt;
      ans += op1;
    }
    if (s[cnt] == op2) {
      cnt++;
      formula();
      ++cnt;
      ans += op2;
    }
  } else if (isSymbol(now)) {
    ans += now;
    ++cnt;
  } else if (now == op3) {
    ++cnt;
    formula();
    ans += op3;
  }
}
int my_and[][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
int my_or[][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
int my_not[] = {2, 1, 0};
int eval(int x1, int x2, char op) {
  ;
  if (op == op2) return my_and[x1][x2];
  if (op == op1) return my_or[x1][x2];
  if (op == op3) return my_not[x1];
  return -1;
}
stack<int> st;
bool isNum(char c) { return c == '0' || c == '1' || c == '2'; }
int evalAll(string &s) {
  for (int i = 0; i < (int)s.size(); ++i) {
    ;
    if (isNum(s[i])) {
      st.push(s[i] - '0');
    } else if (s[i] == op3) {
      int v = eval(st.top(), -1, op3);
      st.pop();
      st.push(v);
    } else {
      int x1 = st.top();
      st.pop();
      int x2 = st.top();
      st.pop();
      int v = eval(x1, x2, s[i]);
      st.push(v);
    }
  }
  int top = st.top();
  st.pop();
  return top;
}
int ansN = 0;
void init() {
  ansN = 0;
  ans = "";
  st = stack<int>();
  cnt = 0;
  s = "";
}
void solve() {
  while (cin >> s, s != ".") {
    formula();
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        for (int k = 0; k < 3; ++k) {
          string ans1 = ans;
          replaceAll(ans1, 'P', i);
          replaceAll(ans1, 'Q', j);
          replaceAll(ans1, 'R', k);
          if (evalAll(ans1) == 2) {
            ++ansN;
          }
        }
      }
    }
    cout << ansN << endl;
    init();
  }
}
void test() {
  string s = "aaaaaa";
  s.insert(2, "b");
  s.erase(2, 1);
  ;
}
void test2() {
  string a = "01+";
  ;
}
void test3() {
  string a = "PQ+";
  replaceAll(a, 'P', 2);
  replaceAll(a, 'Q', 1);
  cerr << evalAll(a) << endl;
}
void test4() {
  string s = "2";
  cerr << evalAll(s) << endl;
}
void test5() {
  s = "(P*-P)";
  formula();
  cerr << ans << endl;
}
int main() {
  solve();
  return 0;
}
