#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
inline long long read() {
  long long X = 0;
  bool flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    X = (X << 1) + (X << 3) + ch - '0';
    ch = getchar();
  }
  if (flag) return X;
  return ~(X - 1);
}
long long n, m, k;
long long ar[N], br[N];
long long ans;
vector<int> vt[250];
void solvve() {
  n = read();
  m = read();
  for (register int i(1); i <= n; ++i) {
    ar[i] = read();
  }
  for (register int i(1); i <= m; ++i) br[i] = read();
  for (register int i(1); i <= n; ++i) {
    for (register int j(1); j <= m; ++j) {
      vt[i].push_back(ar[i] & br[j]);
    }
  }
  for (register int i(0); i <= (1 << 9); ++i) {
    int res = 0;
    int ok = 0;
    for (register int j(1); j <= n; ++j) {
      int f = 0;
      for (int kk = 0; kk < vt[j].size(); kk++) {
        if ((vt[j][kk] | i) == i) {
          f = 1;
          res |= vt[j][kk];
        }
      }
      if (f == 0)
        break;
      else if (j == n)
        ok = 1;
    }
    if (res == i && ok) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
}
int main() {
  { solvve(); }
  return 0;
}
