#include <bits/stdc++.h>
using namespace std;
long long int power[21] = {0};
void solve() {
  power[0] = 1;
  for (long long int j = 1; j <= 20; j++) {
    power[j] = power[j - 1] * 2;
  }
  long long int n, m;
  cin >> n >> m;
  vector<long long int> a(n), b(m);
  for (long long int j = 0; j < n; j++) cin >> a[j];
  for (long long int j = 0; j < m; j++) cin >> b[j];
  long long int ans = 0;
  vector<set<long long int>> pos(n);
  set<long long int> dq;
  for (long long int j = 0; j < m; j++) {
    dq.insert(j);
  }
  for (long long int j = 0; j < n; j++) {
    pos[j] = dq;
  }
  for (long long int p = 10; p >= 0; p--) {
    bool f = true;
    for (long long int j = 0; j < n; j++) {
      if ((power[p] & a[j]) == 0) {
        continue;
      } else {
        bool f1 = false;
        for (auto it = pos[j].begin(); it != pos[j].end(); it++) {
          long long int k = b[*it];
          if ((k & power[p]) == 0) {
            f1 = true;
            break;
          }
        }
        if (f1 == false) {
          f = false;
          break;
        }
      }
    }
    if (f) {
      for (long long int j = 0; j < n; j++) {
        if ((power[p] & a[j]) == 0) {
          continue;
        } else {
          set<long long int> s;
          for (auto it = pos[j].begin(); it != pos[j].end(); it++) {
            long long int k = b[*it];
            if ((power[p] & k) == 0) {
              s.insert(*it);
            }
          }
          pos[j] = s;
        }
      }
    } else {
      ans += power[p];
    }
  }
  cout << ans << "\n";
  return;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
