#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
const int maxn = 205;
long long n, m, a[maxn], b[maxn], c[10];
bool is_ans(int ans) {
  for (int i = 0; i < 9; ++i) c[i] = ans >> i & 1;
  int f;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      long long q = a[i] & b[j];
      f = 1;
      for (int k = 0; k < 9; ++k) {
        if (q >> k & 1 == 1 && c[k] != 1) {
          f = 0;
          break;
        }
      }
      if (f == 1) break;
    }
    if (f == 0) return false;
  }
  return true;
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= m; ++i) b[i] = read();
  for (int ans = 0; ans < 1 << 9; ++ans) {
    if (is_ans(ans)) {
      printf("%d\n", ans);
      return 0;
    }
  }
  return 0;
}
