#include <bits/stdc++.h>
using namespace std;
template <class T>
T modulo(T x, T p) {
  if (x >= 0) return x % p;
  return p + x % p;
}
template <class T>
T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
T lcm(T a, T b) {
  return a * b / gcd(a, b);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  vector<vector<int> > dp(n, vector<int>(m));
  vector<int> mindp(n, 2000000000);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) dp[i][j] = a[i] & b[j];
  int maxdp, notmaxdp, minj, sol = 0;
  vector<int> used(n, 0);
  for (int i = 0; i < n; i++) {
    maxdp = -1;
    for (int j = 0; j < n; j++) {
      if (used[j]) continue;
      for (int k = 0; k < m; k++) mindp[j] = min(mindp[j], dp[j][k]);
      if (maxdp < mindp[j]) {
        maxdp = mindp[j];
        minj = j;
      }
    }
    sol = maxdp | sol;
    used[minj] = 1;
    notmaxdp = 0;
    for (int j = 0; j < 11; j++) {
      if ((maxdp & 1) == 0) notmaxdp += 1 << j;
      maxdp = maxdp >> 1;
    }
    for (int j = 0; j < n; j++)
      for (int k = 0; k < m; k++) {
        dp[j][k] = dp[j][k] & notmaxdp;
      }
  }
  cout << sol;
  return 0;
}
