Problem: Petya wrote a programme on C++ that calculated a very interesting function f(n). Petya ran the program with a certain value of n and went to the kitchen to have some tea. The history has no records concerning how long the program had been working. By the time Petya returned, it had completed the calculations and had the result. However while Petya was drinking tea, a sly virus managed to destroy the input file so that Petya can't figure out for which value of n the program was run. Help Petya, carry out the inverse function!

Mostly, the program consists of a function in C++ with the following simplified syntax:

  * function ::= int f(int n) {operatorSequence}
  * operatorSequence ::= operator | operator operatorSequence
  * operator ::= return arithmExpr; | if (logicalExpr) return arithmExpr;
  * logicalExpr ::= arithmExpr > arithmExpr | arithmExpr < arithmExpr | arithmExpr == arithmExpr
  * arithmExpr ::= sum
  * sum ::= product | sum + product | sum - product
  * product ::= multiplier | product * multiplier | product / multiplier
  * multiplier ::= n | number | f(arithmExpr)
  * number ::= 0|1|2|... |32767



The whitespaces in a operatorSequence are optional.

Thus, we have a function, in which body there are two kinds of operators. There is the operator "return arithmExpr;" that returns the value of the expression as the value of the function, and there is the conditional operator "if (logicalExpr) return arithmExpr;" that returns the value of the arithmetical expression when and only when the logical expression is true. Guaranteed that no other constructions of C++ language — cycles, assignment operators, nested conditional operators etc, and other variables except the n parameter are used in the function. All the constants are integers in the interval [0..32767].

The operators are performed sequentially. After the function has returned a value other operators in the sequence are not performed. Arithmetical expressions are performed taking into consideration the standard priority of the operations. It means that first all the products that are part of the sum are calculated. During the calculation of the products the operations of multiplying and division are performed from the left to the right. Then the summands are summed, and the addition and the subtraction are also performed from the left to the right. Operations ">" (more), "<" (less) and "==" (equals) also have standard meanings.

Now you've got to pay close attention! The program is compiled with the help of 15-bit Berland C++ compiler invented by a Berland company BerSoft, that's why arithmetical operations are performed in a non-standard way. Addition, subtraction and multiplication are performed modulo 32768 (if the result of subtraction is negative, then 32768 is added to it until the number belongs to the interval [0..32767]). Division "/" is a usual integer division where the remainder is omitted.

Examples of arithmetical operations: 

<image>

Guaranteed that for all values of n from 0 to 32767 the given function is performed correctly. That means that:

1. Division by 0 never occures.

2. When performing a function for the value n = N recursive calls of the function f may occur only for the parameter value of 0, 1, ..., N - 1. Consequently, the program never has an infinite recursion.

3. As the result of the sequence of the operators, the function always returns a value.

We have to mention that due to all the limitations the value returned by the function f is independent from either global variables or the order of performing the calculations of arithmetical expressions as part of the logical one, or from anything else except the value of n parameter. That's why the f function can be regarded as a function in its mathematical sense, i.e. as a unique correspondence between any value of n from the interval [0..32767] and a value of f(n) from the same interval.

Given the value of f(n), and you should find n. If the suitable n value is not unique, you should find the maximal one (from the interval [0..32767]).

Input

The first line has an integer f(n) from the interval [0..32767]. The next lines have the description of the function f. In the description can be found extra spaces and line breaks (see the examples) which, of course, can’t break key words int, if, return and numbers. The size of input data can’t exceed 100 bytes.

Output

Output a single number — the answer to the problem. If there’s no answer, output "-1" (without quotes).

Examples

Input

17
int f(int n)
{
if (n &lt; 100) return 17;
if (n &gt; 99) return 27;
}


Output

99


Input

13
int f(int n)
{
if (n == 0) return 0;
return f(n - 1) + 1;
}


Output

13

Input

144
int f(int n)
{
if (n == 0) return 0;
if (n == 1) return n;
return f(n - 1) + f(n - 2);
}

Output

24588

Solution (in CPP):

Solution:
#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;
}