#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n), b(m);
  for (long long &i : a) cin >> i;
  for (long long &i : b) cin >> i;
  long long cur = 0;
  for (cur = 0; cur <= (1 << 9); cur++) {
    bool okk = true;
    for (long long i = 0; i < n; i++) {
      bool ok = false;
      long long c = 0;
      for (long long j = 0; j < m; j++) {
        c = (a[i] & b[j]);
        if ((c | cur) == cur) {
          ok = true;
          break;
        }
      }
      if (!ok) {
        okk = false;
        break;
      }
    }
    if (okk) {
      cout << cur << '\n';
      return 0;
    }
  }
  return 0;
}
