#include <bits/stdc++.h>
using namespace std;
const int maxn = 210, N = 1023;
int n, m, tp;
int a[maxn], b[maxn];
bool p[2000];
void dfs(int now, int x, int y) {
  if (tp != 0) return;
  if (now == n + 1) {
    if (x == y) tp = 1;
    return;
  }
  int flag = 0;
  for (int i = (1); i <= (int)(m); ++i) {
    if (tp != 0) return;
    int num = a[now] & b[i];
    if (p[num]) dfs(now + 1, x | num, y), flag = 1;
  }
  if (!flag) tp = -1;
}
bool check(int x) {
  for (int i = (1); i <= (int)(N); ++i) p[i] = 0;
  p[0] = 1;
  for (int i = x; i > 0; i = (i - 1) & x) {
    p[i] = 1, p[x ^ i] = 1;
  }
  tp = 0;
  dfs(1, 0, x);
  return tp == 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (int)(n); ++i) scanf("%d", &a[i]);
  for (int i = (1); i <= (int)(m); ++i) scanf("%d", &b[i]);
  for (int i = (0); i <= (int)(N); ++i) {
    if (check(i)) {
      printf("%d\n", i);
      break;
    }
  }
  return 0;
}
