#include <bits/stdc++.h>
template <typename T>
void vec_in(std::vector<T> &v, long long int n) {
  if (v.size() == 0) {
    T input;
    for (long long int i = 0; i < n; i++) {
      std::cin >> input;
      v.push_back(input);
    }
  } else {
    for (long long int i = 0; i < n; i++) std::cin >> v[i];
  }
}
template <typename T>
void vec_out(std::vector<T> &v) {
  for (T x : v) {
    std::cout << x << " ";
  }
  std::cout << '\n';
}
template <typename T>
void vec_out(std::vector<T> &&v) {
  for (T x : v) {
    std::cout << x << " ";
  }
  std::cout << '\n';
}
void dfs(std::vector<std::vector<long long int> > &adj,
         std::vector<std::vector<bool> > &ans, std::vector<bool> &vis,
         long long int node) {
  vis[node] = true;
  long long int n = ans[0].size();
  for (long long int i : adj[node]) {
    for (long long int j = 0; j < n; j++) {
      if (ans[j][node]) ans[j][i] = true;
    }
    if (vis[i] == false) dfs(adj, ans, vis, i);
  }
  return;
}
void dfs_start(std::vector<std::vector<bool> > &ans,
               std::vector<std::vector<long long int> > adj) {
  long long int n = ans[0].size();
  for (long long int i = 0; i < n; i++) {
    std::vector<bool> vis(n, false);
    dfs(adj, ans, vis, i);
  }
}
void seg_init(std::vector<long long int> &v, long long int node,
              long long int l, long long int r, std::vector<long long int> &a) {
  if (l == r) {
    v[node] = a[l];
  } else {
    long long int mid = l + (r - l) / 2;
    seg_init(v, 2 * node, l, mid, a);
    seg_init(v, 2 * node + 1, mid + 1, r, a);
    v[node] = std::min(v[2 * node], v[2 * node + 1]);
  }
}
void update(std::vector<long long int> &v, long long int node, long long int l,
            long long int r, long long int index, long long int val) {
  if (l == r) {
    if (l == index) v[node] = val;
  } else {
    long long int mid = l + (r - l) / 2;
    if (index <= mid) update(v, 2 * node, l, mid, index, val);
    if (index > mid) update(v, 2 * node + 1, mid + 1, r, index, val);
    v[node] = std::min(v[2 * node], v[2 * node + 1]);
  }
}
long long int query(std::vector<long long int> &v, long long int node,
                    long long int l, long long int r, long long int fir,
                    long long int sec) {
  if (fir <= l && r <= sec) {
    return v[node];
  } else {
    if (sec < l || r < fir)
      return 10000000000000000;
    else {
      long long int mid = l + (r - l) / 2;
      long long int case1 = query(v, 2 * node, l, mid, fir, sec);
      long long int case2 = query(v, 2 * node + 1, mid + 1, r, fir, sec);
      return std::min(case1, case2);
    }
  }
}
void conca(std::vector<long long int> &a, std::vector<long long int> &b) {
  a.insert(a.end(), b.begin(), b.end());
}
void dfs(std::vector<std::vector<char> > &adj, long long int i, long long int j,
         long long int n, long long int m, std::vector<bool> &row,
         std::vector<bool> &col) {
  if (i == n - 1 && j == m - 1) return;
  if (adj[i][j] == 'R') {
    if (j == m - 1) {
      row[i] = true;
      adj[i][j] = 'D';
      return;
    } else {
      return dfs(adj, i, j + 1, n, m, row, col);
    }
  } else {
    if (i == n - 1) {
      col[j] = true;
      adj[i][j] = 'R';
      return;
    } else {
      return dfs(adj, i + 1, j, n, m, row, col);
    }
  }
}
bool survey(long long int i, long long int j,
            std::vector<std::vector<char> > &a) {
  long long int sum = 0;
  for (long long int m = i; m < i + 2; m++) {
    for (long long int n = j; n < j + 2; n++) {
      if (a[i][j] == '1') sum++;
    }
  }
  return sum;
}
int main() {
  long long int test_case = 1;
  while (test_case--) {
    long long int n, m;
    std::cin >> n;
    std::cin >> m;
    std::vector<long long int> a(n, 0);
    std::vector<long long int> b(m, 0);
    vec_in<long long int>(a, n);
    vec_in<long long int>(b, m);
    long long int max = 513;
    bool dp[n][max];
    for (long long int i = 0; i < n; i++)
      for (long long int j = 0; j < max; j++) dp[i][j] = false;
    for (long long int j = 0; j < m; j++) {
      long long int check = a[0] & b[j];
      dp[0][check] = true;
    }
    long long int ans = 10000000000000000;
    for (long long int i = 1; i < n; i++) {
      for (long long int j = 0; j < m; j++) {
        long long int check = a[i] & b[j];
        for (long long int k = 0; k < max; k++) {
          if (dp[i - 1][k]) {
            dp[i][k | check] = true;
          }
        }
      }
    }
    for (long long int k = 0; k < max; k++) {
      if (dp[n - 1][k]) ans = std::min(ans, k);
    }
    std::cout << ans << std::endl;
  }
}
