#include <bits/stdc++.h>
using namespace std;
long long power(long long x, unsigned 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;
}
long long modInverse(long long n, long long p) { return power(n, p - 2, p); }
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long T = 1;
  while (T--) {
    long long n, m;
    cin >> n >> m;
    vector<long long> a(n), b(m);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (long long i = 0; i < m; i++) {
      cin >> b[i];
    }
    long long ans = 1000000007;
    for (long long k = 0; k < 1000; k++) {
      long long f1 = 0;
      for (long long i = 0; i < n; i++) {
        long long f = 0;
        for (long long j = 0; j < m; j++) {
          if (((a[i] & b[j]) | k) == k) {
            f = 1;
            break;
          }
        }
        if (f)
          continue;
        else {
          f1 = 1;
          break;
        }
      }
      if (!f1) {
        ans = k;
        break;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
