#include <bits/stdc++.h>
using namespace std;
short int min_b = 1024;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  vector<short int> a(n), b(m);
  min_b = 1024;
  for (long long int i = 0; i < n; i++) cin >> a[i];
  for (long long int i = 0; i < m; i++) {
    cin >> b[i];
    min_b = min(min_b, b[i]);
  }
  vector<vector<short int>> vec(n, vector<short int>(m, 0));
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      vec[i][j] = a[i] & b[j];
    }
  }
  for (long long int i = 0; i < min_b + 1; i++) {
    int cnt = 0;
    for (long long int j = 0; j < n; j++) {
      for (long long int k = 0; k < m; k++) {
        if ((vec[j][k] | i) == i) {
          cnt++;
          break;
        }
      }
    }
    if (cnt == n) {
      cout << i << endl;
      break;
    }
  }
}
