#include <bits/stdc++.h>
using namespace std;
void prepare() {}
const int mod = 32768;
string P;
int d[32768];
vector<string> ops;
int arExp(string s, int &N);
int go(int n);
string trim(string s) {
  while ((int)(s).size() > 0 && s[0] <= ' ') s = s.substr(1);
  while ((int)(s).size() > 0 && s[(int)(s).size() - 1] <= ' ')
    s = s.substr(0, (int)(s).size() - 1);
  return s;
}
vector<string> split(string s, char c) {
  vector<string> v;
  string g;
  for (int i = 0; i < (int)(s).size(); i++) {
    if (s[i] == c) {
      g = trim(g);
      if (g != "") v.push_back(g);
      g = "";
    } else
      g.push_back(s[i]);
  }
  g = trim(g);
  if (g != "") v.push_back(g);
  return v;
}
bool isNum(string s) {
  for (int i = 0; i < (int)(s).size(); i++)
    if (!isdigit(s[i])) return false;
  return true;
}
int mult(string s, int &N) {
  s = trim(s);
  if (s == "n") return N;
  if (isNum(s)) return atoi(s.c_str());
  string g;
  for (int i = 0; i < (int)(s).size(); i++) {
    if (s[i] == '(') {
      g = s.substr(i + 1);
      break;
    }
  }
  g = trim(g);
  g = g.substr(0, (int)(g).size() - 1);
  int z = arExp(g, N);
  return go(z);
}
int prod(string s, int &N) {
  s = trim(s);
  int r = 1;
  char z = '*';
  string g;
  int bal = 0;
  for (int i = 0; i < (int)(s).size(); i++) {
    if (s[i] == '(') {
      g.push_back(s[i]);
      bal++;
    } else if (s[i] == ')') {
      bal--;
      g.push_back(s[i]);
    } else if ((s[i] == '*' || s[i] == '/') && bal == 0) {
      if (z == '*')
        r *= mult(g, N);
      else
        r /= mult(g, N);
      while (r < 0) r += mod;
      r %= mod;
      g = "";
      z = s[i];
    } else
      g.push_back(s[i]);
  }
  if (z == '*')
    r *= mult(g, N);
  else
    r /= mult(g, N);
  while (r < 0) r += mod;
  r %= mod;
  return r;
}
int sum(string s, int &N) {
  s = trim(s);
  int r = 0;
  string g;
  char z = '+';
  int bal = 0;
  for (int i = 0; i < (int)(s).size(); i++) {
    if (s[i] == '(') {
      g.push_back(s[i]);
      bal++;
    } else if (s[i] == ')') {
      bal--;
      g.push_back(s[i]);
    } else if ((s[i] == '+' || s[i] == '-') && bal == 0) {
      if (z == '+')
        r += prod(g, N);
      else
        r -= prod(g, N);
      while (r < 0) r += mod;
      while (r >= mod) r -= mod;
      g = "";
      z = s[i];
    } else
      g.push_back(s[i]);
  }
  if (z == '+')
    r += prod(g, N);
  else
    r -= prod(g, N);
  while (r < 0) r += mod;
  r %= mod;
  return r;
}
int arExp(string s, int &N) {
  s = trim(s);
  int z = sum(s, N);
  return z;
}
bool logExp(string s, int &N) {
  s = trim(s);
  string g;
  int ar1, ar2;
  char z;
  for (int i = 0; i < (int)(s).size(); i++) {
    if (s[i] == '>' || s[i] == '<' || s[i] == '=') {
      z = s[i];
      if (g != "") ar1 = arExp(g, N);
      g = "";
    } else {
      g.push_back(s[i]);
    }
  }
  ar2 = arExp(g, N);
  if (z == '<') return ar1 < ar2;
  if (z == '>') return ar1 > ar2;
  return ar1 == ar2;
}
int calcOp(string op, int &N) {
  if (op.substr(0, 6) == "return") return arExp(op.substr(6), N);
  string g;
  int i;
  for (i = 0; i < (int)(op).size(); i++) {
    if (op[i] == '(') break;
  }
  i++;
  for (; i < (int)(op).size(); i++) {
    if (op.substr(i, 6) == "return") break;
    g.push_back(op[i]);
  }
  g = trim(g);
  g = g.substr(0, (int)(g).size() - 1);
  if (logExp(g, N)) return arExp(op.substr(i + 6), N);
  return -1;
}
int opSec(int &N) {
  for (int i = 0; i < (int)(ops).size(); i++) {
    int k = calcOp(ops[i], N);
    if (k >= 0) return k;
  }
  return -1;
}
int go(int n) {
  if (d[n] >= 0) return d[n];
  return d[n] = opSec(n);
}
void solve() {
  int n;
  scanf("%d\n", &n);
  memset((d), (-1), sizeof(d));
  char c;
  while (scanf("%c", &c) > 0) {
    if (c <= ' ')
      P.push_back(' ');
    else
      P.push_back(c);
  }
  int r = -1;
  int k = 0;
  for (int i = 0; i < (int)(P).size(); i++) {
    k++;
    if (P[i] == '{') break;
  }
  P = P.substr(k);
  P = P.substr(0, (int)(P).size() - 1);
  for (int i = (int)(P).size(); i >= 0; i--) {
    if (P[i] == '}') {
      P = P.substr(0, i);
      break;
    }
  }
  ops = split(P, ';');
  for (int i = 0; i < mod; i++) {
    if (go(i) == n) r = i;
  }
  printf("%d\n", r);
}
int main() {
  prepare();
  solve();
  return 0;
}
