#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vpii = vector<pair<int, int> >;
using vpll = vector<pair<long long, long long> >;
using vld = vector<long double>;
using vb = vector<bool>;
const int inf = (int)1e9 + 7;
const long long llinf = (long long)1e18 + 7;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n], b[m];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < m; i++) {
    cin >> b[i];
  }
  vll ar[n], br[n];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      ar[i].push_back(b[j]);
    }
  }
  long long ans = 0;
  for (long long k = 9; k >= 0; k--) {
    long long ke = 0;
    vll v;
    for (long long i = 0; i < n; i++) {
      v.clear();
      long long a1 = 1 << k;
      for (auto j : ar[i]) {
        if ((a1 & (j & a[i])) == 0) {
          v.push_back(j);
        }
      }
      if ((v.size()) == 0) {
        ke = 1;
        break;
      }
      br[i] = v;
    }
    if (ke == 1) {
      ans += 1 << k;
      continue;
    }
    for (long long i = 0; i < n; i++) {
      ar[i] = br[i];
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int ttt = 1;
  while (ttt--) {
    solve();
  }
}
