#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int b[m];
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
  }
  set<int> s;
  for (int i = 0; i < m; ++i) {
    s.insert(a[0] & b[i]);
  }
  for (int i = 1; i < n; ++i) {
    set<int> temp;
    for (int j = 0; j < m; ++j) {
      int x = a[i] & b[j];
      for (auto k : s) {
        temp.insert(x | k);
      }
    }
    s = temp;
  }
  int ans = INT_MAX;
  for (auto i : s) ans = min(i, ans);
  cout << ans;
  return 0;
}
