#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
int n, m, a[205], b[205], res;
vector<int> v[205], tmp[205];
signed main() {
  n = read(), m = read();
  for (register int i = (1); i <= (n); ++i) a[i] = read();
  for (register int i = (1); i <= (m); ++i) b[i] = read();
  for (register int i = (1); i <= (n); ++i)
    for (register int j = (1); j <= (m); ++j) v[i].push_back(j);
  for (register int p = (9); p >= (0); --p) {
    bool ok = 1;
    for (register int i = (1); i <= (n); ++i) {
      int x = a[i];
      if (!(x >> p & 1)) continue;
      tmp[i].clear();
      for (int j = 0; j < v[i].size(); ++j) {
        int y = b[v[i][j]];
        if (!(y >> p & 1)) tmp[i].push_back(v[i][j]);
      }
      if (tmp[i].size() == 0) {
        ok = 0;
        break;
      }
    }
    if (!ok)
      res += (1 << p);
    else
      for (register int i = (1); i <= (n); ++i)
        if (a[i] >> p & 1) v[i] = tmp[i];
  }
  cout << res;
  return 0;
}
