#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long>> pq1, pq2;
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    res *= a;
    b--;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, a1[200005], a2[200005], cnt[300] = {0};
  vector<pair<long long, long long>> v;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a1[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> a2[i];
  }
  long long ans = 1000000000000000000, xo = 0, tot = 0;
  while (tot < n) {
    for (int i = 0; i < n; ++i) {
      if (cnt[i] > 0) continue;
      long long fin = 1000000000000000000;
      for (int j = 0; j < m; ++j) {
        long long st1 = (a1[i] & a2[j]);
        long long st2 = xo | st1;
        fin = min(fin, st2);
      }
      long long st = abs(fin);
      v.push_back({st, i});
    }
    if (v.size() > 0) {
      sort(v.begin(), v.end());
      long long p = v[v.size() - 1].first;
      long long q = v[v.size() - 1].second;
      xo = xo | p;
      cnt[q]++;
      tot++;
    }
    v.clear();
  }
  cout << xo << endl;
  return 0;
}
