#include <bits/stdc++.h>
using namespace std;
long long powmod(long long base, long long exp, long long MOD = 1000000007) {
  long long res = 1;
  while (exp > 0) {
    if (exp % 2 == 1) res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return (res % MOD);
}
long long mul_inv(long long a, long long b = 1000000007) {
  long long t1 = a, t2 = b, t3, v1 = 1, v2 = 0, v3;
  while (t2 != 1) {
    long long x = t1 / t2;
    t3 = t1 - x * t2;
    v3 = v1 - x * v2;
    t1 = t2, t2 = t3;
    v1 = v2, v2 = v3;
  }
  return (v2 + b) % b;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m;
  cin >> n >> m;
  long long a1[n], a2[m];
  for (long long i = 0; i < n; i++) {
    cin >> a1[i];
  }
  for (long long i = 0; i < m; i++) {
    cin >> a2[i];
  }
  bool fl = false;
  long long ans;
  for (long long A = 0; A < 512; A++) {
    fl = false;
    for (long long i = 0; i < n; i++) {
      fl = false;
      for (long long j = 0; j < m; j++) {
        if (((a2[j] & a1[i]) | A) == A) {
          fl = true;
          break;
        }
      }
      if (!fl) break;
    }
    if (fl) {
      ans = A;
      break;
    }
  }
  cout << ans << endl;
}
