#include <bits/stdc++.h>
using namespace std;
int a[201], b[201];
int n, m;
int OO = 1e9;
int dp[201][10000];
int ansr = OO;
void solve(int i, int ans) {
  if (i == n) {
    ansr = min(ansr, ans);
    return;
  }
  int &ret = dp[i][ans];
  if (ret != -1) return;
  ret = 1;
  for (int j = 0; j < m; j++) {
    solve(i + 1, ((a[i] & b[j]) | ans));
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  solve(0, 0);
  cout << ansr;
  return 0;
}
