#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const long long NINF = -INF;
const long long mod = 1e9 + 7;
void solve() {
  int n, m;
  cin >> n >> m;
  int a[n + 1], b[m + 1];
  int i, j;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= m; i++) cin >> b[i];
  int k;
  for (i = 0; i < (1 << 9); i++) {
    bool found = false;
    for (j = 1; j <= n; j++) {
      found = false;
      for (k = 1; k <= m; k++) {
        if ((i | (a[j] & b[k])) == i) {
          found = true;
          break;
        }
      }
      if (!found) {
        break;
      }
    }
    if (found) {
      cout << i << endl;
      return;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
