#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> a, b;
bool chk(int val) {
  bitset<32> bb(val);
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    bool f = 0, kk = 0;
    for (int j = 0; j < m; ++j) {
      bitset<32> in(a[i] & b[j]);
      f = 0;
      for (int k = 0; k < 32; ++k) {
        if (bb[k] == 0 && in[k] == 1) {
          f = 1;
        }
      }
      if (f == 0) {
        kk = 1;
        break;
      }
    }
    if (kk) {
      cnt++;
    }
  }
  if (cnt < n) return false;
  return true;
}
int main() {
  cin >> n >> m;
  a.resize(n);
  b.resize(m);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int j = 0; j < m; ++j) cin >> b[j];
  int lp = (1 << 9);
  for (int i = 0; i <= lp; ++i) {
    if (chk(i)) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
