#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10000000000")
using namespace std;
const int INF = 1000000007;
const long long INF2 = 1000000000000000007;
const long long MOD = 1000000007;
const long long MOD1 = 1864463441;
const int SIZE = 200010;
const double eps = 1e-9;
const long double pi = acos(-1.0);
long long n, m, k, x, y, z, v, v1, sum, med, sum1, a, b, cnt, c, rs, ind, ind1,
    q, l, r, d, sz, mx, mi, bf, bf1;
long long ar[SIZE], ar1[SIZE];
bool fl, fl1, lose, used[SIZE];
string s, t;
bool cmp(int a, int b) { return a > b; }
vector<int> vec[210];
long long kekw[10];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> ar[i];
  for (int i = 0; i < m; ++i) cin >> ar1[i];
  for (int kek = 0; kek < 512; ++kek) {
    fl1 = 1;
    for (int i = 0; i < n; ++i) {
      fl = 0;
      for (int j = 0; j < m; ++j) {
        if ((kek | (ar[i] & ar1[j])) == kek) {
          fl = 1;
          break;
        }
      }
      fl1 &= fl;
    }
    if (fl1) {
      cout << kek;
      return 0;
    }
  }
  return 0;
}
