#include <bits/stdc++.h>
int n, m;
int a[202], b[202];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", &b[i]);
  }
  for (int w = 0; w <= 1 << 9; ++w) {
    int o = 0;
    for (int i = 0; i < n; ++i) {
      int mx = -1;
      for (int j = 0; j < m; ++j) {
        if ((w & (a[i] & b[j])) == (a[i] & b[j])) {
          mx = std::max(a[i] & b[j], mx);
        }
      }
      if (mx == -1) {
        o = -1;
        break;
      }
      o |= mx;
    }
    if (o == w) {
      printf("%d\n", w);
      return 0;
    }
  }
  return 0;
}
