#include <bits/stdc++.h>
using namespace std;
bool sortcol(const vector<int>& v1, const vector<int>& v2) {
  return v1[0] < v2[0];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  int n, m;
  cin >> n >> m;
  int a[n + 1], b[m + 1];
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) cin >> b[i];
  for (int i = 0; i <= 512; i++) {
    int br = 0;
    for (int j = 1; j <= n; j++) {
      bool found = false;
      for (int k = 1; k <= m; k++) {
        if ((i & (a[j] & b[k])) == (a[j] & b[k])) {
          found = true;
        }
      }
      if (found) {
        br++;
      }
    }
    if (br == n) {
      cout << i;
      return 0;
    }
  }
  return 0;
}
