#include <bits/stdc++.h>
using namespace std;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const long long int Inf = 1LL << 62;
int ar[202];
int br[202];
int Set(int num, int pos) { return num | (1LL << pos); }
int Clear(int num, int pos) { return num & ~(1LL << pos); }
int Toggle(int num, int pos) { return num ^ (1LL << pos); }
bool Check(int num, int pos) { return (bool)(num & (1LL << pos)); }
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> br[i];
  }
  for (int val = 0; val < (1 << 9); val++) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      bool okay = 0;
      for (int j = 1; j <= m; j++) {
        int now = ar[i] & br[j];
        bool f = 1;
        for (int pos = 0; pos < 9; pos++) {
          if (Check(now, pos) && !Check(val, pos)) {
            f = 0;
            break;
          }
        }
        okay |= f;
        if (okay) {
          break;
        }
      }
      if (okay) {
        cnt++;
      }
    }
    if (cnt == n) {
      cout << val << endl;
      return 0;
    }
  }
  return 0;
}
