import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
		Scanner in;
		PrintWriter pw;

		String s;
		int pos;
		int n;

		int numb(){
			if(s.charAt(pos)=='f'){
				pos+=2;
				int t = expr();
				pos++;
				return res[t];
			}
			if(s.charAt(pos)=='n'){
				pos++;
				return n;
			}
			int r = 0;
			while(Character.isDigit(s.charAt(pos))){
				r = 10 * r+ s.charAt(pos)-'0';
				pos++;
			}
			return r;
		}

		int term(){
			int res = numb();
			while(s.charAt(pos)=='*' || s.charAt(pos)=='/'){
				char c = s.charAt(pos);
				pos++;
				int t = numb();
				if(c=='*') res *= t;
				else res /= t;
				res%=32768;
				if(res<0) res += 32768;
			}
			return res;
		}

		int expr(){
			int res = term();
			while(s.charAt(pos)=='+' || s.charAt(pos)=='-'){
				char c = s.charAt(pos);
				pos++;
				int t = term();
				if(c=='+') res += t;
				else res -= t;
				res%=32768;
				if(res<0) res += 32768;
			}
			return res;
		}

		boolean logic_expr(){
			int v1 = expr();
			char c = s.charAt(pos);
			if(c=='<'){
				pos++;
				return v1 < expr();
			}
			if(c=='>'){
				pos++;
				return v1 > expr();
			}
			pos++;pos++;
			return v1 == expr();
		}

		int calc(){
			while(s.charAt(pos)!='}'){
				if(s.charAt(pos)=='r'){
					pos += 6;
					return expr();
				}else{
					pos += 3;
					boolean e = logic_expr();
					pos ++;
					if(e){
						pos += 6;
						return expr();
					}else{
						while(s.charAt(pos)!=';') pos++;
						pos++;
					}
				}
			}
			return 0;
		}

		int res[];
		boolean u[];
		int getRes(int _n){
			if(u[_n]) return res[_n];
			n=_n;
			u[n]=true;
			pos = 11;
			return res[n]=calc();
		}
		public void run () throws Exception {
            in = new Scanner(System.in);
            pw = new PrintWriter(System.out);

            int n = in.nextInt();

            s="";
            while(in.hasNext())
            	s+=in.next();
            boolean found = false;
            res = new int[32768];
            u = new boolean[32768];
            for(int i=0;i<=32767;i++){
            	getRes(i);
            }
            for(int i=32767;i>=0;i--){
        	   if(res[i] == n){
        		   pw.println(i);
        		   found = true;
        		   break;
        	   }
            }
            if(!found) pw.println("-1");
            pw.close();
        }
        public static void main(String[] args) throws Exception {
                new Main ().run();
        }
}

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class School1G {

    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int n = Integer.valueOf(in.readLine());
        char[] buf = new char[200];
        int k = in.read(buf);

        String prog = String.valueOf(buf, 0, k);
        prog = prog.substring(prog.indexOf('{') + 1);
        prog = prog.substring(0, prog.indexOf('}'));
        String[] opers = prog.split(";");
        for (int i = 0; i < opers.length; i++)
            opers[i] = opers[i].trim();

        Function.init(opers.length);
        for (int i = 0; i < opers.length; i++)
            Function.body[i] = new Operator(opers[i]);

        int ans = -1;
        for (int i = (1 << 15)-1; i >= 0; i--)
            if (Function.eval(i) == n) {
                ans = i;
                break;
            }

        out.println(ans);

        out.flush();
    }

}

class Function {
    static HashMap<Integer, Integer> func = new HashMap<Integer, Integer>();
    static Operator[] body;

    static void init(int n) {
        body = new Operator[n];
    }

    static int eval(int n) {
        if (func.containsKey(n)) return func.get(n);

        for (Operator oper : body) {
            int r = oper.eval(n);
            if (r != -1) {
                func.put(n, r);
                return r;
            }
        }
        return -1;
    }
}

class Expression {
    static final int MD = 1 << 15;

    List<String> poliz;

