#include <bits/stdc++.h>
using namespace std;
long long int inf = 1e17 + 7;
int solve() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> a(n), b(m);
  long long int ans = 0;
  for (auto &i : a) cin >> i;
  for (auto &i : b) cin >> i;
  long long int dp[n][m];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      dp[i][j] = a[i] & b[j];
    }
    sort(dp[i], dp[i] + m);
    ans = max(ans, dp[i][0]);
  }
  for (long long int i = 0; i < n; i++) {
    long long int temp = inf;
    for (long long int j = 0; j < m; j++) {
      temp = min(temp, ans | dp[i][j]);
    }
    ans = temp;
  }
  cout << ans << '\n';
  return 0;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int t = 1;
  while (t--) solve();
}
