#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

int solve(string expr, int p, int q, int r) {
    int k = 0;
    function<int(void)> calc = [&]() {
        if (isdigit(expr[k])) return expr[k++] - '0';
        if (expr[k] == '-') { k++; return 2 - calc(); }
        if (expr[k] == '(') {
            k++;
            int x = calc();
            bool plus = expr[k++] == '+';
            int y = calc();
            k++;
            return plus ? max(x, y) : min(x, y);
        }
        return -1;
    };
    for (auto& e : expr) {
        switch (e) {
            case 'P': e = p + '0'; break;
            case 'Q': e = q + '0'; break;
            case 'R': e = r + '0'; break;
        }
    }
    return calc() == 2;
}

int main() {
    for (string expr; cin >> expr, expr != ".";) {
        int cnt = 0;
        rep(p, 3) rep(q, 3) rep(r, 3) if (solve(expr, p, q, r)) cnt++;
        cout << cnt << endl;
    }
}