#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int ar[n];
  int br[m];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> br[i];
  }
  int mat[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      mat[i][j] = 1;
    }
  }
  int ans = 0;
  for (int bit = 9; bit >= 0; bit--) {
    int newmat[n][m];
    memcpy(newmat, mat, sizeof(mat));
    int poss = 1;
    for (int i = 0; i < n; i++) {
      int flag = 0;
      for (int j = 0; j < m; j++) {
        int x = (1 & (ar[i] >> bit)) & (1 & (br[j] >> bit));
        if (x == 0 && mat[i][j] == 1) {
          flag = 1;
        } else {
          newmat[i][j] = 0;
        }
      }
      if (flag == 0) {
        poss = 0;
        break;
      }
    }
    if (poss == 1) {
      memcpy(mat, newmat, sizeof(mat));
    } else {
      ans = ans | (1 << bit);
    }
  }
  cout << ans << endl;
  return 0;
}
