#include <iostream>
#include <string>
#include <stack>

using namespace std;

class Solver {
    public:
        string line;
        int solve() {
            int count = 0;
            for (int p = 0; p < 3; ++p) {
                for (int q = 0; q < 3; ++q) {
                    for (int r = 0; r < 3; ++r) {
                        if (eval(p, q, r) == 2) {
                            ++count;
                        }
                    }
                }
            }
            return count;
        }
        void process(stack<char> &operators, stack<int> &values) {
            int x, y;
            while (!operators.empty()) {
                if (operators.top() == '-') {
                    x = 2 - values.top();
                    values.pop();
                    values.push(x);
                } else if (operators.top() == '+') {
                    y = values.top();
                    values.pop();
                    x = values.top();
                    values.pop();
                    values.push(max(x, y));
                } else if (operators.top() == '*') {
                    y = values.top();
                    values.pop();
                    x = values.top();
                    values.pop();
                    values.push(min(x, y));
                }
                operators.pop();
            }
        }
        int eval(int p, int q, int r, int begin=0, int end=-1) {
            int nest = 0;
            int bracket_begin = -1;
            stack<char> operators;
            stack<int> values;
            if (end < 0) {
                end = line.length();
            }
            for (int i = begin; i < end; ++i) {
                if (line[i] == '(') {
                    if (nest == 0) {
                        bracket_begin = i;
                    }
                    ++nest;
                } else if(line[i] == ')') {
                    --nest;
                    if (nest == 0) {
                        int x = eval(p, q, r, bracket_begin + 1, i);
                        values.push(x);
                        process(operators, values);
                    }
                }
                if (line[i] == '(' || line[i] == ')' || nest > 0) {
                    continue;
                }
                if ('0' <= line[i] && line[i] <= '2') {
                    values.push(line[i] - '0');
                    process(operators, values);
                } else if (line[i] == 'P') {
                    values.push(p);
                    process(operators, values);
                } else if (line[i] == 'Q') {
                    values.push(q);
                    process(operators, values);
                } else if (line[i] == 'R') {
                    values.push(r);
                    process(operators, values);
                } else if (line[i] == '-') {
                    operators.push('-');
                } else if (line[i] == '+') {
                    operators.push('+');
                } else if (line[i] == '*') {
                    operators.push('*');
                }
            }
            return values.top();
        }
};

int main() {
    Solver s;
    while (true) {
        cin >> s.line;
        if (s.line == ".") {
            return 0;
        }
        cout << s.solve() << endl;
    }
}

Measured memory_bytes: 1204