#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 7;
int n, m;
int a[222], b[222];
set<int> possible[222];
void solve() {
  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) possible[i].insert(j);
  int ans = 0;
  for (int j = 8; j >= 0; --j) {
    int no = 0;
    for (int i = 1; i <= n; ++i) {
      if (a[i] & (1 << j)) {
        bool valid = false;
        for (int id : possible[i])
          if (!(b[id] & (1 << j))) valid = true;
        if (!valid) no = 1;
      }
    }
    if (no) {
      ans += (1 << j);
      continue;
    }
    for (int i = 1; i <= n; ++i) {
      if (a[i] & (1 << j)) {
        vector<int> not_valid;
        for (int id : possible[i])
          if ((b[id] & (1 << j))) not_valid.emplace_back(id);
        for (int id : not_valid) possible[i].erase(id);
      }
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
  return 0;
}
