#include <bits/stdc++.h>
using namespace std;
const long long M = 1e7 + 10;
long long prime[M];
void precompute() {
  for (long long i = 2; i < M; i++) {
    if (!prime[i]) {
      for (long long j = 1; i * j < M; j++) prime[i * j] = i;
    }
  }
}
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int first(int num) {
  if (num == 0) return 0;
  int i = 1;
  while (num >> 1) {
    num = num >> 1;
    i++;
  }
  return i;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t, i, j, n, m, k, q, temp, x, y, z;
  cin >> n >> m;
  vector<long long> a(n);
  for (i = 0; i < n; i++) cin >> a[i];
  vector<long long> b(m);
  for (i = 0; i < m; i++) cin >> b[i];
  long long res = INT_MAX;
  vector<vector<bool>> p(n, vector<bool>(512, false));
  i = 0;
  for (auto n1 : a) {
    for (auto n2 : b) {
      p[i][n1 & n2] = true;
    }
    i++;
  }
  vector<vector<bool>> dp(n, vector<bool>(512, false));
  dp[0] = p[0];
  for (i = 1; i < n; i++) {
    for (int j = 0; j < 512; j++) {
      if (dp[i - 1][j]) {
        for (int k = 0; k < 512; k++) {
          if (p[i][k]) {
            dp[i][k | j] = true;
          }
        }
      }
    }
  }
  for (int i = 0; i < 512; i++) {
    if (dp[n - 1][i] == true) {
      cout << i << "\n";
      break;
    }
  }
  return 0;
}
