#include <bits/stdc++.h>
char s[1000];
int slen = 0;
struct T {
  char l1[150];
  char l2[150];
  char ans[150];
  int operat;
};
T rr[1000];
int mem[1000000];
int j = 0;
int rec(int n);
int ans(char* s, int n) {
  int l = 0;
  int a = 0;
  int b = -1;
  int c = 1;
  int ll = 0;
  int last = 0;
  ;
  int ele = 0;
  while (*s) {
    ele++;
    l = 0;
    if (*s == 'n') {
      l = n;
      *s++;
    } else if (*s == 'f') {
      char m[100];
      int mn = 0;
      s += 2;
      char* r = s;
      int level = 1;
      while (level > 0) {
        if (*s == ')') level--;
        if (*s == '(') level++;
        m[s - r] = *s;
        s++;
      }
      m[s - r - 1] = 0;
      l = rec(ans(m, n));
    } else
      while (*s != '*' && *s != '/' && *s != '+' && *s != '-' && *s != 0) {
        l *= 10;
        l += *s - '0';
        s++;
      }
    if (last == 1) {
      b *= l;
      b %= 32768;
    }
    if (last == 2) {
      b /= l;
      b %= 32768;
    }
    if (b == -1) b = l;
    switch (*s) {
      case '*':
        last = 1;
        if (b == -1) b = l;
        s++;
        break;
      case '-':
        a += b * c;
        c = -1;
        b = -1;
        s++;
        last = 0;
        break;
      case '/':
        last = 2;
        s++;
        break;
      case '+':
        if (b == -1) b = l;
        a += b * c;
        c = 1;
        b = -1;
        s++;
        last = 0;
        break;
    }
  }
  if (last == 0) b = l;
  return (a + b * c + 32768) % 32768;
}
int rec(int n) {
  if (mem[n] == -1)
    for (int i = 0; i < j; i++) {
      if (rr[i].operat == 0) {
        mem[n] = ans(rr[i].ans, n);
        break;
      };
      if (rr[i].operat == 1) {
        int a = ans(rr[i].l1, n);
        int b = ans(rr[i].l2, n);
        if (a == b) {
          mem[n] = ans(rr[i].ans, n);
          break;
        }
      };
      if (rr[i].operat == 2) {
        int a = ans(rr[i].l1, n);
        int b = ans(rr[i].l2, n);
        if (a > b) {
          mem[n] = ans(rr[i].ans, n);
          break;
        }
      };
      if (rr[i].operat == 3) {
        int a = ans(rr[i].l1, n);
        int b = ans(rr[i].l2, n);
        if (a < b) {
          mem[n] = ans(rr[i].ans, n);
          break;
        }
      };
    }
  return mem[n];
}
int main() {
  memset(mem, 0xFF, sizeof(mem));
  int n;
  scanf("%d", &n);
  char c = 0;
  while (c != '{') {
    scanf("%c", &c);
  }
  scanf("%c", &c);
  while (c != '}') {
    if (c != '\n') {
      s[slen] = c;
      slen++;
    }
    scanf("%c", &c);
  }
  s[slen] = 0;
  char* ss = s;
  while (ss) {
    while (*ss == ' ' || *ss == '\n') ss++;
    if (*ss == 'i') {
      while (*ss != '(') ss++;
      ss++;
      int q = 0;
      while (*ss != '=' && *ss != '<' && *ss != '>') {
        if (*ss != ' ') {
          rr[j].l1[q] = *ss;
          q++;
        }
        ss++;
      }
      rr[j].l1[q] = 0;
      if (*ss == '=') {
        rr[j].operat = 1;
        ss += 2;
      }
      if (*ss == '<') {
        rr[j].operat = 3;
        ss++;
      }
      if (*ss == '>') {
        rr[j].operat = 2;
        ss++;
      }
      q = 0;
      int level = 1;
      while (level > 0) {
        if (*ss == ')') level--;
        if (*ss == '(') level++;
        if (level > 0)
          if (*ss != ' ') {
            rr[j].l2[q] = *ss;
            q++;
          }
        ss++;
      }
      rr[j].l2[q] = 0;
      q = 0;
      while (*ss != 'r') ss++;
      ss += 6;
      while (*ss != ';') {
        if (*ss != ' ') {
          rr[j].ans[q] = *ss;
          q++;
        }
        ss++;
      }
      ss++;
      rr[j].ans[q] = 0;
    } else if (*ss == 'r') {
      ss += 6;
      rr[j].operat = 0;
      int q = 0;
      while (*ss != ';') {
        if (*ss != ' ') {
          rr[j].ans[q] = *ss;
          q++;
        }
        ss++;
      }
      ss++;
      rr[j].ans[q] = 0;
      rr[j].l1[0] = 0;
      rr[j].l2[0] = 0;
    } else if (*ss == 0)
      break;
    j++;
  }
  for (int i = 0; i < (1 << 15); i++) {
    rec(i);
  }
  int max = -1;
  for (int i = 0; i < (1 << 15); i++) {
    if (mem[i] == n) {
      max = i;
    }
  }
  printf("%d", max);
}
