#include <bits/stdc++.h>

#define MAX 50
#define SQRDMAX 2500
#define K 1

using namespace std;

int si, sj, tiles[SQRDMAX], weights[SQRDMAX];
list<tuple<int, int>> path;
bitset<SQRDMAX> visited;
const int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void setTile(int i, int j) {
  visited.flip(i * MAX + j);
  for (const int* dir : directions) {
    int i_ = i + dir[0];
    int j_ = j + dir[1];
    int cell;
    cell = i_ * MAX + j_;
    if (0 <= i_ && i_ < MAX && 0 <= j_ && j_ < MAX && tiles[cell] == tiles[i * MAX + j]) {
      visited.flip(cell);
      break;
    }
  }
}


tuple<int, list<tuple<int, int>>> dfs(int i, int j) {
  priority_queue<tuple<int, int, int>> q;
  //get valid adj
  for (const int* dir : directions) {
    int i_ = i + dir[0];
    int j_ = j + dir[1];

    int cell;
    cell = i_ * MAX + j_;
    if (0 <= i_ && i_ < MAX && 0 <= j_ && j_ < MAX && tiles[cell] != tiles[i * MAX + j] && !visited.test(cell)) {
      q.push(make_tuple(weights[i_ * MAX + j_], i_, j_));
    }
  }
  //dfs
  int bestPathWeight = 0;
  list<tuple<int, int>> bestPath;
  //get k best choices
  for (int k = 0; k < K && !q.empty(); ++k) {
    int weight, i_, j_;
    tie(weight, i_, j_) = q.top();
    q.pop();
    setTile(i_, j_);
//    cout<<visited.count()<<endl;
    int pathWeight;
    list<tuple<int, int>> path;
    tie(pathWeight, path) = dfs(i_, j_);
    setTile(i_, j_);
    if (weight + pathWeight > bestPathWeight) {
      bestPathWeight = weight + pathWeight;
      bestPath = path;
    }
//    cout<<visited.count()<<endl;
  }
  bestPath.push_front(make_tuple(i, j));
  return make_tuple(weights[i * MAX + j] + bestPathWeight, bestPath); 
}

int main(void) {
  scanf("%d%d", &si, &sj);
  for (int i = 0; i < SQRDMAX; ++i) 
    scanf("%d", &tiles[i]);
  for (int i = 0; i < SQRDMAX; ++i) 
    scanf("%d", &weights[i]);
  setTile(si, sj);
  int pathWeight;
  list<tuple<int, int>> path;
  tie(pathWeight, path) = dfs(si, sj);
  setTile(si, sj);
  tuple<int, int> prev = make_tuple(si, sj);
  string res = "";
  for (const tuple<int, int>& t : path) {
    int i, j, i_, j_;
    tie(i, j) = t;
    tie(i_, j_) = prev;
//    if (abs(i - i_) > 1 || abs(j - j_) > 1 || visited.test(i * MAX + j))
//      cout<<"Error"<<endl;
    if (i > i_)
      res += "D";
    else if (i < i_)
      res += "U";
    else if (j < j_)
      res += "L";
    else if (j > j_)
      res += "R";
    setTile(i, j);
    prev = t;
//    cout<<i<<" "<<j<<endl;
  }
  cout<<res<<endl;
  return 0;
}

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int n = ri();
	int a[n];
	for (auto &i : a) i = ri();
	
	int res = 2000000000;
	for (int i = 0; i < 1 << (n - 1); i++) {
		int xored = 0;
		int ored = 0;
		for (int j = 0; j <= n; j++) {
			if (j < n) ored |= a[j];
			if (j == n || (i >> j & 1)) xored ^= ored, ored = 0;
		}
		res = std::min(res, xored);
	}
	
	printf("%d\n", res);
	return 0;
}

