#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <iomanip>
#include <numeric>
#include <queue>
#include <climits>
#include <set>
#include <complex>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define INF 1LL << 59
using ld = long double;
const int MAX = 510000;

int t[3] = {2, 1, 0};

int seki[3][3] = {{0, 0, 0},
                  {0, 1, 1},
                  {0, 1, 2}};

int wa[3][3] = {
    {0, 1, 2},
    {1, 1, 2},
    {2, 2, 2}};
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    //cout << fixed << setprecision(5);

    string s;
    while (cin >> s)
    {
        if (s == ".")
            break;

        int n = s.size();
        int ans = 0;
        for (int i = 0; i < 3; ++i)
        { // P
            for (int j = 0; j < 3; ++j)
            { // Q
                for (int k = 0; k < 3; ++k)
                { // R
                    stack<int> c;
                    stack<char> ope;
                    for (int len = 0; len < n; ++len)
                    {
                        if (s[len] == '-' || s[len] == '(' || s[len] == '*' || s[len] == '+')
                        {
                            ope.push(s[len]);
                        }
                        else if (s[len] != ')')
                        {
                            bool jud = true;
                            while (ope.size() > 0 && ope.top() == '-')
                            {
                                if (jud == true)
                                    jud = false;
                                else
                                    jud = true;
                                ope.pop();
                            }
                            if (s[len] == 'P')
                            {
                                if (!jud)
                                    c.push(t[i]);
                                else
                                    c.push(i);
                            }
                            else if (s[len] == 'Q')
                            {
                                if (!jud)
                                    c.push(t[j]);
                                else
                                    c.push(j);
                            }
                            else if (s[len] == 'R')
                            {
                                if (!jud)
                                    c.push(t[k]);
                                else
                                    c.push(k);
                            }
                            else
                            {
                                if (!jud)
                                    c.push(t[(int)s[len] - '0']);
                                else
                                    c.push((int)s[len] - '0');
                            }
                        }
                        else if (s[len] == ')')
                        {
                            char opt = ope.top();
                            ope.pop();
                            int a = c.top();
                            c.pop();
                            int b = c.top();
                            c.pop();
                            int num = 0;
                            if (opt == '+')
                            {
                                num = wa[b][a];
                            }
                            else if (opt == '*')
                            {
                                num = seki[b][a];
                            }
                            bool jud = true;
                            ope.pop();
                            while (ope.size() > 0 && ope.top() == '-')
                            {
                                jud = jud ? false : true;
                                ope.pop();
                            }

                            num = jud ? num : t[num];
                            c.push(num);
                        }
                        //cout << ope.size() << " " << ope.top() << endl;
                    }

                    if (c.top() == 2)
                        ans++;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 3112