#include <bits/stdc++.h>
using namespace std;
long long dp[205][515];
long long solve(long long n, long long val, vector<long long> &arr1,
                vector<long long> &arr2) {
  if (n == 0) {
    return val;
  }
  if (dp[n][val] != -1) {
    return dp[n][val];
  }
  long long ans = 600;
  for (long long j = 0; j < arr2.size(); j++) {
    long long temp = val | (arr1[n - 1] & arr2[j]);
    ans = min(ans, temp | solve(n - 1, temp, arr1, arr2));
  }
  return dp[n][val] = ans;
}
int32_t main(int32_t argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, m;
  cin >> n >> m;
  vector<long long> arr1(n);
  vector<long long> arr2(m);
  for (long long i = 0; i < n; i++) {
    cin >> arr1[i];
  }
  for (long long i = 0; i < m; i++) {
    cin >> arr2[i];
  }
  memset(dp, -1, sizeof(dp));
  long long ans = solve(n, 0, arr1, arr2);
  cout << ans << "\n";
  return 0;
}
