#include <bits/stdc++.h>
using namespace std;
int b[205];
int a[205];
int main() {
  int n, m;
  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]);
  for (int k = 0; k <= (1 << 9) - 1; k++) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      int flag2 = 0;
      for (int j = 1; j <= m; j++) {
        if (((a[i] & b[j]) | k) == k) {
          cnt++;
          flag2 = 1;
          break;
        }
      }
    }
    if (cnt == n) {
      printf("%d\n", k);
      break;
    }
  }
  return 0;
}
