#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  int a[n];
  int b[m];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
  }
  for (int k = 0; k < (int)pow(2, 9); ++k) {
    bool found = 0;
    for (int i = 0; i < n; ++i) {
      bool temp = 0;
      for (int j = 0; j < m; ++j) {
        if (((a[i] & b[j]) | k) == k) {
          temp = 1;
          break;
        }
      }
      if (!temp) {
        found = 1;
        break;
      }
    }
    if (!found) {
      cout << k << '\n';
      break;
    }
  }
}
