#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)
#define FOR(it,c) for(__typeof((c).begin())it=(c).begin(); it!=(c).end();++it)
#define RFOR(it,c) for(__typeof((c).rbegin())it=(c).rbegin(); it!=(c).rend();++it)
#define exist(c, v) (find((c).begin(), (c).end(), (v)) != (c).end())

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << '(' << p.first << ", " << p.second << ')';
	return os;
}

template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
	copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
	return os;
}

const int INF = 0xfffffff;
const string candidate = "0123456789|^&-+*";
int idx;

int or_expr(int&, const string&);

int stoi(const string& s) {
	istringstream is(s);
	int res;
	is >> res;
	return res;
}

int factor(int& idx, const string& s) {
	if(isdigit(s[idx]) && s[idx] != '0') {
		int end = idx + 1;
		while(isdigit(s[end])) ++end;
		const int res = stoi(s.substr(idx, end - idx));
		idx = end;
		return res;
	}
	else if(s[idx++] == '(') {
		const int res = or_expr(idx, s);
		if(s[idx++] != ')')
			throw 0;

		return res;		
	}
	else
		throw 0;
}

int term(int& idx, const string& s) {
	int res = factor(idx, s);
	while(s[idx] == '*') {
		++idx;
		res *= factor(idx, s);
	}

	return res;	
}

int expr(int& idx, const string& s) {
	int res = term(idx, s);
	while(s[idx] == '+' || s[idx] == '-') {
		if(s[idx++] == '+')
			res += term(idx, s);
		else
			res -= term(idx, s);
	}

	return res;	
}

int and_expr(int& idx, const string& s) {
	int res = expr(idx, s);
	while(s[idx] == '&') {
		++idx;
		res &= expr(idx, s);
	}

	return res;	
}

int xor_expr(int& idx, const string& s) {
	int res = and_expr(idx, s);
	while(s[idx] == '^') {
		++idx;
		res ^= and_expr(idx, s);
	}

	return res;	
}

int or_expr(int& idx, const string& s) {
	int res = xor_expr(idx, s);
	while(s[idx] == '|') {
		++idx;
		res |= xor_expr(idx, s);
	}

	return res;	
}

int min_max(const string& s, int depth, bool first) {
	try {
		int idx = 0;
		const int result = or_expr(idx, s);
		if(idx != s.size())
			throw 0;

		if(depth == 0)
			return result;

	} catch(...) {
		return first ? INT_MAX : -INT_MAX;
	}

	const int next_depth = depth - 1;
	int res = (first ? -INT_MAX : INT_MAX);
	for(int i = 0; i < (int)s.size(); ++i) {
		if(s[i] == '(' || s[i] == ')')
			continue;

		string tmp = s;
		tmp.erase(tmp.begin() + i);
		const int value = min_max(tmp, next_depth, !first);
		if(first)
			chmax(res, value);
		else
			chmin(res, value);
	}

	for(int i = 0; i <= (int)s.size(); ++i) {
		for(int j = 0; j < (int)candidate.size(); ++j) {
			string tmp = s;
			tmp.insert(tmp.begin() + i, candidate[j]);
			const int value = min_max(tmp, next_depth, !first);
			if(first)
				chmax(res, value);
			else
				chmin(res, value);
		}
	}

	return res;
}

int main() {
	int n;
	cin.tie(0);
	ios::sync_with_stdio(false);

	for(int n; cin >> n, n;) {
		n = (n & 1 ? 1 : 2);

		string s;
		cin >> s;
		cout << min_max(s, n, true) << endl;
	}

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1304