#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
bool typetest;
inline void fastIOfileinput() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  typetest = 0;
}
const int N = 2e2 + 5;
int n, m;
int a[N], b[N], c;
bool ck[N], can[N][N];
void Read() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= m; ++i) cin >> b[i];
}
bool Check(int v) {
  for (int i = 1; i <= n; ++i) {
    bool ok = 1;
    for (int j = 1; j <= m; ++j) {
      for (int t = 0; t < 9; ++t)
        if ((((a[i] & b[j]) >> (t)) & 1) && !(((v) >> (t)) & 1)) goto done;
      ok = 0;
    done:;
      if (!ok) break;
    }
    if (ok) return false;
  }
  return true;
}
void Solve() {
  for (int i = 0; i < (1 << 9); ++i) {
    if (Check(i)) {
      cout << i;
      return;
    }
  }
}
int main() {
  fastIOfileinput();
  if (typetest) {
    int t;
    cin >> t;
    while (t--) {
      Read();
      Solve();
    }
  } else {
    Read();
    Solve();
  }
}