    public Expression(String s) {
        s += "$";
        Stack<String> ops = new Stack<String>();
        poliz = new LinkedList<String>();
        int p = 0;
        char c = ' ';
        while (c != '$') {
            String cur = "";
            while (c <= ' ')
                c = s.charAt(p++);
            if (c == 'n') {
                cur = "n";
                c = s.charAt(p++);
                poliz.add(cur);
            } else if (c == 'f') {
                cur = "f";
                while (c != '(')
                    c = s.charAt(p++);
                c = s.charAt(p++);
                ops.push(cur);
            } else if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    cur += (char) c;
                    c = s.charAt(p++);
                }
                poliz.add(cur);
            } else {
                cur += (char) c;
                c = s.charAt(p++);
                if (cur.equals(")")) {
                    while (!ops.peek().equals("f"))
                        poliz.add(ops.pop());
                    poliz.add(ops.pop());
                } else if (cur.equals("+") || cur.equals("-"))
                    while (!ops.empty() && !ops.peek().equals("f"))
                        poliz.add(ops.pop());
                else
                    while (!ops.empty()
                            && (ops.peek().equals("*") || ops.peek()
                                    .equals("/")))
                        poliz.add(ops.pop());
                if (!cur.equals(")"))
                    ops.push(cur);
            }
        }
        while (!ops.empty())
            poliz.add(ops.pop());
    }

    public int eval(int n) {
        Stack<Integer> st = new Stack<Integer>();
        for (String s : poliz) {
            if (s.equals("n"))
                st.push(n);
            else if (s.charAt(0) >= '0' && s.charAt(0) <= '9')
                st.push(Integer.valueOf(s));
            else if (s.equals("f"))
                st.push(Function.eval(st.pop()));
            else {
                int b = st.pop(), a = st.pop();
                if (s.equals("+")) st.push((a + b)%MD);
                else if (s.equals("*")) st.push(a*b%MD);
                else if (s.equals("/")) st.push(a/b);
                else st.push((a - b + MD)%MD);
            }
        }
        return st.pop();
    }
}

class Condition {
    int sign;
    Expression left, right;

    public Condition(String s) {
        s = s.substring(1, s.length() - 1);
        int sgn = s.indexOf('<');
        if (sgn >= 0) {
            sign = -1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else if ((sgn = s.indexOf('>')) >= 0) {
            sign = 1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else {
            sgn = s.indexOf("==");
            sign = 0;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 2).trim());
        }
    }

    public boolean eval(int n) {
        int a = left.eval(n), b = right.eval(n);
        if (sign == -1)
            return a < b;
        else if (sign == 1)
            return a > b;
        else
            return a == b;
    }
}

class Operator {
    boolean isIf;
    Condition cond;
    Expression toReturn;

    public Operator(String s) {
        if (s.equals("")) return;
        if (s.startsWith("if")) {
            int whereRet = s.indexOf("return");
            isIf = true;
            cond = new Condition(s.substring(2, whereRet).trim());
            toReturn = new Expression(s.substring(whereRet + 6).trim());
        } else {
            isIf = false;
            cond = null;
            toReturn = new Expression(s.substring(6).trim());
        }
    }

    public int eval(int n) {
        if (isIf && !cond.eval(n))
            return -1;

        return toReturn.eval(n);
    }
}

import java.util.Scanner;

public class Inverse {
    final int N = 32768;
    String string;
    int[] res = new int[N];
    int n, pos;

    int num() {
        if (string.charAt(pos) == 'f') {
            pos += 2;
            int tmp = expression();
            pos++;
            return res[tmp];
        }
        if (string.charAt(pos) == 'n') {
            pos++;
            return n;
        }
        int ret = 0;
        while (Character.isDigit(string.charAt(pos))) {
            ret = ret * 10 + string.charAt(pos) - '0';
            ++pos;
        }
        return ret;
    }

    int term() {
        int ret = num();
        while (string.charAt(pos) == '*' || string.charAt(pos) == '/') {
            char c = string.charAt(pos++);
            int next = num();
            if (c == '*') {
                ret = (ret * next) % N;
            } else {
                ret /= next;
            }
        }
        return ret;
    }

    int expression() {
        int ret = term();
        while (string.charAt(pos) == '+' || string.charAt(pos) == '-') {
            char c = string.charAt(pos++);
            int next = term();
            if (c == '+') {
                ret += next;
                if (ret >= N) ret -= N;
            } else {
                ret -= next;
                if (ret < 0) ret += N;
            }
        }
        return ret;
    }

