#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m, i, j, k;
  cin >> n >> m;
  int arrn[n];
  for (i = 0; i < n; i++) cin >> arrn[i];
  int arrm[m];
  for (i = 0; i < m; i++) cin >> arrm[i];
  for (i = 0; i <= 512; i++) {
    int c = i;
    int f = 1;
    for (j = 0; j < n; j++) {
      int tt = 0;
      for (k = 0; k < m; k++) {
        int dd = arrn[j] & arrm[k];
        int pp = dd | c;
        if (pp == c) {
          tt = 1;
          break;
        }
      }
      if (tt == 0) {
        f = 0;
        break;
      }
    }
    if (f) {
      cout << c;
      break;
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t > 0) {
    t--;
    solve();
  }
  return 0;
}
