#include <bits/stdc++.h>
using namespace std;
const int maxn = 205;
int a[maxn], b[maxn];
int n, m;
bool check(int num) {
  for (int i = 1; i <= n; ++i) {
    bool flag = 0;
    for (int j = 1; j <= m; ++j) {
      if (((a[i] & b[j]) | num) == num) {
        flag = 1;
        break;
      }
    }
    if (!flag) return false;
  }
  return true;
}
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 ansLimit = 512;
  int minAns = 511;
  for (int i = 0; i < ansLimit; ++i) {
    if (check(i)) {
      minAns = i;
      break;
    }
  }
  printf("%d\n", minAns);
  return 0;
}
