#include <bits/stdc++.h>
using namespace std;
signed main() {
  std ::ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  long long a[n], b[m];
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < m; i++) cin >> b[i];
  for (long long i = 0; i <= (2e9); i++) {
    bool res = true;
    for (long long j = 0; j < n; j++) {
      res = false;
      for (long long k = 0; k < m; k++) {
        if (((a[j] & b[k]) | i) == i) {
          res = true;
          break;
        }
      }
      if (!res) break;
    }
    if (res) {
      cout << i << '\n';
      break;
    }
  }
}
