#include <bits/stdc++.h>
using namespace std;
int t[10];
int main() {
  int a[305];
  int b[305];
  int c[305];
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < m; ++i) cin >> b[i];
  memset(t, 0, sizeof(t));
  for (int cur = 10; cur >= 0; --cur) {
    int nowbit;
    for (int i = 0; i < n; ++i) {
      nowbit = (1 << cur);
      for (int j = 0; j < m; ++j) {
        int tmp = a[i] & b[j];
        bool flag = true;
        for (int k = 9; k >= cur + 1; --k) {
          if (t[k] == 0 && ((1 << k) & tmp)) flag = false;
        }
        if (!flag) continue;
        nowbit &= tmp;
      }
      t[cur] |= nowbit;
    }
  }
  int ans = 0;
  for (int i = 0; i < 10; ++i) ans += t[i];
  cout << ans << endl;
  return 0;
}
