#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 99;
const long long INFLL = 2e18;
const int MOD = 1e9 + 7;
const long long MODLL = 10e9 + 7;
const int N = 2000 + 10;
int fx[8] = {+0, +0, +1, -1, +1, +1, -1, -1};
int fy[8] = {+1, -1, +0, +0, +1, -1, +1, -1};
int bit[32];
int a[N], b[N];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < m; ++i) cin >> b[i];
  sort(a, a + n, greater<int>());
  int ans = INF;
  for (int i = 0; i < m; ++i) {
    int cur = a[0] & b[i];
    for (int j = 1; j < n; ++j) {
      int mn = INF;
      for (int k = 0; k < m; ++k) mn = min(mn, cur | (a[j] & b[k]));
      cur = mn;
    }
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}
