#include <bits/stdc++.h>
double pi = 3.141592653589793238;
using namespace std;
namespace number_theory {
bool prime[15000105];
void sieve(long long n) {
  for (long long i = 0; i <= n; i++) prime[i] = 1;
  for (long long p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  prime[1] = prime[0] = 0;
}
vector<long long> primelist;
bool _primes_generated_ = 0;
void genprimes(long long n) {
  _primes_generated_ = 1;
  sieve(n + 1);
  for (long long i = 2; i <= n; i++)
    if (prime[i]) primelist.push_back(i);
}
vector<long long> factors(long long n) {
  if (!_primes_generated_) {
    cerr << "Call genprimes you dope" << '\n';
    exit(1);
  }
  vector<long long> facs;
  for (long long i = 0;
       primelist[i] * primelist[i] <= n && i < primelist.size(); i++) {
    if (n % primelist[i] == 0) {
      while (n % primelist[i] == 0) {
        n /= primelist[i];
        facs.push_back(primelist[i]);
      }
    }
  }
  if (n > 1) {
    facs.push_back(n);
  }
  return facs;
}
}  // namespace number_theory
using namespace number_theory;
long long __gcd(long long a, long long b) {
  if (b == 0) return a;
  if (b > a) return __gcd(b, a);
  return __gcd(b, a % b);
}
long long power(long long x, unsigned long long y) {
  long long res = 1;
  x = x % 1000000007;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = ((res * x) % 1000000007 + 1000000007) % 1000000007;
    y = y >> 1;
    x = ((x * x) % 1000000007 + 1000000007) % 1000000007;
  }
  return res % 1000000007;
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
bool checkPrime(long long n) {
  long long count = 0;
  for (long long i = 1; (i * i) <= n; i++) {
    if ((i * i) == n) {
      count++;
      continue;
    }
    if (n % i == 0) count += 2;
  }
  if (count == 2) return true;
  return false;
}
long long lcm(long long a, long long b) { return a * b / (__gcd(a, b)); }
using namespace std;
vector<long long> prime1;
long long N = 1e7 + 1;
long long fib[200005];
set<long long> s;
void primeSieve() {
  bool *arr = new bool[N];
  for (long long i = 0; i < N; i++) arr[i] = true;
  arr[0] = false;
  arr[1] = false;
  for (long long i = 1; i * i <= N; i++) {
    if (arr[i] == false)
      continue;
    else {
      for (long long j = i * i; j < N; j += i) {
        arr[j] = false;
      }
    }
  }
  for (long long i = 0; i < N; i++) {
    if (arr[i] == true) {
      prime1.push_back(i);
      s.insert(i);
    }
  }
}
void fibonacci() {
  fib[0] = 1;
  fib[1] = 1;
  fib[2] = 1;
  for (long long i = 3; i < 200005; i++) {
    fib[i] = (fib[i - 1] + fib[i - 2]) % 1000000007;
  }
}
long long fac[10000001];
void factorial() {
  fac[0] = 1;
  for (unsigned long long i = 1; i < 10000001; i++) {
    fac[i] = (((((fac[i - 1]) + 1000000007) % 1000000007) * i) + 1000000007) %
             1000000007;
  }
}
long long C(long long a, long long b) {
  if (a == 0) return 0;
  if (b == 0 || b == a) return 1;
  long long div = ((fac[b] * fac[a - b])) % 1000000007;
  div = power(div, 1000000007 - 2);
  return (fac[a] * div) % 1000000007;
}
long long ceil(long long a, long long b) { return (a + b - 1) / b; }
unsigned long long abds(unsigned long long x) {
  if (x > 0) return x;
  return -x;
}
vector<long long> edges[100005];
bool visited[100005];
long long fun(long long curr, bool red) {
  long long ans = 1;
  if (visited[curr]) return 1;
  visited[curr] = true;
  for (long long i = 0; i < edges[curr].size(); i++) {
    ans *= fun(edges[curr][i], !red);
  }
  if (red) ans *= 2;
  return ans;
}
bool cmp(pair<pair<long long, long long>, long long> a,
         pair<pair<long long, long long>, long long> b) {
  if (a.first.first == b.first.first) {
    return a.first.second > b.first.second;
  }
  return a.first.first < b.first.first;
}
void solve(long long test) {
  long long n, m;
  cin >> n >> m;
  long long a[n];
  long long b[m];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < m; i++) {
    cin >> b[i];
  }
  for (long long i = 0; i < 512; i++) {
    bool mainFlag = true;
    for (long long i1 = 0; i1 < n; i1++) {
      bool flag = false;
      for (long long i2 = 0; i2 < m; i2++) {
        if (((a[i1] & b[i2]) | i) == i) {
          flag = true;
          break;
        }
      }
      if (!flag) {
        mainFlag = false;
        break;
      }
    }
    if (mainFlag) {
      cout << i << "\n";
      return;
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  solve(0);
  ;
  return 0;
}
