#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int a, b, c, d, i, j, n, m, k, fn;
string s = "";
char str[10001];
int mem[1 << 15];
int f(int n);
int operatorSequence(istringstream &iss, int n);
int oper(istringstream &iss, int n);
int arithm(istringstream &iss, int n);
int product(istringstream &iss, int n);
int multiplier(istringstream &iss, int n);
void getproduct(istringstream &iss, int n);
void getmultiplier(istringstream &iss, int n);
void getarithm(istringstream &iss, int n);
bool logical(istringstream &iss, int n) {
  string s, op;
  iss >> s;
  int o1 = arithm(iss, n);
  iss >> op;
  int o2 = arithm(iss, n);
  iss >> s;
  if (op == "<")
    return o1 < o2;
  else if (op == ">")
    return o1 > o2;
  else
    return o1 == o2;
}
void getmultiplier(istringstream &iss, int n) {
  string s;
  iss >> s;
  if (s == "n")
    return;
  else if (s == "f") {
    iss >> s;
    getarithm(iss, n);
    iss >> s;
    return;
  } else {
    return;
  }
}
int multiplier(istringstream &iss, int n) {
  string s;
  iss >> s;
  if (s == "n")
    return n;
  else if (s == "f") {
    iss >> s;
    int o = arithm(iss, n);
    iss >> s;
    return f(o);
  } else {
    int a = 0;
    for (int _n(((int)((s).size())) - 1), i(0); i <= _n; i++)
      a = a * 10 + s[i] - '0';
    return a % 32768;
  }
}
void getproduct(istringstream &iss, int n) {
  while (1) {
    getmultiplier(iss, n);
    string s = "";
    if (iss >> s) {
      if (s != "*" && s != "/") {
        iss.unget();
        break;
      }
    } else
      break;
  }
}
int product(istringstream &iss, int n) {
  int ans = 1;
  int sign = 0;
  while (1) {
    int o = multiplier(iss, n);
    if (!sign) {
      ans = ((long long)ans * o + 1073741824) % 32768;
    } else {
      ans = ans / o;
    }
    string s = "";
    if (iss >> s) {
      if (s == "*" || s == "/") {
        if (s == "*")
          sign = 0;
        else
          sign = 1;
      } else {
        iss.unget();
        break;
      }
    } else
      break;
  }
  return ans;
}
void getarithm(istringstream &iss, int n) {
  while (1) {
    getproduct(iss, n);
    string s;
    if (iss >> s) {
      if (s != "+" && s != "-") {
        iss.unget();
        break;
      }
    } else
      break;
  }
  return;
}
int arithm(istringstream &iss, int n) {
  int ans = 0;
  int sign = 1;
  while (1) {
    int o = product(iss, n);
    ans += sign * o;
    if (ans < 32768) ans += 32768;
    if (ans >= 32768) ans -= 32768;
    string s;
    if (iss >> s) {
      if (s == "+" || s == "-") {
        if (s == "-")
          sign = -1;
        else
          sign = 1;
      } else {
        iss.unget();
        break;
      }
    } else
      break;
  }
  return ans;
}
int oper(istringstream &iss, int n) {
  string ss = "";
  iss >> ss;
  if (ss == "return") {
    int o = arithm(iss, n);
    iss >> ss;
    return o;
  } else if (ss == "if") {
    if (logical(iss, n)) {
      iss >> ss;
      int o = arithm(iss, n);
      iss >> ss;
      return o;
    } else {
      iss >> ss;
      getarithm(iss, n);
      iss >> ss;
    }
  }
  return -1;
}
int operatorSequence(istringstream &iss, int n) {
  while (1) {
    int o = oper(iss, n);
    if (o != -1) return o;
    if (!iss) break;
  }
  return -1;
}
int f(int n) {
  if (mem[n] != -1) return mem[n];
  istringstream iss(s);
  string ss;
  for (int _n((7)), h(1); h <= _n; h++) {
    iss >> ss;
  }
  return mem[n] = operatorSequence(iss, n);
}
int main() {
  scanf("%d\n", &fn);
  while (gets(str)) {
    s += " " + (string)str;
  }
  a = 0;
  while (a < (int)((s).size())) {
    if (s[a] == '=') {
      s.erase(a, 1);
    }
    ++a;
  }
  a = 0;
  while (a < (int)((s).size())) {
    if (s[a] == '(' || s[a] == ')' || s[a] == '+' || s[a] == '-' ||
        s[a] == '*' || s[a] == '/' || s[a] == ';' || s[a] == '{' ||
        s[a] == '}' || s[a] == '<' || s[a] == '>' || s[a] == '=') {
      string str = s.substr(a, 1);
      s.erase(a, 1);
      s.insert(a, " " + str + " ");
      ++a;
    }
    ++a;
  }
  memset(mem, -1, sizeof(mem));
  a = -1;
  for (int _n((32768) - 1), i(0); i <= _n; i++) {
    if (f(i) == fn) a = i;
  }
  printf("%d\n", a);
}
