#include <bits/stdc++.h>
using namespace std;
int Minus(int a) { return 2 - a; }
int Mul(int a, int b) {
  if (a == 0 || b == 0) return 0;
  if (a == 1 || b == 1) return 1;
  return 2;
}
int Add(int a, int b) {
  if (a == 2 || b == 2) return 2;
  if (a == 1 || b == 1) return 1;
  return 0;
}
int p, q, r;
int GetNum(char c) {
  if (c == 'P')
    return p;
  else if (c == 'Q')
    return q;
  else
    return r;
}
int calc(vector<char>& num);
int GetArg(vector<char>& num) {
  char c = num.back();
  num.pop_back();
  if (c == '-' || c == '+' || c == '*') {
    num.push_back(c);
    return calc(num);
  } else if (c == 'P' || c == 'Q' || c == 'R')
    return GetNum(c);
  else
    return c - '0';
}
int calc(vector<char>& num) {
  char c = num.back();
  num.pop_back();
  switch (c) {
    case '-':
      return Minus(GetArg(num));
    case '*':
      return Mul(GetArg(num), GetArg(num));
    case '+':
      return Add(GetArg(num), GetArg(num));
    default:
      num.push_back(c);
      return GetArg(num);
  }
}
void solve(string& str) {
  vector<char> ope, num;
  for (unsigned int i = 0; i < str.size(); i++) {
    char c = str[i];
    switch (c) {
      case '(':
        ope.push_back(c);
        break;
      case ')':
        while (ope.back() != '(') {
          num.push_back(ope.back());
          ope.pop_back();
        }
        ope.pop_back();
        break;
      case '-':
        ope.push_back(c);
        break;
      case '*':
      case '+':
        while (!ope.empty() && ope.back() != '(') {
          num.push_back(ope.back());
          ope.pop_back();
        }
        ope.push_back(c);
        break;
      default:
        num.push_back(c);
    }
  }
  while (!ope.empty()) {
    num.push_back(ope.back());
    ope.pop_back();
  }
  string po(num.begin(), num.end());
  cout << po << endl;
  int count = 0;
  for (p = 0; p < 3; p++)
    for (q = 0; q < 3; q++)
      for (r = 0; r < 3; r++) {
        vector<char> tmp(num.begin(), num.end());
        if (calc(tmp) == 2) count++;
      }
  cout << count << endl;
}
int main() {
  string str;
  while (cin >> str, str[0] != '.') {
    solve(str);
  }
  return 0;
}
