#include <bits/stdc++.h>
using namespace std;
vector<int> a(210);
vector<int> b(210);
int n, m;
vector<int> getBits(int ans) {
  vector<int> ret(10);
  for (int i = 0; i < 10; i++) ret[i] = (ans & (1 << i));
  return ret;
}
bool valid(vector<int> &bits, int ans, int currAnd) {
  vector<int> bitA = getBits(currAnd);
  for (int i = 0; i < 10; i++) {
    if (!bitA[i]) continue;
    if (!bits[i]) return false;
  }
  return true;
}
bool ok(int ans) {
  int resp = 0;
  vector<int> bits = getBits(ans);
  for (int i = 0; i < n; i++) {
    int currSolved = -1;
    for (int j = 0; j < m; j++) {
      int currAnd = a[i] & b[j];
      if (!valid(bits, ans, currAnd)) continue;
      currSolved = max(currSolved, currAnd);
    }
    if (currSolved == -1) return false;
    resp |= currSolved;
  }
  return resp == ans;
}
void solve() {
  cin >> n >> m;
  for (int k = 0; k < n; k++) cin >> a[k];
  for (int k = 0; k < m; k++) cin >> b[k];
  for (int ans = 0; ans < 512; ans++)
    if (ok(ans)) {
      cout << ans << endl;
      return;
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
