#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long a[N], b[N];
long long n, m, res;
bool check(long long x) {
  for (long long i = 1; i <= n; i++) {
    bool ok = false;
    for (long long j = 1; j <= m; j++) {
      res = a[i] & b[j];
      if ((res | x) == x) {
        ok = true;
        break;
      }
    }
    if (!ok) return false;
  }
  return true;
}
void sol() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= m; i++) cin >> b[i];
  long long st = 0;
  for (long long i = 1; i <= n; i++) st |= a[i];
  for (long long ans = 0; ans <= st; ans++) {
    if (check(ans)) {
      cout << ans << "\n";
      return;
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  t = 1;
  while (t--) {
    sol();
  }
  return 0;
}
