#include <bits/stdc++.h>
using namespace std;
int n, m, a[205], b[205], rs = INT_MAX;
vector<int> v[205];
int dd[205][5000];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= m; ++i) cin >> b[i];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) v[i].push_back((a[i] & b[j]));
  for (int i = 0; i <= (int)v[1].size() - 1; ++i) dd[1][v[1][i]] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j <= (int)v[i].size() - 1; ++j) {
      for (int k = 0; k <= 2000; ++k) {
        if (dd[i - 1][k]) dd[i][(k | v[i][j])] = 1;
      }
    }
  }
  for (int k = 0; k <= 2000; ++k)
    if (dd[n][k]) rs = min(rs, k);
  cout << rs;
  return 0;
}
