#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, 0, -1, 1, -1, -1, 1};
const int dy[] = {0, 1, -1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-8;
int expr(string&, int&);
int term(string&, int&);
int number(string&, int&);
int number(string& s, int& i) {
  int c = s[i] - '0';
  i++;
  return c;
}
int term(string& s, int& i) {
  if (isdigit(s[i])) return number(s, i);
  if (s[i] == '-') {
    while (s[i] == '-') {
      int val;
      i++;
      val = term(s, i);
      if (val == 2) {
        val = 0;
      } else if (val == 0) {
        val = 2;
      }
      return val;
    }
  }
  i++;
  int res = expr(s, i);
  i++;
  return res;
}
int expr(string& s, int& i) {
  int val = term(s, i);
  while (s[i] == '*' || s[i] == '+') {
    char op = s[i];
    i++;
    int val2 = term(s, i);
    if (op == '*') {
      if (val == 0 || val2 == 0) {
        val = 0;
      } else if (val == 1 || val2 == 1) {
        val = 1;
      } else {
        val = 2;
      }
    } else if (op == '+') {
      if (val == 2 || val2 == 2) {
        val = 2;
      } else if (val == 1 || val2 == 1) {
        val = 1;
      } else {
        val = 0;
      }
    }
  }
  return val;
}
map<string, int> memo;
map<string, int> used;
int solve(string str, int depth) {
  if (memo[str]) return memo[str];
  if (depth == 3) {
    used[str] = 1;
    int idx = 0;
    int res = expr(str, idx);
    if (res == 2) {
      memo[str] = 1;
    }
    return memo[str];
  }
  string a, b, c;
  for (int i = (0); i < (str.size()); ++i) {
    if (str[i] == 'P' + depth) {
      a += "0";
      b += "1";
      c += "2";
    } else {
      a += str[i];
      b += str[i];
      c += str[i];
    }
  }
  memo[str] = solve(a, depth + 1) + solve(b, depth + 1) + solve(c, depth + 1);
  return memo[str];
}
int main() {
  string str;
  while (true) {
    cin >> str;
    if (str == ".") {
      break;
    }
    printf("%d\n", solve(str, 0));
  }
  return 0;
}
