#include <bits/stdc++.h>
using namespace std;
vector<bool> primes(100006, true);
void sieve() {
  for (long long int i = 3; i <= 100006; i += 2) {
    primes[i] = 1;
  }
  for (long long int i = 3; i <= 100006; i++) {
    if (primes[i] == 1) {
      for (long long int j = i * i; j <= 100006; j += i) {
        primes[j] = 0;
      }
    }
  }
  primes[2] = 1;
  primes[0] = primes[1] = 0;
}
long long int recpow(long long int base, long long int exp) {
  long long int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((long long int)result * base);
    base = ((long long int)base * base);
    exp >>= 1;
  }
  return result;
}
const long long int N = 1e9 + 1;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  long long int a[n], b[m];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  for (long long int i = 0; i < m; i++) cin >> b[i];
  vector<long long int> x[n];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      x[i].push_back((a[i] & b[j]));
    }
  }
  for (long long int k = 0; k < 512; k++) {
    long long int oh = 0;
    for (long long int i = 0; i < n; i++) {
      long long int ok = 0;
      for (long long int j = 0; j < m; j++) {
        if ((k | x[i][j]) == k) {
          ok = 1;
          break;
        }
      }
      if (!ok) {
        oh = 1;
        break;
      }
    }
    if (!oh) {
      cout << k << "\n";
      break;
    }
  }
}
