#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k, n, m, x;
  cin >> n >> m;
  int a[n], b[m], dp[n][1 << 9];
  for (i = 0; i < n; i++) cin >> a[i];
  for (j = 0; j < m; j++) cin >> b[j];
  memset(dp, 0, sizeof(dp));
  for (j = 0; j < m; j++) dp[0][a[0] & b[j]] = 1;
  for (i = 1; i < n; i++) {
    for (j = 0; j < m; j++) {
      x = a[i] & b[j];
      for (k = 0; k < (1 << 9); k++) dp[i][k | x] |= dp[i - 1][k];
    }
  }
  for (i = 0; i < (1 << 9); i++)
    if (dp[n - 1][i]) break;
  cout << i << endl;
}
