#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n), b(m);
  for (auto &i : a) cin >> i;
  for (auto &i : b) cin >> i;
  for (long long i = 0; i < (1 << 9); i++) {
    long long x = 0;
    for (long long j = 0; j < a.size(); j++) {
      for (long long k = 0; k < b.size(); k++) {
        if (((a[j] & b[k]) | i) == i) {
          x++;
          break;
        }
      }
    }
    if (x == a.size()) {
      cout << i << endl;
      exit(0);
    }
  }
}
