#include <functional>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cctype>
using namespace std;
using node_t = function<int(int[3])>;
string S;
size_t cur = 0;

node_t parse() {
    char c = S[cur++];
    if (isdigit(c)) return [=](int[3]) { return c - '0'; };     // 定数関数
    if (isalpha(c)) return [=](int a[3]) { return a[c - 'P']; };
    node_t left = parse();
    if (c == '-') return [=](int a[3]) { return 2 - left(a); };             // NOT
    assert(c == '(');
    char op = S[cur++];
    node_t right = parse();
    ++cur;  // skip ')'
    if (op == '*') return [=](int a[3]) { return min(left(a), right(a)); }; // AND
    return [=](int a[3]) { return max(left(a), right(a)); };                // OR
}

int main() {
    while (cin >> S, S != ".") {
        int cnt = 0;
        cur = 0;
        auto tree = parse();
        for (int p : {0, 1, 2}) {
            for (int q : {0, 1, 2}) {
                for (int r : {0, 1, 2}) {
                    int a[] = {p, q, r};
                    if (tree(a) == 2) cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
