#include <bits/stdc++.h>
using namespace std;
const long long N = 100005, INF = 2000000000000000000;
long long power(long long a, long long b, long long p) {
  if (a == 0) return 0;
  long long res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1) res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}
vector<long long> prime;
bool isprime[N];
void pre() {
  for (long long i = 2; i < N; i++) {
    if (isprime[i]) {
      for (long long j = i * i; j < N; j += i) isprime[j] = false;
      prime.push_back(i);
    }
  }
  return;
}
long long binarySearch(long long arr[], long long l, long long r, long long x) {
  if (r >= l) {
    long long mid = l + (r - l) / 2;
    if (arr[mid] == x) return mid;
    if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
bool isPowerOfTwo(long long n) {
  if (n == 0) return false;
  return (ceil(log2(n)) == floor(log2(n)));
}
long long gcd(long long a, long long b) {
  if (a < b)
    return gcd(b, a);
  else if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
bool isPerfectSquare(long double x) {
  long double sr = sqrt(x);
  return ((sr - floor(sr)) == 0);
}
void print(bool n) {
  if (n) {
    cout << "YES";
  } else {
    cout << "NO";
  }
}
int32_t main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  long long t[n][m], a[n], b[m], c[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < m; i++) cin >> b[i];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      t[i][j] = a[i] & b[j];
    }
    cout << "\n";
  }
  long long small = 0;
  for (long long i = 0; i < n; i++) {
    long long possible = INF;
    for (long long j = 0; j < m; j++) {
      long long x = a[i] & b[j];
      possible = min(possible, x);
    }
    small = max(small, possible);
  }
  long long ans = small;
  for (long long i = 0; i < n; i++) {
    long long possible = INF;
    for (long long j = 0; j < m; j++) {
      long long x = (ans) | (a[i] & b[j]);
      possible = min(possible, x);
    }
    ans = possible;
  }
  cout << ans << "\n";
}
