#include <bits/stdc++.h>
using namespace std;
long long isKthBitSet(long long n, long long k) { return n & (1 << (k - 1)); }
int main() {
  long long int n, m;
  cin >> n >> m;
  ;
  long long a[n];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  ;
  long long b[m];
  for (long long int i = 0; i < m; i++) cin >> b[i];
  ;
  set<long long> st[n];
  bool visited[n][m];
  memset(visited, true, sizeof(visited));
  long long arr[n][m];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      arr[i][j] = a[i] & b[j];
    }
  }
  vector<long long> ans;
  for (long long i = 11; i >= 1; i--) {
    bool flag = true;
    long long d = 0;
    for (long long int j = 0; j < n; j++) {
      long long c = 0, p = 0;
      for (long long int k = 0; k < m; k++) {
        if (visited[j][k]) {
          p++;
          if (isKthBitSet(arr[j][k], i)) {
            c++;
          }
        }
      }
      if (c == p) {
        d++;
      }
    }
    if (d > 0) {
      ans.push_back(i);
    } else {
      for (long long int j = 0; j < n; j++) {
        for (long long int k = 0; k < m; k++) {
          if (visited[j][k]) {
            if (isKthBitSet(arr[j][k], i)) {
              visited[j][k] = false;
            }
          }
        }
      }
    }
  }
  long long val = 0;
  for (long long int i = 0; i < ans.size(); i++) val += pow(2, ans[i] - 1);
  cout << val;
  return 0;
}
