#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<bool>> dp(n, vector<bool>(512, false));
  vector<vector<bool>> can(n, vector<bool>(512, false));
  vector<bool> nw(512, false);
  vector<int> a(n);
  vector<int> b(m);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int j = 0; j < m; ++j) cin >> b[j];
  for (int i = 0; i < n; ++i) {
    vector<bool> can(512, false);
    for (int j = 0; j < m; ++j) {
      if (i == 0) {
        dp[i][a[i] & b[j]] = true;
        can[a[i] & b[j]] = true;
      } else {
        for (int k = 0; k < 512; ++k) {
          if (nw[k]) can[k | (a[i] & b[j])] = true;
        }
      }
    }
    nw = can;
  }
  int ans = INT_MAX;
  for (int i = 0; i < 512; ++i) {
    if (nw[i]) {
      ans = i;
      break;
    }
  }
  cout << ans << "\n";
  return 0;
}
