#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (auto& e : a) cin >> e;
  for (auto& e : b) cin >> e;
  for (int mask = 0; mask < (1 << 9); ++mask) {
    bool poss = true;
    for (auto& e : a) {
      bool found = false;
      for (auto& f : b) {
        int val = e & f;
        if ((mask | val) == mask) {
          found = true;
          break;
        }
      }
      if (!found) {
        poss = false;
        break;
      }
    }
    if (poss) {
      cout << mask << '\n';
      return;
    }
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
