#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <string>

using namespace std;

struct Node {
    char v;
    Node* l = nullptr;
    Node* r = nullptr;
    Node(char c)
        : v{c}
    {
    }
};

int main()
{
    while (true) {
        char ss[100];
        scanf("%s", ss);
        if (ss[0] == '.') {
            break;
        }
        string s{ss};
        int n = 0;
        function<Node*(const string&)> parse = [&parse, &n](const string& s) {
            // printf("%s\n", s.c_str() + n);
            auto ss = string("012PQR");
            if (ss.find(s[n]) != string::npos) {
                auto a = new Node(s[n]);
                n += 1;
                return a;
            }
            else if (s[n] == '-') {
                auto a = new Node('-');
                n += 1;
                a->l = parse(s);
                return a;
            }
            else if (s[n] == '(') {
                n += 1;
                auto l = parse(s);
                // printf("left %c\n", l->v);
                auto v = new Node(s[n]);
                n += 1;
                auto r = parse(s);
                n += 1;
                v->l = l;
                v->r = r;
                return v;
            }
            return new Node('!');
        };
        auto r = parse(s);
        function<void(Node*)> p = [&p](Node* n) {
            if (!n) {
                return;
            }
            if (n->v == '+') {
                putchar('(');
                p(n->l);
                putchar('+');
                p(n->r);
                putchar(')');
            }
            else if (n->v == '*') {
                putchar('(');
                p(n->l);
                putchar('*');
                p(n->r);
                putchar(')');
            }
            else if (n->v == '-') {
                putchar('-');
                p(n->l);
            }
            else {
                putchar(n->v);
            }
        };
        auto pn = [&p](Node* n) {
            printf("result: ");
            p(n);
            putchar('\n');
        };
        // pn(r);

        function<int(const Node*, int, int, int)> f = [&f](const Node* n, int p,
                                                           int q, int r) {
            auto and_ = [](int lhs, int rhs) {
                if (lhs == 0 || rhs == 0) {
                    return 0;
                }
                else if (lhs == 1 || rhs == 1) {
                    return 1;
                }
                else {
                    return 2;
                }
            };
            auto not_ = [](int lhs) {
                if (lhs == 0) {
                    return 2;
                }
                else if (lhs == 2) {
                    return 0;
                }
                else {
                    return 1;
                }
            };
            auto or_ = [](int lhs, int rhs) {
                if (lhs == 2 || rhs == 2) {
                    return 2;
                }
                else if (lhs == 1 || rhs == 1) {
                    return 1;
                }
                else {
                    return 0;
                }
            };
            if (n == nullptr) {
                return 2;
            }
            else if (n->v == '*') {
                return and_(f(n->l, p, q, r), f(n->r, p, q, r));
            }
            else if (n->v == '+') {
                return or_(f(n->l, p, q, r), f(n->r, p, q, r));
            }
            else if (n->v == '-') {
                return not_(f(n->l, p, q, r));
            }
            else if (n->v == 'P') {
                return p;
            }
            else if (n->v == 'Q') {
                return q;
            }
            else if (n->v == 'R') {
                return r;
            }

            return n->v - '0';
        };
        auto count = 0;
        for (auto i = 0; i <= 2; ++i) {
            for (auto j = 0; j <= 2; ++j) {
                for (auto k = 0; k <= 2; ++k) {
                    auto rr = f(r, i, j, k);
                    // printf("%d %d %d %d\n", i, j, j, rr);
                    if (rr == 2) {
                        ++count;
                    }
                }
            }
        }
        printf("%d\n", count);
    }
}

Measured memory_bytes: 2668