#include <bits/stdc++.h>
using namespace std;
long long st(long long a, long long b) {
  long long ans = 1;
  for (long long i = 0; i < b; ++i) {
    ans *= a;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n, 0);
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<long long> b(m, 0);
  for (long long i = 0; i < m; ++i) {
    cin >> b[i];
  }
  vector<long long> ans(10, 0);
  vector<vector<vector<long long>>> g(
      n, vector<vector<long long>>(m, vector<long long>(11, 0)));
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < m; ++j) {
      long long a1 = a[i];
      long long b1 = b[j];
      for (long long k = 0; k < 10; ++k) {
        g[i][j][k] = (a1 % 2) * (b1 % 2);
        a1 /= 2;
        b1 /= 2;
      }
    }
  }
  for (long long k = 9; k >= 0; k -= 1) {
    long long q = 0;
    for (long long i = 0; i < n; ++i) {
      q = 1;
      for (long long j = 0; j < m; ++j) {
        if (g[i][j][k] == 0 && g[i][j][10] == 0) {
          q = 0;
          break;
        }
      }
      if (q == 1) {
        break;
      }
    }
    if (q == 1) {
      ans[k] = 1;
    } else {
      for (long long i = 0; i < n; ++i) {
        for (long long j = 0; j < m; ++j) {
          if (g[i][j][k] == 1) {
            g[i][j][10] = 1;
          }
        }
      }
    }
  }
  long long ans1 = 0;
  for (long long k = 0; k < 10; ++k) {
    ans1 += ans[k] * st(2, k);
  }
  cout << ans1;
  return 0;
}
