#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int t;
  t = 1;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    vector<int> b(m);
    for (int &x : a) cin >> x;
    for (int &x : b) cin >> x;
    int res = -1;
    for (int ans = 0; ans < (1 << 9); ++ans) {
      bool f = true;
      for (int i = 0; i < n; ++i) {
        f = false;
        for (int j = 0; j < m; ++j) {
          if (((a[i] & b[j]) | ans) == ans) {
            f = true;
            break;
          }
        }
        if (!f) break;
      }
      if (f) res = ans;
      if (res != -1) break;
    }
    cout << res << '\n';
  }
  return 0;
}