    boolean logicalExpression() {
        int X = expression();
        char flag = string.charAt(pos++);
        if (flag == '=') pos++;
        int Y = expression();
        if (flag == '<' && X < Y) return true;
        if (flag == '=' && X == Y) return true;
        if (flag == '>' && X > Y) return true;
        return false;
    }

    int calculate() {
        pos = 11;
        while (string.charAt(pos) != '}') {
            if (string.charAt(pos) == 'r') {
                pos += 6;
                return expression();
            } else {
                pos += 3;
                boolean flag = logicalExpression();
                ++pos;
                if (flag == true) {
                    pos += 6;
                    return expression();
                } else {
                    while (string.charAt(pos) != ';') ++pos;
                    ++pos;
                }
            }
        }
        return 0;
    }

    public void solve() {
        Scanner inScanner = new Scanner(System.in);
        string = new String();
        int ans = inScanner.nextInt();
        while (inScanner.hasNext()) {
            string += inScanner.next();
        }
        for (int i = 0; i < N; ++i) {
            n = i;
            res[i] = calculate();
        }
        for (int i = N - 1; i >= 0; --i)
            if (res[i] == ans) {
                System.out.println(i);
                return;
            }
        System.out.println("-1");
    }

    public static void main(String[] args) {
        new Inverse().solve();
    }
}

import java.util.Scanner;

public class Inverse {
    final int N = 32768;
    String string;
    int[] res = new int[N];
    int n, pos;

    int num() {
        if (string.charAt(pos) == 'f') {
            pos += 2;
            int tmp = expression();
            pos++;
            return res[tmp];
        }
        if (string.charAt(pos) == 'n') {
            pos++;
            return n;
        }
        int ret = 0;
        while (Character.isDigit(string.charAt(pos))) {
            ret = ret * 10 + string.charAt(pos) - '0';
            ++pos;
        }
        return ret;
    }

    int term() {
        int ret = num();
        while (string.charAt(pos) == '*' || string.charAt(pos) == '/') {
            char c = string.charAt(pos++);
            int next = num();
            if (c == '*') {
                ret = (ret * next) % N;
            } else {
                ret /= next;
            }
        }
        return ret;
    }

    int expression() {
        int ret = term();
        while (string.charAt(pos) == '+' || string.charAt(pos) == '-') {
            char c = string.charAt(pos++);
            int next = term();
            if (c == '+') {
                ret += next;
                if (ret >= N) ret -= N;
            } else {
                ret -= next;
                if (ret < 0) ret += N;
            }
        }
        return ret;
    }

    boolean logicalExpression() {
        int X = expression();
        char flag = string.charAt(pos++);
        if (flag == '=') pos++;
        int Y = expression();
        if (flag == '<' && X < Y) return true;
        if (flag == '=' && X == Y) return true;
        if (flag == '>' && X > Y) return true;
        return false;
    }

    int calculate() {
        pos = 11;
        while (string.charAt(pos) != '}') {
            if (string.charAt(pos) == 'r') {
                pos += 6;
                return expression();
            } else {
                pos += 3;
                boolean flag = logicalExpression();
                ++pos;
                if (flag == true) {
                    pos += 6;
                    return expression();
                } else {
                    while (string.charAt(pos) != ';') ++pos;
                    ++pos;
                }
            }
        }
        return 0;
    }

    public void solve() {
        Scanner inScanner = new Scanner(System.in);
        string = new String();
        int ans = inScanner.nextInt();
        while (inScanner.hasNext()) {
            string += inScanner.next();
            if (string.charAt(string.length() - 1) == '}') break;
        }
        for (int i = 0; i < N; ++i) {
            n = i;
            res[i] = calculate();
        }
        for (int i = N - 1; i >= 0; --i)
            if (res[i] == ans) {
                System.out.println(i);
                return;
            }
        System.out.println("-1");
    }

    public static void main(String[] args) {
        new Inverse().solve();
    }
}

import java.util.Scanner;

public class Inverse {
    final int N = 32768;
    String string;
    int[] res = new int[N];
    int n, pos;

