#include <sys/time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = int64_t;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) FOR(i, 0, n)
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int INF = 1000000003;
constexpr ll LINF = 1000000000000000003;

constexpr int BEAM_SIZE = 10000;
constexpr int N = 50;
int tile[50][50];
int board[50][50];
int dirx[4] = {1, -1, 0, 0}, diry[4] = {0, 0, 1, -1};
char op[4] = {'D', 'U', 'R', 'L'};

inline bool in_board(int x, int y) {
    return x >= 0 && x < N && y >= 0 && y < N;
}

struct beam_search {
    struct State {
        int px, py;
        int score;
        bitset<2048> is_used;
        string ops;

        State(int _px, int _py) : px(_px), py(_py), score(), is_used() {
            score += board[px][py];
            is_used.set(tile[px][py]);
        }

        bool operator<(const State &rhs) const { return eval() < rhs.eval(); }
        bool operator>(const State &rhs) const { return rhs < *this; }

        int eval() const { return score; }

        void update_score(int row) {}

        void _place(int dir) {
            px += dirx[dir];
            py += diry[dir];
            score += board[px][py];
            is_used.set(tile[px][py]);
            ops.push_back(op[dir]);
        }

        State place(int dir) {
            State res(*this);
            res._place(dir);
            return res;
        }

        bool is_place(int dir) {
            int x = px + dirx[dir];
            int y = py + diry[dir];
            return in_board(x, y) && !is_used[tile[x][y]];
        }

        string answer() const {
            return ops;
        }
    };

    string solve(int sx, int sy) {
        priority_queue<State> cur, nxt;
        cur.push(State(sx, sy));
        State res(sx, sy);
        while (!cur.empty() && (double)clock() / CLOCKS_PER_SEC < 1.95) {
            while (!nxt.empty()) nxt.pop();
            while (!cur.empty()) {
                State now = cur.top();
                cur.pop();
                if (cur.empty()) chmax(res, now);
                for (int dir = 0; dir < 4; ++dir) {
                    if (!now.is_place(dir))
                        continue;
                    State s = now.place(dir);
                    if (nxt.size() < BEAM_SIZE || s.eval() > nxt.top().eval())
                        nxt.push(s);
                    if (nxt.size() > BEAM_SIZE) nxt.pop();
                }
            }
            swap(cur, nxt);
        }

        cerr << res.eval() << endl;
        return res.answer();
    }
};

int main(void) {
    int sx, sy;
    cin >> sx >> sy;
    rep(i, N) {
        rep(j, N) cin >> tile[i][j];
    }
    rep(i, N) {
        rep(j, N) cin >> board[i][j];
    }

    beam_search solver;
    cout << solver.solve(sx, sy) <<endl;

    return 0;
}

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int main() {
    ll N; cin>>N;
    vector<ll> A(N);
    rep(i, N) cin>>A[i];

    int n = N-1;
    ll res = -1;
    for (int bit = 0; bit < (1<<n); ++bit) {
        ll xxor = 0;
        queue<ll> oor;
        oor.push(A[0]);
        for (int ii = 0; ii < n; ++ii) {
            if (bit & (1 << ii)) {
                ll o = 0;
                while(!oor.empty()){
                    o |= oor.front();
                    oor.pop();
                }
                xxor ^= o;
            }
            oor.push(A[ii+1]);
        }
        ll o = 0;
        while(!oor.empty()){
            o |= oor.front();
            oor.pop();
        }
        xxor ^= o;
        if(res == -1) res = xxor;
        else chmin(res, xxor);
    }
    cout << res << endl;
}