#include <bits/stdc++.h>
using namespace std;
vector<int> a(201), b(201);
int n, m;
int dp[201][2000];
int fun(int ind, int val) {
  if (ind >= n) return val;
  if (dp[ind][val] != (-1)) return dp[ind][val];
  int i, temp = INT_MAX, j;
  for (i = 0; i < m; i++) {
    j = fun(ind + 1, ((a[ind] & b[i]) | val));
    temp = min(temp, j);
  }
  return dp[ind][val] = temp;
}
int main() {
  int i, j;
  cin >> n >> m;
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < m; i++) cin >> b[i];
  for (i = 0; i < n; i++)
    for (j = 0; j < 2000; j++) dp[i][j] = (-1);
  cout << fun(0, 0);
  return 0;
}