    int num() {
        if (string.charAt(pos) == 'f') {
            pos += 2;//f(
            int tmp = expression();
            pos++;//)
            return res[tmp];
        }
        if (string.charAt(pos) == 'n') {
            pos++;
            return n;
        }
        int ret = 0;
        while (Character.isDigit(string.charAt(pos))) {
            ret = ret * 10 + string.charAt(pos) - '0';
            ++pos;
        }
        //System.out.println("num");
        //System.out.println(pos);
        //System.out.println(string.charAt(pos));
        //System.out.println(ret);
        return ret;
    }

    int term() {
        int ret = num();
        while (string.charAt(pos) == '*' || string.charAt(pos) == '/') {
            char c = string.charAt(pos++);
            int next = num();
            if (c == '*') {
                ret = (ret * next) % N;
            } else {
                ret /= next;
            }
        }
        return ret;
    }

    int expression() {
        int ret = term();
        while (string.charAt(pos) == '+' || string.charAt(pos) == '-') {
            char c = string.charAt(pos++);
            int next = term();
            if (c == '+') {
                ret += next;
                if (ret >= N) ret -= N;
            } else {
                ret -= next;
                if (ret < 0) ret += N;
            }
        }
        return ret;
    }

    boolean logicalExpression() {
        //System.out.println("Logival");
        //System.out.println(pos);
        int X = expression();
        char flag = string.charAt(pos++);
        if (flag == '=') pos++;
        int Y = expression();
        if (flag == '<' && X < Y) return true;
        if (flag == '=' && X == Y) return true;
        if (flag == '>' && X > Y) return true;
        return false;
    }

    int calculate() {
        pos = 11;//"intf(intn){".length();
        while (string.charAt(pos) != '}') {
            if (string.charAt(pos) == 'r') {
                pos += 6;//"return".length();
                return expression();
            } else {
                pos += 3;//"if(".length();
                boolean flag = logicalExpression();
                ++pos;//)
                if (flag == true) {
                    pos += 6;//"return".length();
                    return expression();
                } else {
                    while (string.charAt(pos) != ';') ++pos;
                    ++pos;
                }
            }
        }
        return 0;
    }

    public void solve() {
        Scanner inScanner = new Scanner(System.in);
        string = new String();
        int ans = inScanner.nextInt();
        while (inScanner.hasNext()) {
            string += inScanner.next();
            if (string.charAt(string.length() - 1) == '}') break;
        }
        //System.out.println(string);
        for (int i = 0; i < N; ++i) {
            n = i;
            res[i] = calculate();
            //if (i < 100)System.out.println(res[i]);
        }
        for (int i = N - 1; i >= 0; --i)
            if (res[i] == ans) {
                System.out.println(i);
                return;
            }
        System.out.println("-1");
    }

    public static void main(String[] args) {
        new Inverse().solve();
    }
}

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class School1G {

    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int n = Integer.valueOf(in.readLine());
        char[] buf = new char[200];
        int k = in.read(buf);

        String prog = String.valueOf(buf, 0, k);
        prog = prog.substring(prog.indexOf('{') + 1);
        prog = prog.substring(0, prog.indexOf('}'));
        String[] opers = prog.split(";");
        for (int i = 0; i < opers.length; i++)
            opers[i] = opers[i].trim();

        Function.init(opers.length - 1);
        for (int i = 0; i < opers.length - 1; i++)
            Function.body[i] = new Operator(opers[i]);

        int ans = -1;
        for (int i = (1 << 15)-1; i >= 0; i--)
            if (Function.eval(i) == n) {
                ans = i;
                break;
            }

        out.println(ans);

        out.flush();
    }

}

class Function {
    static HashMap<Integer, Integer> func = new HashMap<Integer, Integer>();
    static Operator[] body;

    static void init(int n) {
        body = new Operator[n];
    }

    static int eval(int n) {
        if (func.containsKey(n)) return func.get(n);

        for (Operator oper : body) {
            int r = oper.eval(n);
            if (r != -1) {
                func.put(n, r);
                return r;
            }
        }
        return -1;
    }
}

class Expression {
    static final int MD = 1 << 15;

    List<String> poliz;

