#include <bits/stdc++.h>
using namespace std;
char* read(char* a, const char* str) {
  while (*a == ' ') {
    a++;
  }
  a += strlen(str);
  return a;
}
char buf[1000];
struct sum;
int eval(sum*, int);
int f(int n);
sum* createSum(char*, char*, char**);
const int M = 32768;
struct logicalExpr {
  char op = 0;
  sum* s1;
  sum* s2;
  bool ex(int n) {
    int r1 = eval(s1, n);
    int r2 = eval(s2, n);
    if (op == '<') {
      return r1 < r2;
    } else if (op == '>') {
      return r1 > r2;
    }
    return r1 == r2;
  }
  logicalExpr(char* a, char* end, char** b) {
    s1 = createSum(a, end, b);
    a = *b;
    for (; a != end; a++) {
      if (*a == ' ') continue;
      if (*a == '>' || *a == '<' || *a == '=') {
        op = *a;
        if (*a == '=') {
          a++;
        }
        s2 = createSum(a + 1, end, b);
        return;
      }
    }
  }
};
struct multiplier {
  bool n = false;
  int number = 0;
  sum* expr = 0;
  multiplier(char* a, char* end, char** b) {
    for (; a != end; a++) {
      if (*a == ' ') continue;
      if (*a == 'f') {
        a = read(a, "(");
        expr = createSum(a + 1, end, b);
        *b = read(*b, ")");
        return;
      } else if (*a == 'n') {
        n = true;
        *b = a + 1;
        return;
      } else if (isdigit(*a)) {
        number *= 10;
        number += *a - '0';
      } else {
        *b = a;
        return;
      }
    }
    *b = end;
  }
  int ex(int v) {
    if (n) {
      return v;
    }
    if (expr != 0) {
      return f(eval(expr, v));
    }
    return number;
  }
};
struct product {
  vector<char> ops;
  vector<multiplier*> ms;
  product(char* a, char* end, char** b) {
    char* d;
    ms.push_back(new multiplier(a, end, &d));
    assert(d != a);
    a = d;
    for (; a != end; a++) {
      if (*a == ' ') continue;
      if (*a == '*' || *a == '/') {
        ops.push_back(*a);
        a++;
        char* d;
        ms.push_back(new multiplier(a, end, &d));
        a = d - 1;
      } else {
        *b = a;
        return;
      }
    }
    *b = end;
  }
  int ex(int n) {
    int v = ms[0]->ex(n);
    for (int i = 1; i < ms.size(); i++) {
      if (ops[i - 1] == '*') {
        v *= ms[i]->ex(n);
        v %= M;
      } else {
        v /= ms[i]->ex(n);
      }
    }
    if (v >= M) {
      cerr << n << ", " << ops.size() << endl;
    }
    assert(v < M);
    return v;
  }
};
struct sum {
  vector<char> ops;
  vector<product*> ps;
  sum(char* a, char* end, char** b) {
    char* d;
    ps.push_back(new product(a, end, &d));
    a = d;
    for (; a != end; a++) {
      if (*a == ' ') continue;
      if (*a == '+' || *a == '-') {
        ops.push_back(*a);
        a++;
        char* d;
        ps.push_back(new product(a, end, &d));
        a = d - 1;
      } else {
        *b = a;
        return;
      }
    }
  }
  int ex(int n) {
    int v = ps[0]->ex(n);
    for (int i = 1; i < ps.size(); i++) {
      if (ops[i - 1] == '+') {
        v += ps[i]->ex(n);
        v %= M;
      } else {
        v += M - ps[i]->ex(n);
        v %= M;
      }
    }
    assert(v < M);
    return v;
  }
};
int eval(sum* s, int n) { return s->ex(n); }
sum* createSum(char* a, char* e, char** b) { return new sum(a, e, b); }
bool ng = false;
struct operatorSequence {
  logicalExpr* l = 0;
  sum* ar = 0;
  operatorSequence* op = 0;
  operatorSequence(char* a, char* end) {
    for (; a < end; a++) {
      if (*a == ' ') continue;
      if (*a == 'i') {
        a = read(a, "if");
        a = read(a, "(");
        char* b;
        l = new logicalExpr(a, end, &b);
        a = b;
      } else if (*a == 'r') {
        a = read(a, "return");
      } else if (*a == '}') {
        return;
      } else {
        char* b;
        ar = new sum(a, end, &b);
        a = b;
        op = new operatorSequence(a + 1, end);
        break;
      }
    }
  }
  int ex(int n) {
    if (l) {
      if (l->ex(n)) {
        return ar->ex(n);
      }
    } else if (ar) {
      return ar->ex(n);
    }
    if (op) {
      return op->ex(n);
    }
    assert(false);
  }
};
operatorSequence* theOp;
int memo[32768];
int f(int n) {
  if (memo[n] != -1) {
    return memo[n];
  }
  return memo[n] = theOp->ex(n);
}
int memo2[32768];
int g(int n) {
  if (memo2[n] != -1) {
    return memo2[n];
  }
  if (n == 0) return 0;
  if (n == 1) return n;
  return memo2[n] = (g(n - 1) + g(n - 2)) % M;
}
int main() {
  int s0;
  cin.getline(buf, 1000);
  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));
  s0 = strlen(buf);
  int n = atoi(buf);
  int s = s0;
  while (cin.getline(buf + s, 1000)) {
    s += strlen(buf + s);
  }
  char* a = buf + s0;
  a = read(a, "int");
  a = read(a, "f");
  a = read(a, "(");
  a = read(a, "int");
  a = read(a, "n");
  a = read(a, ")");
  a = read(a, "{");
  theOp = new operatorSequence(a, buf + s);
  int ret = -1;
  for (int i = 0; i <= 32767; i++) {
    if (f(i) == n) {
      ret = i;
    }
  }
  cout << ret << endl;
  return 0;
}
