#include <bits/stdc++.h>
using namespace std;
void PrintArrInt(int arr[], int s, int e) {
  for (int i = s; i <= e; i++) {
    if (i == s)
      printf("%d", arr[i]);
    else
      printf(" %d", arr[i]);
  }
  putchar('\n');
}
const int maxn = 200 + 5;
int N, M, a[maxn], b[maxn];
int main() {
  scanf("%d%d", &(N), &(M));
  for (int i = (1); i <= (N); i++) scanf("%d", &(a[i]));
  for (int i = (1); i <= (M); i++) scanf("%d", &(b[i]));
  int maxv = 0;
  for (int i = (1); i <= (N); i++) {
    int minv = INT_MAX;
    for (int j = (1); j <= (M); j++) minv = min(a[i] & b[j], minv);
    maxv = max(minv, maxv);
  }
  for (int i = (1); i <= (N); i++) {
    int minv = INT_MAX;
    for (int j = (1); j <= (M); j++) minv = min(maxv | (a[i] & b[j]), minv);
    maxv |= minv;
  }
  printf("%d\n", (maxv));
  return 0;
}
