#include <bits/stdc++.h>
using namespace std;
const int fx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
const int fxx[9][2] = {{0, 0}, {0, 1},  {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
void subsequences(long long ar[], long long n);
long long apow(long long a, long long b);
long long bpow(long long a, long long b, long long mod);
long long gcd(long long a, long long b);
bool dio(long long a, long long b, long long c);
bool palindrome(string s);
void sieve();
long long binarySearch(vector<long long> vec, long long n, long long x);
long long kadane(vector<long long> vec, long long n);
vector<long long> factors(long long n);
long long countBits(long long n);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long vec[n];
  long long res[m];
  for (long long i = 0; i < n; i++) {
    cin >> vec[i];
  }
  for (long long j = 0; j < m; j++) {
    cin >> res[j];
  }
  long long ab[n][m];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      ab[i][j] = vec[i] & res[j];
    }
  }
  for (long long i = 0; i < 512; i++) {
    bool ok = true;
    for (long long j = 0; j < n; j++) {
      bool flag = true;
      for (long long k = 0; k < m; k++) {
        if ((ab[j][k] | i) == i) {
          flag = false;
        }
      }
      if (flag) {
        ok = false;
      }
    }
    if (ok) {
      cout << i;
      break;
    }
  }
  return 0;
}
