#include <bits/stdc++.h>
using namespace std;
template <class T>
T modulo(T x, T p) {
  if (x >= 0) return x % p;
  return p + x % p;
}
template <class T>
T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
T lcm(T a, T b) {
  return a * b / gcd(a, b);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  vector<vector<int> > c(n, vector<int>(m));
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int j = 0; j < m; j++) cin >> b[j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) c[i][j] = (a[i] & b[j]);
  int ans = -1, test = 0;
  while (ans == -1) {
    bool found = true;
    for (int i = 0; i < n; i++) {
      bool pos = false;
      for (int j = 0; j < m; j++) {
        if ((c[i][j] | test) == test) {
          pos = true;
          break;
        }
      }
      if (!pos) {
        found = false;
        break;
      }
    }
    if (found)
      ans = test;
    else
      test++;
  }
  cout << ans;
  return 0;
}
