#include <bits/stdc++.h>
using namespace std;
int n, m, a[205], b[205];
bool ok(int ans) {
  for (int i = 0; i < n; ++i) {
    bool cant = 1;
    for (int j = 0; j < m; ++j) {
      if (((a[i] & b[j]) | ans) == ans) cant = 0;
    }
    if (cant) return false;
  }
  return true;
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < m; ++i) cin >> b[i];
  for (int ans = 0; ans <= (1 << 9); ++ans) {
    if (ok(ans)) {
      cout << ans;
      return;
    }
  }
}
int main() {
  solve();
  return 0;
}
