#include <bits/stdc++.h>
using namespace std;
int largestBitOn(int value) {
  int ret = -1;
  for (int i = 0; i < 32; i++) {
    if (value & 1 << i) ret = i;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> as(n);
  for (int& a : as) cin >> a;
  vector<int> bs(m);
  for (int& b : bs) cin >> b;
  vector<vector<int>> ad(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) ad[i][j] = as[i] & bs[j];
    sort(ad[i].begin(), ad[i].end());
  }
  int ans = 0;
  while (true) {
    int maxSmallest = ad[0][0];
    for (int i = 1; i < n; i++) maxSmallest = max(maxSmallest, ad[i][0]);
    int cnt = largestBitOn(maxSmallest);
    if (cnt == -1) {
      cout << ans << '\n';
      return 0;
    }
    ans |= 1 << cnt;
    for (int i = 0; i < n; i++) {
      int okay = 0;
      for (auto it = ad[i].begin(); it != ad[i].end(); it++) {
        if (cnt < 31 && *it >= (1 << (cnt + 1))) {
          break;
        }
        okay++;
        *it &= ~(1 << cnt);
      }
      int bad = ad[i].size() - okay;
      while (bad--) {
        ad[i].pop_back();
      }
      sort(ad[i].begin(), ad[i].end());
    }
  }
}
