#include <bits/stdc++.h>
using namespace std;

typedef string::iterator State;
typedef map<char, int> Env;

int formula(State& state, Env env) {
    if ('0' <= *state && *state <= '2') {
        return *state++ - '0';
    }

    if ('P' <= *state && *state <= 'R') {
        return env[*state++];
    }

    if (*state == '-') {
        ++state;
        return 2 - formula(state, env);
    }

    ++state; // '('
    int lhs = formula(state, env);
    char op = *state++;
    int rhs = formula(state, env);
    ++state; // ')'

    if (op == '+') {
        return max(lhs, rhs);
    } else if (op == '*') {
        return min(lhs, rhs);
    }

    assert(false);
}


int main() {
    string expr;
    Env env;
    while (true) {
        cin >> expr;

        if (expr == ".") {
            break;
        }

        int count = 0;

        for (int j = 0; j < 27; ++j) {
            env['P'] = j % 3;
            env['Q'] = (j / 3) % 3;
            env['R'] = j / 9;

            State state = expr.begin();
            if (formula(state, env) == 2) {
                ++count;
            }
        }
        
        cout << count << endl;
    }
    return 0;
}