#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
string trim(const string& str) {
  size_t first = str.find_first_not_of(' ');
  if (string::npos == first) {
    return str;
  }
  int last = str.find_last_not_of(' ');
  return str.substr(first, (last - first + 1));
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(15);
  int n, m;
  cin >> n >> m;
  int a[n], b[m];
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  int ans;
  for (int mask = pow(2, 9) - 1; mask >= 0; mask--) {
    bool ch;
    for (int i = 0; i < n; i++) {
      ch = false;
      for (int k = 0; k < m; k++) {
        int c = a[i] & b[k];
        if ((c | mask) == mask) {
          ch = true;
        }
      }
      if (!ch) {
        break;
      }
    }
    if (ch) {
      ans = mask;
    }
  }
  cout << ans << '\n';
  cerr << "Time elapsed" << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
  return 0;
}
