#include <bits/stdc++.h>
struct LOGIC {
  char formula[81];
  char v[3];
};
int Main();
char Calc(struct LOGIC *logic);
int Not(int a);
int And(int a, int b);
int Or(int a, int b);
int main(int argc, char *argv[]) {
  while (Main() == 0)
    ;
  return 0;
}
int Main() {
  struct LOGIC logic;
  int num = 0, count = 0, i = 0;
  for (i = 0; i < 81; i++) {
    logic.formula[i] = '\0';
  }
  if (scanf("%s", logic.formula) != 1) {
    return -1;
  }
  if (logic.formula[0] == '.') {
    return 1;
  }
  printf("%s\n", logic.formula);
  for (logic.v[0] = '0'; logic.v[0] <= '2'; logic.v[0]++) {
    for (logic.v[1] = '0'; logic.v[1] <= '2'; logic.v[1]++) {
      for (logic.v[2] = '0'; logic.v[2] <= '2'; logic.v[2]++) {
        num = 0;
        count += (Calc(&logic) == '2');
      }
    }
  }
  printf("%d\n", count);
  return 0;
}
char Calc(struct LOGIC *logic) {
  int a[2], sc = 0, i = 0;
  char stack[81], s = '\0', ope = '\0';
  for (i = 0; i < 81; i++) {
    stack[i] = '\0';
  }
  for (i = 0; logic->formula[i] != '\0'; i++) {
    if ('P' <= logic->formula[i] && logic->formula[i] <= 'R') {
      stack[sc++] = logic->v[logic->formula[i] - 'P'];
    } else {
      stack[sc++] = logic->formula[i];
    }
    if (logic->formula[i] == ')') {
      for (s = stack[--sc]; s != '+' && s != '*'; s = stack[--sc]) {
        stack[sc + 1] = '\0';
        if ('P' <= s && s <= 'R') {
          a[1] = logic->v[s - 'P'] + '0';
        } else if ('0' <= s && s <= '2') {
          a[1] = s;
        } else if (s == '-') {
          a[1] = Not(a[1]);
        }
      }
      stack[sc + 1] = '\0';
      ope = stack[sc];
      for (s = stack[--sc]; stack[sc] != '('; s = stack[--sc]) {
        stack[sc + 1] = '\0';
        if ('P' <= s && s <= 'R') {
          a[0] = logic->v[s - 'P'] + '0';
        } else if ('0' <= s && s <= '2') {
          a[0] = s;
        } else if (s == '-') {
          a[0] = Not(a[0]);
        }
      }
      stack[sc + 1] = '\0';
      if (ope == '*') {
        stack[sc++] = And(a[0], a[1]);
      } else if (ope == '+') {
        stack[sc++] = Or(a[0], a[1]);
      }
    }
  }
  a[0] = stack[--sc];
  for (sc--; sc >= 0; sc--) {
    if (stack[sc] == '-') {
      a[0] = Not(a[0]);
    }
  }
  stack[++sc] = a[0];
  return stack[0];
}
int Not(int a) {
  switch (a) {
    case '0':
      return '2';
    case '1':
      return '1';
    case '2':
      return '0';
  }
  return -1;
}
int And(int a, int b) {
  if (a == -1 || b == -1) {
    return -1;
  }
  if (a == '0' || b == '0') {
    return '0';
  } else if (a == '1' || b == '1') {
    return '1';
  } else if (a == '2' && b == '2') {
    return '2';
  }
  return -1;
}
int Or(int a, int b) {
  if (a == -1 || b == -1) {
    return -1;
  }
  if (a == '2' || b == '2') {
    return '2';
  } else if (a == '1' || b == '1') {
    return '1';
  } else if (a == '0' && b == '0') {
    return '0';
  }
  return -1;
}
