#include <bits/stdc++.h>
using namespace std;
const int MAXN = 513;
const int MAXN2 = 200;
int a[MAXN2], b[MAXN2];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  int ans = 0;
  bool good, check;
  for (int i = 0; i < MAXN; i++) {
    good = true;
    for (int j = 0; j < n; j++) {
      check = false;
      for (int k = 0; k < m; k++) {
        int nums = (a[j] & b[k]) | i;
        if (nums == i) {
          check = true;
          break;
        }
      }
      if (!check) {
        good = false;
      }
    }
    if (good) {
      cout << i << endl;
      return 0;
    }
  }
}
