#include <bits/stdc++.h>
using namespace std;
long long t, n, m, a;
using namespace std;
int dp(vector<vector<int>>& g, int n, int m, int ind, int o,
       vector<vector<int>>& dp1) {
  if (ind == 0) {
    int minn = INT_MAX;
    int ans;
    for (int i = 0; i < m; i++) {
      if ((g[0][i] | o) < minn) {
        minn = g[0][i] | o;
        ans = g[0][i];
      }
    }
    return minn;
  }
  if (dp1[ind][o] != -1) return dp1[ind][o];
  int minn = INT_MAX;
  int ans;
  for (int i = 0; i < m; i++) {
    int re = dp(g, n, m, ind - 1, (o | g[ind][i]), dp1);
    if ((re | o | g[ind][i]) < minn) {
      minn = re | g[ind][i] | o;
      ans = re | g[ind][i];
    }
  }
  dp1[ind][o] = ans;
  return ans;
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<int> v1;
  vector<int> v2;
  vector<vector<int>> g(n, vector<int>(m, 0));
  vector<vector<int>> dp1(n, vector<int>(514, -1));
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v1.push_back(a);
  }
  for (int i = 0; i < m; i++) {
    int b;
    cin >> b;
    v2.push_back(b);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      g[i][j] = v1[i] & v2[j];
    }
  }
  cout << dp(g, n, m, n - 1, 0, dp1);
}
