#include <bits/stdc++.h>
using namespace std;
const long long maxS = 2e5 + 5;
long long N, M;
long long A[201], B[201];
long long DP[201][515];
long long go(long long pos, long long prev) {
  if (pos == N + 1) return 0;
  if (DP[pos][prev] != -1) return DP[pos][prev];
  long long ans = INT_MAX;
  for (long long i = 1; i <= M; i++)
    ans = min(ans,
              (prev | (A[pos] & B[i]) | go(pos + 1, (prev | (A[pos] & B[i])))));
  return DP[pos][prev] = ans;
}
void code_idhr_h() {
  cin >> N >> M;
  for (long long i = 1; i <= N; i++) cin >> A[i];
  for (long long i = 1; i <= M; i++) cin >> B[i];
  memset(DP, -1, sizeof DP);
  cout << go(1, 0) << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long T = 1;
  for (long long t = 1; t <= T; t++) {
    code_idhr_h();
  }
  return 0;
}