    public Expression(String s) {
        s += "$";
        Stack<String> ops = new Stack<String>();
        poliz = new LinkedList<String>();
        int p = 0;
        char c = ' ';
        while (c != '$') {
            String cur = "";
            while (c <= ' ')
                c = s.charAt(p++);
            if (c == 'n') {
                cur = "n";
                c = s.charAt(p++);
                poliz.add(cur);
            } else if (c == 'f') {
                cur = "f";
                while (c != '(')
                    c = s.charAt(p++);
                c = s.charAt(p++);
                ops.push(cur);
            } else if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    cur += (char) c;
                    c = s.charAt(p++);
                }
                poliz.add(cur);
            } else {
                cur += (char) c;
                c = s.charAt(p++);
                if (cur.equals(")")) {
                    while (!ops.peek().equals("f"))
                        poliz.add(ops.pop());
                    poliz.add(ops.pop());
                } else if (cur.equals("+") || cur.equals("-"))
                    while (!ops.empty() && !ops.peek().equals("f"))
                        poliz.add(ops.pop());
                else
                    while (!ops.empty()
                            && (ops.peek().equals("*") || ops.peek()
                                    .equals("/")))
                        poliz.add(ops.pop());
                if (!cur.equals(")"))
                    ops.push(cur);
            }
        }
        while (!ops.empty())
            poliz.add(ops.pop());
    }

    public int eval(int n) {
        Stack<Integer> st = new Stack<Integer>();
        for (String s : poliz) {
            if (s.equals("n"))
                st.push(n);
            else if (s.charAt(0) >= '0' && s.charAt(0) <= '9')
                st.push(Integer.valueOf(s));
            else if (s.equals("f"))
                st.push(Function.eval(st.pop()));
            else {
                int b = st.pop(), a = st.pop();
                if (s.equals("+")) st.push((a + b)%MD);
                else if (s.equals("*")) st.push(a*b%MD);
                else if (s.equals("/")) st.push(a/b);
                else {
                    int x = a - b;
                    while (x < 0) x += MD;
                    st.push(x);
                }
            }
        }
        return st.pop();
    }
}

class Condition {
    int sign;
    Expression left, right;

    public Condition(String s) {
        s = s.substring(1, s.length() - 1);
        int sgn = s.indexOf('<');
        if (sgn >= 0) {
            sign = -1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else if ((sgn = s.indexOf('>')) >= 0) {
            sign = 1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else {
            sgn = s.indexOf("=");
            sign = 0;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 2).trim());
        }
    }

    public boolean eval(int n) {
        int a = left.eval(n), b = right.eval(n);
        if (sign == -1)
            return a < b;
        else if (sign == 1)
            return a > b;
        else
            return a == b;
    }
}

class Operator {
    boolean isIf;
    Condition cond;
    Expression toReturn;

    public Operator(String s) {
        if (s.startsWith("if")) {
            int whereRet = s.indexOf("return");
            isIf = true;
            cond = new Condition(s.substring(2, whereRet).trim());
            toReturn = new Expression(s.substring(whereRet + 6).trim());
        } else {
            isIf = false;
            cond = null;
            toReturn = new Expression(s.substring(6).trim());
        }
    }

    public int eval(int n) {
        if (isIf && !cond.eval(n))
            return -1;

        return toReturn.eval(n);
    }
}

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class School1G {

    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int n = Integer.valueOf(in.readLine());
        char[] buf = new char[200];
        int k = in.read(buf);

        String prog = String.valueOf(buf, 0, k);
        prog = prog.substring(prog.indexOf('{') + 1);
        prog = prog.substring(0, prog.indexOf('}'));
        String[] opers = prog.split(";");
        for (int i = 0; i < opers.length; i++)
            opers[i] = opers[i].trim();

        Function.init(opers.length - 1);
        for (int i = 0; i < opers.length - 1; i++)
            Function.body[i] = new Operator(opers[i]);

        int ans = -1;
        for (int i = (1 << 15)-1; i >= 0; i--)
            if (Function.eval(i) == n) {
                ans = i;
                break;
            }

        out.println(ans);

        out.flush();
    }

}

class Function {
    static HashMap<Integer, Integer> func = new HashMap<Integer, Integer>();
    static Operator[] body;

    static void init(int n) {
        body = new Operator[n];
    }

