#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 207;
int a1[N], a2[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a1[i];
  for (int i = 0; i < m; i++) cin >> a2[i];
  for (int ms = 0; ms < (1LL << 10); ms++) {
    bool Gf = 1;
    for (int i = 0; i < n; i++) {
      int f = 0;
      for (int j = 0; j < m; j++) {
        if ((ms | (a1[i] & a2[j])) == ms) f = 1;
      }
      if (!f) Gf = 0;
    }
    if (Gf) return cout << ms << endl, 0;
  }
}
