// Slower C++ solution:
#include <algorithm>

#include <cmath>

#include <climits>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <fstream>

#include <iostream>

#include <list>

#include <map>

#include <queue>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <vector>



using namespace std;



#define LOG(...) printf(__VA_ARGS__)

//#define LOG(...)

#define FOR(i,a,b) for(int i=(a);i<(b);++i)

#define REP(i,n) for(int i=0;i<(int)(n);++i)

#define ALL(a) (a).begin(),(a).end()

#define EXIST(s,e) ((s).find(e)!=(s).end())

#define SORT(c) sort((c).begin(),(c).end())



typedef long long ll;

typedef unsigned long long ull;

typedef vector<bool> vb;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef vector<vb> vvb;

typedef vector<vi> vvi;

typedef vector<vll> vvll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;



#define MAX_VAL 26



std::string ReplaceString(std::string& subject, const std::string& search,

                          const std::string& replace) {

    size_t pos = 0;

    while ((pos = subject.find(search, pos)) != std::string::npos) {

         subject.replace(pos, search.length(), replace);

         pos += replace.length();

    }

    return subject;

}



int main() {

    string expr;

    while (getline(cin, expr), expr != ".") {

        ReplaceString(expr, "--", "");



        string expr_copy = expr;

        int count = 0;

        for (int v = 0; v <= 26; v++) {

            expr = expr_copy;

            int w = v;

            int p = w % 3; w /= 3;

            int q = w % 3; w /= 3;

            int r = w % 3; w /= 3;



            REP(i, expr.size()) {

                switch (expr[i]) {

                case 'P':

                    expr[i] = '0' + p;

                    break;

                case 'Q':

                    expr[i] = '0' + q;

                    break;

                case 'R':

                    expr[i] = '0' + r;

                    break;

                }

            }



            while (expr.size() > 1) {

                stack<int> parens;

                REP(i, expr.size()) {

                    // NOT

                    ReplaceString(expr, "-0", "2");

                    ReplaceString(expr, "-1", "1");

                    ReplaceString(expr, "-2", "0");

                    if (expr[i] == '(') {

                        parens.push(i);

                        continue;

                    } else if (expr[i] == ')') {

                        int j = parens.top(); parens.pop();

                        int a = expr[j+1] - '0';

                        int b = expr[j+3] - '0';

                        string ret;

                        if (expr[j+2] == '+') {

                            // OR

                            if (a == b) ret = a + '0';

                            else ret = a + b >= 2 ? "2" : "1";

                        } else {

                            // AND

                            if (a == 2 && b == 2) ret = "2";

                            else ret = a * b > 0 ? "1" : "0";

                        }

                        string e = string(expr.begin() + j, expr.begin() + i + 1);

                        ReplaceString(expr, e, ret);

                        break;

                    }

                }

            }



            if (expr == "2") {

                count++;

            }

        }

        cout << count << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 1.3518952987642066x):
#include <algorithm>

#include <cmath>

#include <climits>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <fstream>

#include <iostream>

#include <list>

#include <map>

#include <queue>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <vector>



using namespace std;



#define LOG(...) printf(__VA_ARGS__)

//#define LOG(...)

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

#define ALL(a) (a).begin(), (a).end()

#define RALL(a) (a).rbegin(), (a).rend()

#define EXIST(s, e) ((s).find(e) != (s).end())

#define SORT(c) sort((c).begin(), (c).end())

#define RSORT(c) sort((c).rbegin(), (c).rend())

#define CLR(a) memset((a), 0, sizeof(a))



typedef long long ll;

typedef unsigned long long ull;

typedef vector<bool> vb;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef vector<vb> vvb;

typedef vector<vi> vvi;

typedef vector<vll> vvll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;



string expr;

int idx;

int p, q, r;



int form();



int paren() {

    int left = form();

    char op = expr[idx++];

    int right = form();

    idx++; // ')'



    if (left == right) {

        return left;

    }

    if (op == '+') {

        return (left + right >= 2) ? 2 : 1;

    } else {

        return (left * right > 0) ? 1 : 0;

    }

}



int form() {

    char c = expr[idx++];

    switch (c) {

    case '0':

    case '1':

    case '2':

        return c - '0';

    case 'P': return p;

    case 'Q': return q;

    case 'R': return r;

    case '-':

        return 2 - form();

    case '(':

        return paren();

    }

}



int main() {

    while (cin >> expr, expr != ".") {

        int cnt = 0;

        REP(i, 27) {

            idx = 0;

            int w = i;

            p = w % 3; w /= 3;

            q = w % 3; w /= 3;

            r = w % 3; w /= 3;

            int n = form();

            if (n == 2) cnt++;

        }

        cout << cnt << endl;

    }

}