#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long a[n], 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 ans = 0; ans < 512; ans++) {
    long long cnt = 0;
    for (long long i = 0; i < n; i++) {
      long long ok = 0;
      for (long long j = 0; j < m; j++) {
        long long c = a[i] & b[j];
        if ((c | ans) == ans) {
          ok = 1;
          break;
        }
      }
      cnt += ok;
    }
    if (cnt == n) {
      cout << ans;
      return 0;
    }
  }
  return 0;
}
