#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const int inf = 2e9;
const int N = 205;
int n, m, a[N], b[N], ans;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  for (int num = 0; num < (1 << 9); num++) {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      bool found = false;
      for (int j = 0; j < m; j++) {
        int cur = a[i] & b[j];
        if ((cur & num) == cur) {
          found = true;
          break;
        }
      }
      if (!found) {
        ok = false;
        break;
      }
    }
    if (ok) {
      cout << num;
      break;
    }
  }
}
