#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int a[n], b[m];
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  sort(a, a + n, greater<int>());
  int res = 0;
  for (int i = 0; i <= 512; i++) {
    bool found = true;
    for (int j = 0; j < n; j++) {
      bool f = false;
      for (int k = 0; k < m; k++) {
        if (((a[j] & b[k]) | i) == i) {
          f = true;
          break;
        }
      }
      if (!f) {
        found = false;
        break;
      }
    }
    if (found) {
      res = i;
      break;
    }
  }
  cout << res;
}
