#include <bits/stdc++.h>
using namespace std;
long long n, m;
bool isvalid(long long x, long long *a, long long *b) {
  for (long long i = 0; i < n; i++) {
    bool poss = false;
    for (long long j = 0; j < m; j++) {
      long long res = a[i] & b[j];
      if ((res | x) == x) {
        poss = true;
        break;
      }
    }
    if (poss == false) {
      return false;
    }
  }
  return true;
}
int32_t main() {
  cin >> n >> m;
  long long a[n];
  long long b[m];
  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 < (1 << 9); i++) {
    if (isvalid(i, a, b)) {
      cout << i << " " << endl;
      break;
    }
  }
  return 0;
}