    static int eval(int n) {
        if (func.containsKey(n)) return func.get(n);

        for (Operator oper : body) {
            int r = oper.eval(n);
            if (r != -1) {
                func.put(n, r);
                return r;
            }
        }
        //throw new RuntimeException();
        return -1;
    }
}

class Expression {
    static final int MD = 1 << 15;

    List<String> poliz;

    public Expression(String s) {
        s += "$";
        Stack<String> ops = new Stack<String>();
        poliz = new LinkedList<String>();
        int p = 0;
        char c = ' ';
        while (c != '$') {
            String cur = "";
            while (c <= ' ')
                c = s.charAt(p++);
            if (c == 'n') {
                cur = "n";
                c = s.charAt(p++);
                poliz.add(cur);
            } else if (c == 'f') {
                cur = "f";
                while (c != '(')
                    c = s.charAt(p++);
                c = s.charAt(p++);
                ops.push(cur);
            } else if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    cur += (char) c;
                    c = s.charAt(p++);
                }
                poliz.add(cur);
            } else {
                cur += (char) c;
                c = s.charAt(p++);
                if (cur.equals(")")) {
                    while (!ops.peek().equals("f"))
                        poliz.add(ops.pop());
                    poliz.add(ops.pop());
                } else if (cur.equals("+") || cur.equals("-"))
                    while (!ops.empty() && !ops.peek().equals("f"))
                        poliz.add(ops.pop());
                else
                    while (!ops.empty()
                            && (ops.peek().equals("*") || ops.peek()
                                    .equals("/")))
                        poliz.add(ops.pop());
                if (!cur.equals(")"))
                    ops.push(cur);
            }
        }
        while (!ops.empty())
            poliz.add(ops.pop());
    }

    public int eval(int n) {
        Stack<Integer> st = new Stack<Integer>();
        for (String s : poliz) {
            if (s.equals("n"))
                st.push(n);
            else if (s.charAt(0) >= '0' && s.charAt(0) <= '9')
                st.push(Integer.valueOf(s));
            else if (s.equals("f"))
                st.push(Function.eval(st.pop()));
            else {
                int b = st.pop(), a = st.pop();
                if (s.equals("+")) st.push((a + b)%MD);
                else if (s.equals("*")) st.push(a*b%MD);
                else if (s.equals("/")) st.push(a/b);
                else st.push((a - b + MD)%MD);
            }
        }
        return st.pop();
    }
}

class Condition {
    int sign;
    Expression left, right;

    public Condition(String s) {
        s = s.substring(1, s.length() - 1);
        int sgn = s.indexOf('<');
        if (sgn >= 0) {
            sign = -1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else if ((sgn = s.indexOf('>')) >= 0) {
            sign = 1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else {
            sgn = s.indexOf("==");
            sign = 0;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 2).trim());
        }
    }

    public boolean eval(int n) {
        int a = left.eval(n), b = right.eval(n);
        if (sign == -1)
            return a < b;
        else if (sign == 1)
            return a > b;
        else
            return a == b;
    }
}

class Operator {
    boolean isIf;
    Condition cond;
    Expression toReturn;

    public Operator(String s) {
        if (s.startsWith("if")) {
            int whereRet = s.indexOf("return");
            isIf = true;
            cond = new Condition(s.substring(2, whereRet).trim());
            toReturn = new Expression(s.substring(whereRet + 6).trim());
        } else {
            isIf = false;
            cond = null;
            toReturn = new Expression(s.substring(6).trim());
        }
    }

