#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  for (int i = 0; i < (m); ++i) cin >> b[i];
  int c[200][200];
  int e[200][200];
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) e[i][j] = true;
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) {
      c[i][j] = a[i] & b[j];
    }
  for (int d = 9; d >= 0; d--) {
    bool keseru = true;
    for (int i = 0; i < (n); ++i) {
      bool keserui = false;
      for (int j = 0; j < (m); ++j)
        if (e[i][j])
          if ((c[i][j] >> d & 1) == 0) keserui = true;
      if (!keserui) keseru = false;
    }
    if (keseru)
      for (int i = 0; i < (n); ++i)
        for (int j = 0; j < (m); ++j)
          if ((c[i][j] >> d & 1) == 1) e[i][j] = false;
  }
  int ans = 0;
  for (int i = 0; i < (n); ++i) {
    int cc;
    for (int j = 0; j < (m); ++j)
      if (e[i][j]) cc = c[i][j];
    ans |= cc;
  }
  cout << ans << endl;
  return 0;
}
