#include <bits/stdc++.h>
using namespace std;
double const pi = 3.1415926535897932384626433832795;
int const inf = (int)1e9;
long long const inf64 = (long long)2e18;
const string task = "";
int const P = 32768;
int ans, n, f[40000], num, p, l;
string s[100], S;
int exp();
int mn() {
  if (S[l] == 'n') {
    l++;
    return n;
  }
  if (S[l] == 'f') {
    l += 2;
    int res = f[exp()];
    l++;
    return res;
  }
  int res = 0;
  while (S[l] >= '0' && S[l] <= '9') res = res * 10 + S[l++] - '0';
  return res;
}
int sl() {
  int res = mn();
  while (S[l] == '*' || S[l] == '/') {
    char zn = S[l++];
    int tmp = mn();
    if (zn == '*')
      res = (int)(((long long)res * tmp) % P);
    else
      res = (res / tmp) % P;
  }
  return res;
}
int exp() {
  int res = sl();
  while (S[l] == '+' || S[l] == '-') {
    char zn = S[l++];
    int tmp = sl();
    if (zn == '+')
      res = (res + tmp) % P;
    else
      res = (P + res - tmp) % P;
  }
  return res;
}
bool lexp() {
  int p1, p2, tp;
  p1 = exp();
  if (S[l] == '=')
    tp = 0, l += 2;
  else if (S[l] == '<')
    tp = -1, l++;
  else
    tp = 1, l++;
  p2 = exp();
  if (tp == 0 && p1 == p2) return true;
  if (tp == -1 && p1 < p2) return true;
  if (tp == 1 && p1 > p2) return true;
  return false;
}
int solve() {
  for (int i = 0; i < (int)num; i++) {
    S = s[i];
    if (S[0] == 'i') {
      l = 3;
      if (lexp()) {
        l += 7;
        return exp();
      }
    } else {
      l = 6;
      return exp();
    }
  }
  return -1;
}
int main() {
  char ch;
  cin >> ans;
  cin >> ch;
  while (ch != '{') cin >> ch;
  num = 0;
  while (true) {
    cin >> ch;
    if (ch == '}') break;
    if (ch == ';') {
      num++;
      continue;
    }
    if (ch > ' ') s[num] += ch;
  }
  int answer = -1;
  for (int i = 0; i < (int)P; i++) {
    n = i;
    f[n] = solve();
    if (f[n] == ans) answer = n;
  }
  cout << answer << endl;
  return 0;
}
