#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
long long int dx[] = {-1, 0, 0, 1};
long long int dy[] = {0, -1, 1, 0};
long long int a[1000005];
long long int b[1000005];
bool dp[205][515];
long long int ctr = 0;
long long binpow(long long a, long long b) {
  long long int m = 1000000007;
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long int brute(vector<long long int> &ans) {
  long long int ctr = 0;
  long long int maxm = -1;
  for (auto i : ans) {
    if (i > maxm) {
      ctr++;
      maxm = i;
    }
  }
  return ctr;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  t = 1;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (long long int i = 0; i < m; i++) {
      cin >> b[i];
    }
    for (long long int i = 0; i < m; i++) {
      dp[0][a[0] & b[i]] = true;
    }
    for (long long int i = 1; i < n; i++) {
      for (long long int j = 0; j < m; j++) {
        long long int q = a[i] & b[j];
        for (long long int k = 0; k < (1 << 9); k++) {
          if (dp[i - 1][k]) {
            dp[i][k | q] = true;
          }
        }
      }
    }
    long long int maxm = -1;
    for (long long int i = (1 << 9); i >= 0; i--) {
      if (dp[n - 1][i]) maxm = i;
    }
    cout << maxm << "\n";
  }
}
