#include <bits/stdc++.h>
const int MOD = 32768;
char code[333];
int f[MOD], n;
int calc(int l, int r) {
  if (l == r && code[l] == 'n') return n;
  if (code[l] == 'f') {
    int cnt = 0;
    for (int i = l + 2, t = 1; i <= r; ++i) {
      if (code[i] == '(')
        ++t;
      else if (code[i] == ')')
        --t;
      if (t == 0) ++cnt;
    }
    if (cnt == 1) return f[calc(l + 2, r - 1)];
  }
  bool f = true;
  int ret = 0;
  for (int i = l; f && i <= r; ++i)
    if (isdigit(code[i]))
      ret = ret * 10 + code[i] - 48;
    else
      f = false;
  if (f) return ret % MOD;
  f = true;
  int inb = 0;
  for (int i = l; f && i <= r; ++i) {
    if (!inb && (code[i] == '+' || code[i] == '-'))
      f = false;
    else if (code[i] == '(')
      ++inb;
    else if (code[i] == ')')
      --inb;
  }
  if (f) {
    ret = 1;
    bool mul = 1;
    int last = l, inb = 0;
    for (int i = l; i <= r; ++i)
      if (!inb && (code[i] == '*' || code[i] == '/')) {
        if (mul)
          ret = ret * calc(last, i - 1) % MOD;
        else
          ret = ret / calc(last, i - 1) % MOD;
        mul = code[i] == '*';
        last = i + 1;
      } else if (code[i] == '(')
        ++inb;
      else if (code[i] == ')')
        --inb;
    if (mul)
      ret = ret * calc(last, r) % MOD;
    else
      ret = ret / calc(last, r) % MOD;
    return ret;
  }
  bool plus = 1;
  int last = l;
  ret = inb = 0;
  for (int i = l; i <= r; ++i)
    if (!inb && (code[i] == '+' || code[i] == '-')) {
      if (plus)
        ret = (ret + calc(last, i - 1)) % MOD;
      else
        ret = (ret - calc(last, i - 1) + MOD) % MOD;
      plus = code[i] == '+';
      last = i + 1;
    } else if (code[i] == '(')
      ++inb;
    else if (code[i] == ')')
      --inb;
  if (plus)
    ret = (ret + calc(last, r)) % MOD;
  else
    ret = (ret - calc(last, r) + MOD) % MOD;
  return ret;
}
int main() {
  int goal, len = 0, ans = -1;
  scanf("%d int f ( int n ) { ", &goal);
  char c;
  while ((c = getchar()) != '}')
    if (c != ' ' && c != '\n') code[len++] = c;
  for (n = 0; n < MOD; ++n) {
    for (int i = 0; i < len;)
      if (code[i] == 'r') {
        int j = i + 6;
        for (; code[j] != ';'; ++j)
          ;
        f[n] = calc(i + 6, j - 1);
        break;
      } else {
        int j = i + 3;
        for (; code[j] != '=' && code[j] != '<' && code[j] != '>'; ++j)
          ;
        char op = code[j];
        int x = calc(i + 3, j - 1);
        if (code[j] == '=') ++j;
        i = ++j;
        for (int t = 0; t != 1; ++j)
          if (code[j] == '(')
            --t;
          else if (code[j] == ')')
            ++t;
        int y = calc(i, j - 2);
        i = j;
        if (op == '=' && x != y || op == '<' && x >= y || op == '>' && x <= y) {
          for (; code[i] != ';'; ++i)
            ;
          ++i;
        }
      }
    if (f[n] == goal) ans = n;
  }
  printf("%d\n", ans);
  return 0;
}