    public int eval(int n) {
        if (isIf && !cond.eval(n))
            return -1;

        return toReturn.eval(n);
    }
}

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class School1G {

    static BufferedReader in = new BufferedReader(new InputStreamReader(
            System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int n = Integer.valueOf(in.readLine());
        char[] buf = new char[200];
        int k = in.read(buf);

        String prog = String.valueOf(buf, 0, k);
        prog = prog.substring(prog.indexOf('{') + 1);
        prog = prog.substring(0, prog.indexOf('}'));
        String[] opers = prog.split(";");
        for (int i = 0; i < opers.length; i++)
            opers[i] = opers[i].trim();

        Function.init(opers.length - 1);
        for (int i = 0; i < opers.length - 1; i++)
            Function.body[i] = new Operator(opers[i]);

        int ans = -1;
        for (int i = (1 << 15)-1; i >= 0; i--)
            if (Function.eval(i) == n) {
                ans = i;
                break;
            }

        out.println(ans);

        out.flush();
    }

}

class Function {
    static HashMap<Integer, Integer> func = new HashMap<Integer, Integer>();
    static Operator[] body;

    static void init(int n) {
        body = new Operator[n];
    }

    static int eval(int n) {
        if (func.containsKey(n)) return func.get(n);

        for (Operator oper : body) {
            int r = oper.eval(n);
            if (r != -1) {
                func.put(n, r);
                return r;
            }
        }
        return -1;
    }
}

class Expression {
    static final int MD = 1 << 15;

    List<String> poliz;

    public Expression(String s) {
        s += "$";
        Stack<String> ops = new Stack<String>();
        poliz = new LinkedList<String>();
        int p = 0;
        char c = ' ';
        while (c != '$') {
            String cur = "";
            while (c <= ' ')
                c = s.charAt(p++);
            if (c == 'n') {
                cur = "n";
                c = s.charAt(p++);
                poliz.add(cur);
            } else if (c == 'f') {
                cur = "f";
                while (c != '(')
                    c = s.charAt(p++);
                c = s.charAt(p++);
                ops.push(cur);
            } else if (c >= '0' && c <= '9') {
                while (c >= '0' && c <= '9') {
                    cur += (char) c;
                    c = s.charAt(p++);
                }
                poliz.add(cur);
            } else {
                cur += (char) c;
                c = s.charAt(p++);
                if (cur.equals(")")) {
                    while (!ops.peek().equals("f"))
                        poliz.add(ops.pop());
                    poliz.add(ops.pop());
                } else if (cur.equals("+") || cur.equals("-"))
                    while (!ops.empty() && !ops.peek().equals("f"))
                        poliz.add(ops.pop());
                else
                    while (!ops.empty()
                            && (ops.peek().equals("*") || ops.peek()
                                    .equals("/")))
                        poliz.add(ops.pop());
                if (!cur.equals(")"))
                    ops.push(cur);
            }
        }
        while (!ops.empty())
            poliz.add(ops.pop());
    }

    public int eval(int n) {
        Stack<Integer> st = new Stack<Integer>();
        for (String s : poliz) {
            if (s.equals("n"))
                st.push(n);
            else if (s.charAt(0) >= '0' && s.charAt(0) <= '9')
                st.push(Integer.valueOf(s));
            else if (s.equals("f"))
                st.push(Function.eval(st.pop()));
            else {
                int b = st.pop(), a = st.pop();
                if (s.equals("+")) st.push((a + b)%MD);
                else if (s.equals("*")) st.push(a*b%MD);
                else if (s.equals("/")) st.push(a/b);
                else st.push((a - b + MD)%MD);
            }
        }
        return st.pop();
    }
}

class Condition {
    int sign;
    Expression left, right;

    public Condition(String s) {
        s = s.substring(1, s.length() - 1);
        int sgn = s.indexOf('<');
        if (sgn >= 0) {
            sign = -1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else if ((sgn = s.indexOf('>')) >= 0) {
            sign = 1;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 1).trim());
        } else {
            sgn = s.indexOf("=");
            sign = 0;
            left = new Expression(s.substring(0, sgn).trim());
            right = new Expression(s.substring(sgn + 2).trim());
        }
    }

    public boolean eval(int n) {
        int a = left.eval(n), b = right.eval(n);
        if (sign == -1)
            return a < b;
        else if (sign == 1)
            return a > b;
        else
            return a == b;
    }
}

class Operator {
    boolean isIf;
    Condition cond;
    Expression toReturn;

    public Operator(String s) {
        if (s.startsWith("if")) {
            int whereRet = s.indexOf("return");
            isIf = true;
            cond = new Condition(s.substring(2, whereRet).trim());
            toReturn = new Expression(s.substring(whereRet + 6).trim());
        } else {
            isIf = false;
            cond = null;
            toReturn = new Expression(s.substring(6).trim());
        }
    }

    public int eval(int n) {
        if (isIf && !cond.eval(n))
            return -1;

        return toReturn.eval(n);
    }
}