#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  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];
  int l = 512, ans;
  vector<int> answers(l + 1);
  for (int i = 0; i <= l; i++) answers[i] = i;
  for (int x : answers) {
    ans = x;
    for (int i = 0; i < n; i++) {
      int f = 0;
      for (int j = 0; j < m; j++) {
        int c = a[i] & b[j];
        if ((c | x) == x) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        ans = -1;
        break;
      }
    }
    if (ans != -1) break;
  }
  cout << ans;
  return 0;
}
