#include <bits/stdc++.h>
using namespace std;
char ss[1 << 10];
int n;
int F(int x);
const int M = 32768;
struct Parse {
  enum SIGN { le, gr, eq };
  int nn;
  int pos;
  char c() { return pos < n ? ss[pos] : 0; }
  void next() {
    if (pos < n) pos++;
  }
  void next(int k) {
    for (int i = 0; i < (int)(k); ++i) next();
  }
  void snext(int k) {
    skip();
    next(k);
  }
  void skip() {
    while (c() <= 32 && c() != 0) next();
  }
  Parse(int nn) : nn(nn) { pos = 0; }
  int _add(int a, int b) { return (a + b) % M; }
  int _sub(int a, int b) { return ((a - b) % M + M) % M; }
  int _mul(int a, int b) { return (a * b) % M; }
  int _div(int a, int b) {
    if (b == 0)
      return 0;
    else
      return a / b;
  }
  int multiplier() {
    skip();
    int res = 0;
    char f = c();
    if (f == 'n') {
      next();
      res = nn;
    } else if (f == 'f') {
      snext(1);
      snext(1);
      int arg = sum();
      snext(1);
      if (arg >= nn)
        res = 0;
      else
        res = F(arg);
    } else {
      while (c() >= '0' && c() <= '9') {
        res = res * 10 + c() - '0';
        next();
      }
    }
    skip();
    return res;
  }
  int product() {
    skip();
    int res = multiplier();
    for (;;) {
      char sign = c();
      if (sign == '*') {
        next();
        res = _mul(res, multiplier());
      } else if (sign == '/') {
        next();
        res = _div(res, multiplier());
      } else
        break;
    }
    skip();
    return res;
  }
  int sum() {
    skip();
    int res = product();
    for (;;) {
      char sign = c();
      if (sign == '+') {
        next();
        res = _add(res, product());
      } else if (sign == '-') {
        next();
        res = _sub(res, product());
      } else
        break;
    }
    skip();
    return res;
  }
  int Get() {
    snext(3);
    snext(1);
    snext(1);
    snext(3);
    snext(1);
    snext(1);
    snext(1);
    skip();
    while (c() != '}') {
      if (c() == 'r') {
        snext(6);
        int res = sum();
        snext(1);
        return res;
      } else {
        snext(2);
        snext(1);
        int a = sum();
        SIGN sign;
        if (c() == '<') {
          sign = le;
          snext(1);
        } else if (c() == '>') {
          sign = gr;
          snext(1);
        } else {
          sign = eq;
          snext(2);
        }
        int b = sum();
        snext(1);
        snext(6);
        int res = -1;
        try {
          res = sum();
        } catch (...) {
          res = -1;
        }
        snext(1);
        if (sign == le && a < b) return res;
        if (sign == gr && a > b) return res;
        if (sign == eq && a == b) return res;
      }
      skip();
    }
  }
};
int mem[1 << 16];
int F(int x) {
  int& res = mem[x];
  if (res == -1) {
    Parse p(x);
    res = p.Get();
  }
  return res;
}
int main() {
  memset(mem, -1, sizeof(mem));
  int x;
  gets(ss);
  sscanf(ss, "%d", &x);
  n = 0;
  for (;;) {
    if (!gets(ss + n))
      break;
    else
      n += strlen(ss + n);
  }
  int res = -1;
  for (int i = 32768 - 1; i >= 0; i--) {
    if (F(i) == x) {
      res = i;
      break;
    }
  }
  printf("%d\n", res);
  return 0;
}
