#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const double pi = acos(-1);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;
int a[210];
int b[210];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = (1); i <= (n); i++) cin >> a[i];
  for (int i = (1); i <= (m); i++) cin >> b[i];
  int ans = 1 << 9;
  ans--;
  for (int i = (8); i >= (0); i--) {
    bool f = true;
    ans ^= 1 << i;
    for (int j = (1); j <= (n); j++) {
      bool g = false;
      ;
      for (int k = (1); k <= (m); k++) {
        if ((a[j] & b[k] | ans) == ans) {
          g = true;
          break;
        }
      }
      if (!g) {
        f = false;
        break;
      }
    }
    if (!f) ans ^= 1 << i;
  }
  cout << ans;
}
