#include <bits/stdc++.h>
using namespace std;
const int sz = 210;
const int Max = 1e9 + 100;
int dp[sz][600];
int ara[sz], bara[sz];
int n, m;
int cholo(int id, int tot) {
  if (id >= n) return tot;
  int &ret = dp[id][tot];
  if (~ret) return ret;
  int mx = Max;
  for (int i = 0; i < m; i++) {
    int temp = tot;
    temp |= (ara[id] & bara[i]);
    mx = min(mx, cholo(id + 1, temp));
  }
  return (ret = mx);
}
int main() {
  int a, b, c, i, j, k, q, p, x, y, ct, ct1, l, r, x1, y1, mn, h, sum1, in, z,
      mid, mx;
  char ch;
  double d;
  string str1, str2, str;
  bool bl, bl1;
  int t, cs = 1;
  cin >> n >> m;
  for (i = 0; i < n; i++) cin >> ara[i];
  for (i = 0; i < m; i++) cin >> bara[i];
  memset(dp, -1, sizeof dp);
  int ans = cholo(0, 0);
  cout << ans << '\n';
}
