#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MAXN = 200 + 15;
int a[MAXN], b[MAXN], ab[MAXN][MAXN];
bool vis[MAXN][MAXN];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ab[i][j] = a[i] & b[j];
    }
  }
  int res = 0;
  for (int dig = 10; dig >= 0; dig--) {
    bool legal = true;
    for (int i = 1; i <= n; i++) {
      bool flag = false;
      for (int j = 1; j <= m; j++) {
        if (vis[i][j]) continue;
        if (!((ab[i][j] >> dig) & 1)) {
          flag = true;
          break;
        }
      }
      if (!flag) {
        legal = false;
        break;
      }
    }
    if (legal) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          if ((ab[i][j] >> dig) & 1) {
            vis[i][j] = true;
          }
        }
      }
    } else
      res |= (1 << dig);
  }
  printf("%d\n", res);
  return 0;
}
