using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Contest
{
    class Program
    {
        static int Main(string[] args)
        {
            new Program().Process();

            return 0;
        }

        private CommonInput cin;

        private void Process()
        {
            cin = new CommonInput();

            string formula;

            int inNum, count;
            bool inP, inQ, inR;

            while (true)
            {
                formula = cin.GetNext();

                // ??\????????????????????¨???????????????????????????????????¨???????????¨??????????????\??????????????????
                if (formula == ".")
                    break;

                inNum = count = 0;

                inP = formula.Contains("P");
                inQ = formula.Contains("Q");
                inR = formula.Contains("R");

                for (int i = inP ? 0 : 2; i < 3; i++)
                {
                    for (int j = inQ ? 0 : 2; j < 3; j++)
                    {
                        for (int k = inR ? 0 : 2; k < 3; k++)
                        {
                            if (Calc(formula.Replace("P", i.ToString()).Replace("Q", j.ToString()).Replace("R", k.ToString())) == 2)
                                count++;
                        }
                    }
                }

                if (!inP)
                    count *= 3;
                if (!inQ)
                    count *= 3;
                if (!inR)
                    count *= 3;

                Console.WriteLine(count);
            }
        }

        private int Not(string x)
        {
            if (x[0] == '-')
                return 2 - Not(x.Remove(0, 1));
            else if (x[0] == '(')
                return Calc(x);
            else
                return int.Parse(x);
        }

        private int Calc(string x)
        {
            int count = 0;
            int i = 0;
            int j = 0;
            StringBuilder tmp = new StringBuilder();
            string f1, f2;
            f1 = f2 = "";
            Func<string, string, int> func = new Func<string, string, int>((a, b) => 0);

            if (x[0] == '(')
            {
                count++;
                i = 1;

                while (count > 0)
                {
                    if (count == 1 && x[i] == '+')
                    {
                        f1 = tmp.ToString();
                        tmp.Clear();
                        func = new Func<string, string, int>((a, b) => Or(a, b));
                        j = i + 2;
                    }
                    else if (count == 1 && x[i] == '*')
                    {
                        f1 = tmp.ToString();
                        tmp.Clear();
                        func = new Func<string, string, int>((a, b) => And(a, b));
                        j = i;
                    }
                    else
                    {
                        if (x[i] == '(')
                            count++;
                        else if (x[i] == ')')
                            count--;

                        tmp.Append(x[i]);
                    }

                    i++;
                }

                f2 = tmp.ToString();
                f2 = f2.Remove(f2.Length - 1, 1);

                return func(f1, f2);
            }
            else
            {
                return Not(x);
            }
        }

        private int And(string x, string y)
        {
            int xx, yy;

            if (x[0] == '(')
                xx = Calc(x);
            else
                xx = Not(x);

            if (y[0] == '(')
                yy = Calc(y);
            else
                yy = Not(y);

            return xx < yy ? xx : yy;
        }

        private int Or(string x, string y)
        {
            int xx, yy;

            if (x[0] == '(')
                xx = Calc(x);
            else
                xx = Not(x);

            if (y[0] == '(')
                yy = Calc(y);
            else
                yy = Not(y);

            return xx > yy ? xx : yy;
        }
    }

    public class CommonInput
    {
        public CommonInput()
        {
            this._s = new string[0];
            this._i = 0;
        }

        public string Line { get; set; }
        private string[] _s;
        private int _i;
        private char[] _sep = { ' ' };

        public void GetLine()
        {
            this._i = 0;
            do
            {
                this.Line = Console.ReadLine();
            } while (this.Line == "");

            if (this.Line != null)
                this._s = this.Line.Split(this._sep, StringSplitOptions.RemoveEmptyEntries);
        }

        public string GetNext()
        {
            if (this._i < this._s.Length)
                return this._s[this._i++];

            this.GetLine();

            return this._s[this._i++];
        }

        public int GetNextToInt()
        {
            return int.Parse(this.GetNext());
        }

        public long GetNextToLong()
        {
            return long.Parse(this.GetNext());
        }

        public double GetNextToDouble()
        {
            return double.Parse(this.GetNext());
        }
    }
}