/*

    Author      : Yash Sharma
    Institution : IIT Patna
    Country     : India

*/

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;
using namespace std;

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

typedef long long ll;
typedef long double ld;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl "\n"
//#define mpq priority_queue<int,vector<int>,greater<int>>
#define debug(a) cerr << #a << ": " << a << endl
#define umii unordered_map<int64_t,int64_t>
#define ftt int t;cin>>t;for(int tt=1;tt<=t;++tt)
#define forr(i,n) for((i)=1;(i)<=(n);(i)++)
#define Rev(v) reverse(v.begin(),v.end());
#define Sort(v) sort(v.begin(),v.end());
#define Sum(v) accumulate(v.begin(),v.end(),0)
#define Input(v) for(auto &x:v)cin>>x;
#define mem(a,b) memset(a,b,sizeof(a))
#define deb(x) cout<<"\nhi"<<x<<"\n"
#define pq priority_queue<int>
#define dbg cout<<"\nhi\n"
#define sayy cout<<"Yes\n"
#define sayn cout<<"No\n"
//#define int long long
#define double long double
#define pii pair<int,int>
#define mii map<int,int>
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define snd second
#define fst first

const int INF = numeric_limits<int>::max();
const int MOD = 1000000007;
const double PI = 3.1415926535898;
const int LIM = 100005;

int fpow( int x,int y) {
    int temp;
    if(y == 0)
        return 1;
    temp = fpow(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;}

int gcd(int a,int b) {
    if(b==0)return a;
    return gcd(b,a%b);}

void sieve(int n) {
     bool prime[5*LIM];
     memset(prime, true, sizeof(prime));
     for (int p=2; p*p<=n; p++)
          if (prime[p] == true)
               for (int i=p*p; i<=n; i += p)
                    prime[i] = false;
     prime[1]=0;}

bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

map<int,int> primeFactors(int n)
{
    map<int, int> mp;
    while (n % 2 == 0)
    {
        if(mp.count(2) == 1)
            mp[2]++;
        else
            mp.insert({2,1});
        n = n/2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            if(mp.count(i) == 1)
                mp[i]++;
            else
                mp.insert({i,1});
            n = n/i;
        }
    }
    if (n > 2)
    {
        if(mp.count(n) == 1)
            mp[n]++;
        else
            mp.insert({n,1});
    }
    return mp;
}

//*************************************************************************************************************************

int32_t main()
{    fio;

    int n, m, cnt, cnt1, ele;
    cin>>n>>m;
    vi a(n), b(m), c(n, INF);
    Input(a);
    Input(b);
    for(int i=0;i<(1<<9);i++)
    {
        cnt1 = 0;
        for(int j=0;j<n;j++)
        {
            cnt = 0;
            for(int k=0;k<m;k++)
            {
                ele = a[j] & b[k];
                if((ele | i) == i)
                {
                    cnt++;
                    break;
                }
            }
            if(cnt == 0)
                break;
            else
                cnt1++;
        }
        if(cnt1 == n)
        {
            cout<<i;
            return 0;
        }
    }
    return 0;
}


/*

    Author      : Yash Sharma
    Institution : IIT Patna
    Country     : India

*/

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;
using namespace std;

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

typedef long long ll;
typedef long double ld;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl "\n"
//#define mpq priority_queue<int,vector<int>,greater<int>>
#define debug(a) cerr << #a << ": " << a << endl
#define umii unordered_map<int64_t,int64_t>
#define ftt int t;cin>>t;for(int tt=1;tt<=t;++tt)
#define forr(i,n) for((i)=1;(i)<=(n);(i)++)
#define Rev(v) reverse(v.begin(),v.end());
#define Sort(v) sort(v.begin(),v.end());
#define Sum(v) accumulate(v.begin(),v.end(),0)
#define Input(v) for(auto &x:v)cin>>x;
#define mem(a,b) memset(a,b,sizeof(a))
#define deb(x) cout<<"\nhi"<<x<<"\n"
#define pq priority_queue<int>
#define dbg cout<<"\nhi\n"
#define sayy cout<<"Yes\n"
#define sayn cout<<"No\n"
#define int long long
#define double long double
#define pii pair<int,int>
#define mii map<int,int>
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define snd second
#define fst first

const int INF = numeric_limits<int>::max();
const int MOD = 1000000007;
const double PI = 3.1415926535898;
const int LIM = 100005;

int fpow( int x,int y) {
    int temp;
    if(y == 0)
        return 1;
    temp = fpow(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;}

int gcd(int a,int b) {
    if(b==0)return a;
    return gcd(b,a%b);}

void sieve(int n) {
     bool prime[5*LIM];
     memset(prime, true, sizeof(prime));
     for (int p=2; p*p<=n; p++)
          if (prime[p] == true)
               for (int i=p*p; i<=n; i += p)
                    prime[i] = false;
     prime[1]=0;}

bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

map<int,int> primeFactors(int n)
{
    map<int, int> mp;
    while (n % 2 == 0)
    {
        if(mp.count(2) == 1)
            mp[2]++;
        else
            mp.insert({2,1});
        n = n/2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            if(mp.count(i) == 1)
                mp[i]++;
            else
                mp.insert({i,1});
            n = n/i;
        }
    }
    if (n > 2)
    {
        if(mp.count(n) == 1)
            mp[n]++;
        else
            mp.insert({n,1});
    }
    return mp;
}

//*************************************************************************************************************************

int32_t main()
{    fio;

    int n, m, ans, maxi = -1, ret, ele, finans = INF;
    cin>>n>>m;
    vi a(n), b(m), c(n, INF);
    Input(a);
    Input(b);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            c[i] = min(c[i], a[i] & b[j]);
        }
    }
    for(int i=0;i<n;i++)
    {
        maxi = max(maxi, c[i]);
    }
    ans = maxi;
    for(int x : c)
    {
        ans = x;
    for(int i=0;i<n;i++)
    {
        ret = INF;
        for(int j=0;j<m;j++)
        {
            ele = a[i] & b[j];
            //if(ele <= maxi)
            {
                ret = min(ret, ele | ans);
            }
        }
        ans = ret;
        //cout<<ans<<" ";
    }
    finans = min(finans, ans);
    }
    cout<<finans;
    return 0;
}


/*

    Author      : Yash Sharma
    Institution : IIT Patna
    Country     : India

*/

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;
using namespace std;

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

typedef long long ll;
typedef long double ld;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl "\n"
//#define mpq priority_queue<int,vector<int>,greater<int>>
#define debug(a) cerr << #a << ": " << a << endl
#define umii unordered_map<int64_t,int64_t>
#define ftt int t;cin>>t;for(int tt=1;tt<=t;++tt)
#define forr(i,n) for((i)=1;(i)<=(n);(i)++)
#define Rev(v) reverse(v.begin(),v.end());
#define Sort(v) sort(v.begin(),v.end());
#define Sum(v) accumulate(v.begin(),v.end(),0)
#define Input(v) for(auto &x:v)cin>>x;
#define mem(a,b) memset(a,b,sizeof(a))
#define deb(x) cout<<"\nhi"<<x<<"\n"
#define pq priority_queue<int>
#define dbg cout<<"\nhi\n"
#define sayy cout<<"Yes\n"
#define sayn cout<<"No\n"
#define int long long
#define double long double
#define pii pair<int,int>
#define mii map<int,int>
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define snd second
#define fst first

const int INF = numeric_limits<int>::max();
const int MOD = 1000000007;
const double PI = 3.1415926535898;
const int LIM = 100005;

int fpow( int x,int y) {
    int temp;
    if(y == 0)
        return 1;
    temp = fpow(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;}

int gcd(int a,int b) {
    if(b==0)return a;
    return gcd(b,a%b);}

void sieve(int n) {
     bool prime[5*LIM];
     memset(prime, true, sizeof(prime));
     for (int p=2; p*p<=n; p++)
          if (prime[p] == true)
               for (int i=p*p; i<=n; i += p)
                    prime[i] = false;
     prime[1]=0;}

bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

map<int,int> primeFactors(int n)
{
    map<int, int> mp;
    while (n % 2 == 0)
    {
        if(mp.count(2) == 1)
            mp[2]++;
        else
            mp.insert({2,1});
        n = n/2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            if(mp.count(i) == 1)
                mp[i]++;
            else
                mp.insert({i,1});
            n = n/i;
        }
    }
    if (n > 2)
    {
        if(mp.count(n) == 1)
            mp[n]++;
        else
            mp.insert({n,1});
    }
    return mp;
}

//*************************************************************************************************************************

int32_t main()
{    fio;

    int n, m, ans, maxi = -1, ret, ele;
    cin>>n>>m;
    vi a(n), b(m), c(n, INF);
    Input(a);
    Input(b);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            c[i] = min(c[i], a[i] & b[j]);
        }
    }
    for(int i=0;i<n;i++)
    {
        maxi = max(maxi, c[i]);
    }
    ans = maxi;
    //cout<<ans<<" ";
    for(int i=0;i<n;i++)
    {
        ret = INF;
        for(int j=0;j<m;j++)
        {
            ele = a[i] & b[j];
            //if(ele <= maxi)
            {
                ret = min(ret, ele | ans);
            }
        }
        ans = ret;
        //cout<<ans<<" ";
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<stack>
#define PI 3.1415926535898
#define Mid ((l+r)>>1)
#define lson rt<<1,l,Mid
#define rson rt<<1|1,Mid+1,r
#define fo(i,x,y) for(auto i=(x);i<=(y);++i)
#define dfo(i,x,y) for(auto i=(x);i>=(y);--i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll > pa;
const int inf = 0x3f3f3f3f;
const ll mo = 998244353;
ll sx, sy, ex, ey, dx[6] = { 0,1,0,-1,0,0 }, dy[6] = { 1,0,-1,0,0,0 }, m, n, k, dz[6]{ 0,0,0,0,-1,1 },sg;
ll  p, no, v, ans, w;
int par[55];
ll  a[400005], b[400005], c[500006], d[400006], ma[500006], mi[500006], ne[200005];

ll bit[200005];
ll gcd(ll a, ll b)
{
	return a ? gcd(b%a, a) : b;
}
set<ll> se,se1;
struct node {
	ll  to,cost;
}a2[200005];
vector<node> eg;
ll qu(ll a, ll b, ll m)
{
	ll ans = 1;
	while (b)
	{
		if (b & 1)
		{
			ans = ans % m * a%m;
		}
		b >>= 1;
		a = a % m * a%m;
	}
	return ans;
}
int su(int n)
{
	if (n == 1 || n == 0)
		return 0;
	for (int i = 2; i <= sqrt(n); i++)
	{
		if (n%i == 0)
			return 0;

	}

	return 1;
}
ll query(int i)
{
	ll ans = 0;
	while (i > 0)
	{
		ans += bit[i];
		i -= i & -i;
	}
	return ans;
}
void add(int i, ll x)
{
	while (i <= n)
	{
		bit[i] += x;
		i += i & -i;
	}
}
int fi(int a)
{
	if (a == par[a])
		return a;
	else return par[a] = fi(par[a]);
}
map<string, ll> mp, mp1;
map<pa, int> mp2;
// int nu = 1, id = 1;
queue<int> que;
pa a1[400000];
int vis[200005], vis1[400055]; ll dp[200005][15];
//int seg[400005], add[400005];
char maze[605][605];
vector<pa> g[2000000], g1[400000];
vector <ll > ve, ve1, ve2;
string s, ss;

ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
string ans1;

inline ll read()
{
	ll x = 0; bool t = false; char ch = getchar();
	while ((ch<'0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-')t = true, ch = getchar();
	while (ch <= '9'&&ch >= '0')x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}
void ge(string &a)
{
	int j = 0, k = -1;
	int n = a.size();
	ne[0] = -1;
	while (j < n)
	{

		if (k == -1 || a[j] == a[k])
			ne[++j] = ++k;
		else k = ne[k];
	}
}
int ct = 0, ct1 = 0;
string divv(string a, int b)//高精度a除以单精度b
{
	string r, ans;
	int d = 0;
	if (a == "0") return a;//特判
	for (int i = 0; i < a.size(); i++)
	{
		r += (d * 10 + a[i] - '0') / b + '0';//求出商
		d = (d * 10 + (a[i] - '0')) % b;//求出余数
	}
	int p = 0;
	for (int i = 0; i < r.size(); i++)
		if (r[i] != '0') { p = i; break; }
	return r.substr(p);
}
int cmp(int a, int b)
{
	return a > b;
}
int cp(string s, string ss)
{
	if (s.length() > ss.length())
		return 1;
	else if (s.length() < ss.length())
		return 0;
	else return s >= ss;
}
int che(int x)
{
	for (int i = 0; i < n; i++)
	{
		sg = 0;
		for (int j = 0; j < m; j++)
		{
			no = a[i] & b[j];
			if ((no | x) == x)
				sg = 1;
		}
		if (!sg)
			return 0;
	}
	return 1;
}
int main()
{
	int t, p2, p3;
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	ll l, r, n1, u, l1, r1;
	int tot = 1;
	char ch, ch1;
	no = 1;

	while (cin >>n>>m)
	{

		no = 0;
		ans = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
			ans |= a[i];
		}
		for (int j = 0; j < m; j++)
		{
			cin >> b[j];

		}
		for (int i = 0; i <= ans; i++)
		{
			if (che(i))
			{
				cout << i << endl;
				break;
			}

	    }

	}

}


#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<stack>
#define PI 3.1415926535898
#define Mid ((l+r)>>1)
#define lson rt<<1,l,Mid
#define rson rt<<1|1,Mid+1,r
#define fo(i,x,y) for(auto i=(x);i<=(y);++i)
#define dfo(i,x,y) for(auto i=(x);i>=(y);--i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll > pa;
const int inf = 0x3f3f3f3f;
const ll mo = 998244353;
ll sx, sy, ex, ey, dx[6] = { 0,1,0,-1,0,0 }, dy[6] = { 1,0,-1,0,0,0 }, m, n, k, dz[6]{ 0,0,0,0,-1,1 },sg;
ll  p, no, v, ans, w;
int par[55];
ll  a[400005], b[400005], c[500006], d[400006], ma[500006], mi[500006], ne[200005];

ll bit[200005];
ll gcd(ll a, ll b)
{
	return a ? gcd(b%a, a) : b;
}
set<ll> se,se1;
struct node {
	ll  to,cost;
}a2[200005];
vector<node> eg;
ll qu(ll a, ll b, ll m)
{
	ll ans = 1;
	while (b)
	{
		if (b & 1)
		{
			ans = ans % m * a%m;
		}
		b >>= 1;
		a = a % m * a%m;
	}
	return ans;
}
int su(int n)
{
	if (n == 1 || n == 0)
		return 0;
	for (int i = 2; i <= sqrt(n); i++)
	{
		if (n%i == 0)
			return 0;

	}

	return 1;
}
ll query(int i)
{
	ll ans = 0;
	while (i > 0)
	{
		ans += bit[i];
		i -= i & -i;
	}
	return ans;
}
void add(int i, ll x)
{
	while (i <= n)
	{
		bit[i] += x;
		i += i & -i;
	}
}
int fi(int a)
{
	if (a == par[a])
		return a;
	else return par[a] = fi(par[a]);
}
map<string, ll> mp, mp1;
map<pa, int> mp2;
// int nu = 1, id = 1;
queue<int> que;
pa a1[400000];
int vis[200005], vis1[400055]; ll dp[200005][15];
//int seg[400005], add[400005];
char maze[605][605];
vector<pa> g[2000000], g1[400000];
vector <ll > ve, ve1, ve2;
string s, ss;

ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
string ans1;

inline ll read()
{
	ll x = 0; bool t = false; char ch = getchar();
	while ((ch<'0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-')t = true, ch = getchar();
	while (ch <= '9'&&ch >= '0')x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}
void ge(string &a)
{
	int j = 0, k = -1;
	int n = a.size();
	ne[0] = -1;
	while (j < n)
	{

		if (k == -1 || a[j] == a[k])
			ne[++j] = ++k;
		else k = ne[k];
	}
}
int ct = 0, ct1 = 0;
string divv(string a, int b)//高精度a除以单精度b
{
	string r, ans;
	int d = 0;
	if (a == "0") return a;//特判
	for (int i = 0; i < a.size(); i++)
	{
		r += (d * 10 + a[i] - '0') / b + '0';//求出商
		d = (d * 10 + (a[i] - '0')) % b;//求出余数
	}
	int p = 0;
	for (int i = 0; i < r.size(); i++)
		if (r[i] != '0') { p = i; break; }
	return r.substr(p);
}
int cmp(int a, int b)
{
	return a > b;
}
int cp(string s, string ss)
{
	if (s.length() > ss.length())
		return 1;
	else if (s.length() < ss.length())
		return 0;
	else return s >= ss;
}
int main()
{
	int t, p2, p3;
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	ll l, r, n1, u, l1, r1;
	int tot = 1;
	char ch, ch1;
	no = 1;

	while (cin >>n>>m)
	{

		no = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for (int j = 0; j < m; j++)
		{
			cin >> b[j];

		}
		ans = 0;
		for (int i = 0; i < n; i++)
		{
			no = 1e9+5;
			for (int j = 0; j < m; j++)
				no = min(no, a[i] & b[j]);
			ans = max(ans, no);
		}
		sort(a, a + n);
		for (int i = 0; i < n; i++)
		{
			ll no1 = 1e9+5;
			for (int j = 0; j < m; j++)
			{
				no = a[i] & b[j];
				no1 = min(no1, no | ans);
			}
			ans |= no1;
		}
		cout << ans << endl;

	}

}


#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<stack>
#define PI 3.1415926535898
#define Mid ((l+r)>>1)
#define lson rt<<1,l,Mid
#define rson rt<<1|1,Mid+1,r
#define fo(i,x,y) for(auto i=(x);i<=(y);++i)
#define dfo(i,x,y) for(auto i=(x);i>=(y);--i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll > pa;
const int inf = 0x3f3f3f3f;
const ll mo = 998244353;
ll sx, sy, ex, ey, dx[6] = { 0,1,0,-1,0,0 }, dy[6] = { 1,0,-1,0,0,0 }, m, n, k, dz[6]{ 0,0,0,0,-1,1 },sg;
ll  p, no, v, ans, w;
int par[55];
ll  a[400005], b[400005], c[500006], d[400006], ma[500006], mi[500006], ne[200005];

ll bit[200005];
ll gcd(ll a, ll b)
{
	return a ? gcd(b%a, a) : b;
}
set<ll> se,se1;
struct node {
	ll  to,cost;
}a2[200005];
vector<node> eg;
ll qu(ll a, ll b, ll m)
{
	ll ans = 1;
	while (b)
	{
		if (b & 1)
		{
			ans = ans % m * a%m;
		}
		b >>= 1;
		a = a % m * a%m;
	}
	return ans;
}
int su(int n)
{
	if (n == 1 || n == 0)
		return 0;
	for (int i = 2; i <= sqrt(n); i++)
	{
		if (n%i == 0)
			return 0;

	}

	return 1;
}
ll query(int i)
{
	ll ans = 0;
	while (i > 0)
	{
		ans += bit[i];
		i -= i & -i;
	}
	return ans;
}
void add(int i, ll x)
{
	while (i <= n)
	{
		bit[i] += x;
		i += i & -i;
	}
}
int fi(int a)
{
	if (a == par[a])
		return a;
	else return par[a] = fi(par[a]);
}
map<string, ll> mp, mp1;
map<pa, int> mp2;
// int nu = 1, id = 1;
queue<int> que;
pa a1[400000];
int vis[200005], vis1[400055]; ll dp[200005][15];
//int seg[400005], add[400005];
char maze[605][605];
vector<pa> g[2000000], g1[400000];
vector <ll > ve, ve1, ve2;
string s, ss;

ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
string ans1;

inline ll read()
{
	ll x = 0; bool t = false; char ch = getchar();
	while ((ch<'0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-')t = true, ch = getchar();
	while (ch <= '9'&&ch >= '0')x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}
void ge(string &a)
{
	int j = 0, k = -1;
	int n = a.size();
	ne[0] = -1;
	while (j < n)
	{

		if (k == -1 || a[j] == a[k])
			ne[++j] = ++k;
		else k = ne[k];
	}
}
int ct = 0, ct1 = 0;
string divv(string a, int b)//高精度a除以单精度b
{
	string r, ans;
	int d = 0;
	if (a == "0") return a;//特判
	for (int i = 0; i < a.size(); i++)
	{
		r += (d * 10 + a[i] - '0') / b + '0';//求出商
		d = (d * 10 + (a[i] - '0')) % b;//求出余数
	}
	int p = 0;
	for (int i = 0; i < r.size(); i++)
		if (r[i] != '0') { p = i; break; }
	return r.substr(p);
}
int cmp(int a, int b)
{
	return a > b;
}
int cp(string s, string ss)
{
	if (s.length() > ss.length())
		return 1;
	else if (s.length() < ss.length())
		return 0;
	else return s >= ss;
}
int main()
{
	int t, p2, p3;
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	ll l, r, n1, u, l1, r1;
	int tot = 1;
	char ch, ch1;
	no = 1;

	while (cin >>n>>m)
	{

		no = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for (int j = 0; j < m; j++)
		{
			cin >> b[j];

		}
		ans = 0;
		for (int i = 0; i < n; i++)
		{
			no = 1e9+5;
			for (int j = 0; j < m; j++)
				no = min(no, a[i] & b[j]);
			ans = max(ans, no);
		}
		for (int i = 0; i < n; i++)
		{
			ll no1 = 1e9+5;
			for (int j = 0; j < m; j++)
			{
				no = a[i] & b[j];
				no1 = min(no1, no | ans);
			}
			ans |= no1;
		}
		cout << ans << endl;

	}

}


#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<stack>
#define PI 3.1415926535898
#define Mid ((l+r)>>1)
#define lson rt<<1,l,Mid
#define rson rt<<1|1,Mid+1,r
#define fo(i,x,y) for(auto i=(x);i<=(y);++i)
#define dfo(i,x,y) for(auto i=(x);i>=(y);--i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll > pa;
const int inf = 0x3f3f3f3f;
const ll mo = 998244353;
ll sx, sy, ex, ey, dx[6] = { 0,1,0,-1,0,0 }, dy[6] = { 1,0,-1,0,0,0 }, m, n, k, dz[6]{ 0,0,0,0,-1,1 },sg;
ll  p, no, v, ans, w;
int par[55];
ll  a[400005], b[400005], c[500006], d[400006], ma[500006], mi[500006], ne[200005];

ll bit[200005];
ll gcd(ll a, ll b)
{
	return a ? gcd(b%a, a) : b;
}
set<ll> se,se1;
struct node {
	ll  to,cost;
}a2[200005];
vector<node> eg;
ll qu(ll a, ll b, ll m)
{
	ll ans = 1;
	while (b)
	{
		if (b & 1)
		{
			ans = ans % m * a%m;
		}
		b >>= 1;
		a = a % m * a%m;
	}
	return ans;
}
int su(int n)
{
	if (n == 1 || n == 0)
		return 0;
	for (int i = 2; i <= sqrt(n); i++)
	{
		if (n%i == 0)
			return 0;

	}

	return 1;
}
ll query(int i)
{
	ll ans = 0;
	while (i > 0)
	{
		ans += bit[i];
		i -= i & -i;
	}
	return ans;
}
void add(int i, ll x)
{
	while (i <= n)
	{
		bit[i] += x;
		i += i & -i;
	}
}
int fi(int a)
{
	if (a == par[a])
		return a;
	else return par[a] = fi(par[a]);
}
map<string, ll> mp, mp1;
map<pa, int> mp2;
// int nu = 1, id = 1;
queue<int> que;
pa a1[400000];
int vis[200005], vis1[400055]; ll dp[200005][15];
//int seg[400005], add[400005];
char maze[605][605];
vector<pa> g[2000000], g1[400000];
vector <ll > ve, ve1, ve2;
string s, ss;

ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
string ans1;

inline ll read()
{
	ll x = 0; bool t = false; char ch = getchar();
	while ((ch<'0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-')t = true, ch = getchar();
	while (ch <= '9'&&ch >= '0')x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}
void ge(string &a)
{
	int j = 0, k = -1;
	int n = a.size();
	ne[0] = -1;
	while (j < n)
	{

		if (k == -1 || a[j] == a[k])
			ne[++j] = ++k;
		else k = ne[k];
	}
}
int ct = 0, ct1 = 0;
string divv(string a, int b)//高精度a除以单精度b
{
	string r, ans;
	int d = 0;
	if (a == "0") return a;//特判
	for (int i = 0; i < a.size(); i++)
	{
		r += (d * 10 + a[i] - '0') / b + '0';//求出商
		d = (d * 10 + (a[i] - '0')) % b;//求出余数
	}
	int p = 0;
	for (int i = 0; i < r.size(); i++)
		if (r[i] != '0') { p = i; break; }
	return r.substr(p);
}
int cmp(int a, int b)
{
	return a > b;
}
int cp(string s, string ss)
{
	if (s.length() > ss.length())
		return 1;
	else if (s.length() < ss.length())
		return 0;
	else return s >= ss;
}
int main()
{
	int t, p2, p3;
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	ll l, r, n1, u, l1, r1;
	int tot = 1;
	char ch, ch1;
	no = 1;

	while (cin >>n>>m)
	{

		no = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for (int j = 0; j < m; j++)
		{
			cin >> b[j];

		}
		ans = 0;
		for (int i = 0; i < n; i++)
		{
			no = 1e9;
			for (int j = 0; j < m; j++)
				no = min(no, a[i] & b[j]);
			ans = max(ans, no);
		}
		for (int i = 0; i < n; i++)
		{
			ll no1 = 1e9;
			for (int j = 0; j < m; j++)
			{
				no = a[i] & b[j];
				no1 = min(no1, no | ans);
			}
			ans |= no1;
		}
		cout << ans << endl;

	}

}


#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<stack>
#define PI 3.1415926535898
#define Mid ((l+r)>>1)
#define lson rt<<1,l,Mid
#define rson rt<<1|1,Mid+1,r
#define fo(i,x,y) for(auto i=(x);i<=(y);++i)
#define dfo(i,x,y) for(auto i=(x);i>=(y);--i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll > pa;
const int inf = 0x3f3f3f3f;
const ll mo = 998244353;
ll sx, sy, ex, ey, dx[6] = { 0,1,0,-1,0,0 }, dy[6] = { 1,0,-1,0,0,0 }, m, n, k, dz[6]{ 0,0,0,0,-1,1 },sg;
ll  p, no, v, ans, w;
int par[55];
ll  a[400005], b[400005], c[500006], d[400006], ma[500006], mi[500006], ne[200005];

ll bit[200005];
ll gcd(ll a, ll b)
{
	return a ? gcd(b%a, a) : b;
}
set<ll> se,se1;
struct node {
	ll  to,cost;
}a2[200005];
vector<node> eg;
ll qu(ll a, ll b, ll m)
{
	ll ans = 1;
	while (b)
	{
		if (b & 1)
		{
			ans = ans % m * a%m;
		}
		b >>= 1;
		a = a % m * a%m;
	}
	return ans;
}
int su(int n)
{
	if (n == 1 || n == 0)
		return 0;
	for (int i = 2; i <= sqrt(n); i++)
	{
		if (n%i == 0)
			return 0;

	}

	return 1;
}
ll query(int i)
{
	ll ans = 0;
	while (i > 0)
	{
		ans += bit[i];
		i -= i & -i;
	}
	return ans;
}
void add(int i, ll x)
{
	while (i <= n)
	{
		bit[i] += x;
		i += i & -i;
	}
}
int fi(int a)
{
	if (a == par[a])
		return a;
	else return par[a] = fi(par[a]);
}
map<string, ll> mp, mp1;
map<pa, int> mp2;
// int nu = 1, id = 1;
queue<int> que;
pa a1[400000];
int vis[200005], vis1[400055]; ll dp[200005][15];
//int seg[400005], add[400005];
char maze[605][605];
vector<pa> g[2000000], g1[400000];
vector <ll > ve, ve1, ve2;
string s, ss;

ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
string ans1;

inline ll read()
{
	ll x = 0; bool t = false; char ch = getchar();
	while ((ch<'0' || ch>'9') && ch != '-')ch = getchar();
	if (ch == '-')t = true, ch = getchar();
	while (ch <= '9'&&ch >= '0')x = x * 10 + ch - 48, ch = getchar();
	return t ? -x : x;
}
void ge(string &a)
{
	int j = 0, k = -1;
	int n = a.size();
	ne[0] = -1;
	while (j < n)
	{

		if (k == -1 || a[j] == a[k])
			ne[++j] = ++k;
		else k = ne[k];
	}
}
int ct = 0, ct1 = 0;
string divv(string a, int b)//高精度a除以单精度b
{
	string r, ans;
	int d = 0;
	if (a == "0") return a;//特判
	for (int i = 0; i < a.size(); i++)
	{
		r += (d * 10 + a[i] - '0') / b + '0';//求出商
		d = (d * 10 + (a[i] - '0')) % b;//求出余数
	}
	int p = 0;
	for (int i = 0; i < r.size(); i++)
		if (r[i] != '0') { p = i; break; }
	return r.substr(p);
}
int cmp(int a, int b)
{
	return a > b;
}
int cp(string s, string ss)
{
	if (s.length() > ss.length())
		return 1;
	else if (s.length() < ss.length())
		return 0;
	else return s >= ss;
}
int main()
{
	int t, p2, p3;
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	ll l, r, n1, u, l1, r1;
	int tot = 1;
	char ch, ch1;
	no = 1;

	while (cin >>n>>m)
	{

		no = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for (int j = 0; j < m; j++)
		{
			cin >> b[j];

		}
		ans = 0;
		for (int i = 0; i < n; i++)
		{
			no = 1e9+5;
			for (int j = 0; j < m; j++)
				no = min(no, a[i] & b[j]);
			ans = max(ans, no);
		}
		sort(a, a + n,cmp);
		for (int i = 0; i < n; i++)
		{
			ll no1 = 1e9+5;
			for (int j = 0; j < m; j++)
			{
				no = a[i] & b[j];
				no1 = min(no1, no | ans);
			}
			ans |= no1;
		}
		cout << ans << endl;

	}

}


#include<bits/stdc++.h>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define unmap unordered_map
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define cj "Case #" << tc << ": "
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define SIZE(xx) (int)(xx.size())
#define all(xx) xx.begin(),xx.end()
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 20005

using namespace std;

const int inf = 1e9;
const db eps = 1e-9;
const int mod = 1e9 + 7;
const int nax = 205;
int powr(int x,int p) {if(p==0)return 1;int xx=powr(x,p/2);xx=(1LL*xx*xx)%mod;if(p&1)xx=(1LL*xx*x)%mod;return xx;}

int n, m;
int a[nax], b[nax];
int lim = (1 << 9);

void solve(int tc) {
	cin >> n >> m;

	for(int i = 0; i < n; ++i) {
		cin >> a[i];
	}

	for(int i = 0; i < m; ++i) {
		cin >> b[i];
	}

	vi temp;

	for(int i = 0; i < m; ++i) {
		temp.eb(a[0] & b[i]);
	}

	for(int i = 1; i < n; ++i) {
		vi tp;
		for(int j = 0; j < m; ++j) {
			int c = (a[i] & b[j]);

			for(int mask : temp) {
				tp.eb(mask | c);
			}
		}
		temp = tp;
	}
	int ans = inf;
	for(int s: temp) {
		ans = min(ans, s);
	}
	cout << ans << el;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	// cout << fixed << setprecision(10);
	int tt = 1;
	// sc(tt);
	for(int i = 1; i <= tt; ++i) {
		solve(i);
	}

	return 0;
}


#include<bits/stdc++.h>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define unmap unordered_map
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define cj "Case #" << tc << ": "
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define SIZE(xx) (int)(xx.size())
#define all(xx) xx.begin(),xx.end()
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 20005

using namespace std;

const int inf = 1e9;
const db eps = 1e-9;
const int mod = 1e9 + 7;
const int nax = 205;
int powr(int x,int p) {if(p==0)return 1;int xx=powr(x,p/2);xx=(1LL*xx*xx)%mod;if(p&1)xx=(1LL*xx*x)%mod;return xx;}

int n, m;
int a[nax], b[nax];
int lim = (1 << 9);

void solve(int tc) {
	cin >> n >> m;

	for(int i = 0; i < n; ++i) {
		cin >> a[i];
	}

	for(int i = 0; i < m; ++i) {
		cin >> b[i];
	}

	vector<vec<bool> > dp(n, vec<bool> (lim, false));

	for(int i = 0; i < m; ++i) {
		int mask = (a[0] & b[i]);
		dp[0][mask] = true;
	}

	for(int i = 1; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			int c = (a[i] & b[j]);

			for(int mask = 0; mask < lim; ++mask) {
				if(dp[i - 1][mask] == false) continue;
				dp[i][mask | c] = true;
			}
		}

	}
	int ans = inf;
	for(int mask = 0; mask < lim; ++mask) {
		if(dp[n - 1][mask]) {
			ans = mask;
			break;
		}
	}
	cout << ans << el;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout << fixed << setprecision(10);
	int tt = 1;
	// sc(tt);
	for(int i = 1; i <= tt; ++i) {
		solve(i);
	}

	return 0;
}


#include<bits/stdc++.h>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define unmap unordered_map
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define cj "Case #" << tc << ": "
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define SIZE(xx) (int)(xx.size())
#define all(xx) xx.begin(),xx.end()
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 20005

using namespace std;

const int inf = 1e9;
const db eps = 1e-9;
const int mod = 1e9 + 7;
const int nax = 205;
int powr(int x,int p) {if(p==0)return 1;int xx=powr(x,p/2);xx=(1LL*xx*xx)%mod;if(p&1)xx=(1LL*xx*x)%mod;return xx;}

int n, m;
int a[nax], b[nax];
int lim = (1 << 9);

void solve(int tc) {
	cin >> n >> m;

	for(int i = 0; i < n; ++i) {
		cin >> a[i];
	}

	for(int i = 0; i < m; ++i) {
		cin >> b[i];
	}

	vi temp;

	for(int i = 0; i < m; ++i) {
		temp.eb(a[0] & b[i]);
	}

	for(int i = 1; i < n; ++i) {
		unmap<int, int> mm;
		for(int j = 0; j < m; ++j) {
			int c = (a[i] & b[j]);

			for(int mask : temp) {
				mm[(mask | c)] = 1;
			}
		}
		temp.clear();
		for(int i = 0; i < lim; ++i) {
			if(mm.find(i) != mm.end()) temp.eb(i);
		}
	}
	int ans = inf;
	for(int s: temp) {
		ans = min(ans, s);
	}
	cout << ans << el;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	// cout << fixed << setprecision(10);
	int tt = 1;
	// sc(tt);
	for(int i = 1; i <= tt; ++i) {
		solve(i);
	}

	return 0;
}


#include<bits/stdc++.h>

#define deb(x) cout << #x << " -> " << x << el
#define db double
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define unmap unordered_map
#define sc(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define sclu(x) scanf("%llu", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define pass cout<<"pass\n"
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define vec vector
#define el "\n"
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define cj "Case #" << tc << ": "
#define f(i,x,y) for(int i=x;i<y;++i)
#define ff(i,x,y) for(int i=x;i<=y;++i)
#define mem(x) memset(x, 0, sizeof(x))
#define mems(x,y) memset(x, y, sizeof(x))
#define SIZE(xx) (int)(xx.size())
#define all(xx) xx.begin(),xx.end()
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 20005

using namespace std;

const int inf = 1e9;
const db eps = 1e-9;
const int mod = 1e9 + 7;
const int nax = 205;
int powr(int x,int p) {if(p==0)return 1;int xx=powr(x,p/2);xx=(1LL*xx*xx)%mod;if(p&1)xx=(1LL*xx*x)%mod;return xx;}

int n, m;
int a[nax], b[nax];
int lim = (1 << 9);

void solve(int tc) {
	cin >> n >> m;

	for(int i = 0; i < n; ++i) {
		cin >> a[i];
	}

	for(int i = 0; i < m; ++i) {
		cin >> b[i];
	}

	vi temp;

	for(int i = 0; i < m; ++i) {
		temp.eb(a[0] & b[i]);
	}

	for(int i = 1; i < n; ++i) {
		vi tp;
		for(int j = 0; j < m; ++j) {
			int c = (a[i] & b[j]);

			for(int mask : temp) {
				tp.eb(mask | c);
			}
		}
		temp.clear();
		for(int s: tp) {
			temp.eb(s);
		}
		tp.clear();
	}
	int ans = inf;
	for(int s: temp) {
		ans = min(ans, s);
	}
	cout << ans << el;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	// cout << fixed << setprecision(10);
	int tt = 1;
	// sc(tt);
	for(int i = 1; i <= tt; ++i) {
		solve(i);
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define in insert
#define F first
#define S second
#define mod 1000000007
#define endl '\n'
#define all(x)  x.begin(),x.end()
#define w(x) int x;cin>>x; while(x-->0)
#define memset(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << "[ " << #x  << " : " << x << " ]" << endl;

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
/*
think fast
think simple
*/
vector<int>arr, brr;
int n, m;
int dp[209][1 << 9];
int ans = 1e10;
int fun(int idx, int yet)
{
    if (idx == n)
        return yet;
    if (dp[idx][yet] != -1)
        return dp[idx][yet];
    int an = 1e10;
    for (int i = 0; i < m; i++)
    {
        int a = arr[idx];
        int b = brr[i];
        int now = a & b;
        an = min(an, fun(idx + 1, (yet | now)));
    }
    return dp[idx][yet] = an;
}
void mysol()
{
    cin >> n >> m;
    arr.resize(n);
    brr.resize(m);
    memset(dp, -1);
    for (int &a : arr)
        cin >> a;
    for (int &b : brr)
        cin >> b;
    cout << fun(0, 0) << "\n";
}

int32_t main()
{
    fast();
    // w(t)
    {
        mysol();
    }
    //cerr << sizeof(dp)*(1e-6) << " megabytes\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define in insert
#define F first
#define S second
#define mod 1000000007
#define endl '\n'
#define all(x)  x.begin(),x.end()
#define w(x) int x;cin>>x; while(x-->0)
#define memset(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << "[ " << #x  << " : " << x << " ]" << endl;

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
/*
think fast
think simple
*/
vector<int>arr, brr;
int n, m;
vector<vector<int>>dp;
int fun(int idx, int last, int yet)
{
    if (idx == n)
        return yet;
    if (dp[idx][yet] != -1)
        return dp[idx][yet];
    int ans = 1e10;
    for (int i = 0; i < m; i++)
    {
        int a = arr[idx];
        int b = brr[i];
        int now = a & b;
        ans = min(ans, fun(idx + 1, i, (yet | now)) | now);
    }
    return dp[idx][yet] = ans;
}
void mysol()
{
    cin >> n >> m;
    arr.resize(n);
    brr.resize(m);
    dp.resize(n + 9);
    for (auto &i : dp)
        i.assign(1 << 10, -1);// runtime error?
    for (int &a : arr)
        cin >> a;
    for (int &b : brr)
        cin >> b;
    cout << fun(0, 0, 0);
}

int32_t main()
{
    fast();
    // w(t)
    {
        mysol();
    }
    //cerr << sizeof(dp)*(1e-6) << " megabytes\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define in insert
#define F first
#define S second
#define mod 1000000007
#define endl '\n'
#define all(x)  x.begin(),x.end()
#define w(x) int x;cin>>x; while(x-->0)
#define memset(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << "[ " << #x  << " : " << x << " ]" << endl;

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
/*
think fast
think simple
*/
vector<int>arr, brr;
int n, m;
vector<vector<int>>dp;
int fun(int idx, int last, int yet)
{
    if (idx == n)
        return yet;
    if (dp[idx][yet] != -1)
        return dp[idx][yet];
    int ans = 1e10;
    for (int i = 0; i < m; i++)
    {
        int a = arr[idx];
        int b = brr[i];
        int now = a & b;
        ans = min(ans, fun(idx + 1, i, (yet | now)) | now);
    }
    return dp[idx][yet] = ans;
}
void mysol()
{
    cin >> n >> m;
    arr.resize(n);
    brr.resize(m);
    dp.resize(n + 9);
    for (auto &i : dp)
        i.assign(1 << 12, -1);
    for (int &a : arr)
        cin >> a;
    for (int &b : brr)
        cin >> b;
    cout << fun(0, 0, 0);
}

int32_t main()
{
    fast();
    // w(t)
    {
        mysol();
    }
    //cerr << sizeof(dp)*(1e-6) << " megabytes\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define in insert
#define F first
#define S second
#define mod 1000000007
#define endl '\n'
#define all(x)  x.begin(),x.end()
#define w(x) int x;cin>>x; while(x-->0)
#define memset(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << "[ " << #x  << " : " << x << " ]" << endl;

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
/*
think fast
think simple
*/
vector<int>arr, brr;
int n, m;
vector<vector<int>>dp;
int fun(int idx, int last)
{
    if (idx == n)
        return 0;
    if (dp[idx][last] != -1)
        return dp[idx][last];
    int ans = 1e10;

    for (int i = 0; i < m; i++)
    {
        int a = arr[idx];
        int b = brr[i];
        int now = a & b;
        ans = min(ans, fun(idx + 1, i) | now);
    }
    return dp[idx][last] = ans;
}
void mysol()
{
    cin >> n >> m;
    arr.resize(n);
    brr.resize(m);
    dp.resize(n + 9);
    for (auto &i : dp)
        i.assign(m + 9, -1);
    for (int &a : arr)
        cin >> a;
    for (int &b : brr)
        cin >> b;
    cout << fun(0, 0);
}

int32_t main()
{
    fast();
    // w(t)
    {
        mysol();
    }
    //cerr << sizeof(dp)*(1e-6) << " megabytes\n";
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

# define vii vector<int>
# define vll vector<long long int>
# define vvi vector<vector<int>>
# define vvl vector<vector<long long int>>
# define pii pair<int,int>
# define pll pair<long long int, long long int>
# define sii set<int>
# define sll set<long long int>
# define usii unordered_set<int>
# define usll unordered_set<long long int>
# define mii map<int,int>
# define mll map<long long int,long long int>
# define umii unordered_map<int,int>
# define umll unordered_map<long long int,long long int>

# define forr(i,a,b) for(ll i=a;i<=b;i++)
# define fors(p,s) for(auto p=s.begin();p!=s.end();p++)
# define form(p,m) for(auto p=m.begin();p!=m.end();p++)

# define print(x) cout<<(x)<<"\n"
# define put(x) cout<<(x)<<' '<<"\n";
# define put2(x,y) cout<<(x)<<' '<<(y)<<' '<<"\n"
# define put3(x,y,z) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<"\n"
# define put4(x,y,z,w) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<(w)<<' '<<"\n";
# define El cout<<"\n"
# define F first
# define S second
# define B begin()
# define E end()
# define L size()
# define Pb push_back
# define Mp make_pair
# define Max(p1,p2) *max_element(p1,p2)
# define Min(p1,p2) *min_element(p1,p2)
# define Sum(p1,p2) accumulate(p1,p2,0LL)
# define Ins(s,x) (find(s.begin(),s.end(),(x))!=s.end()) // note that x and s should be of the same type
# define Inm(m,x) (m.find(x)!=m.end()) // note that m and x should be of the same type

# define printa(a,n) for(int idnphq=0;idnphq<n;idnphq++) cout << a[idnphq] << " ";cout << "\n";  // only for debugging (of arrays and vectors) thus int
# define prints(s) for(auto idnphq=s.begin();idnphq!=s.end();idnphq++) cout<< *idnphq << ' ';cout << "\n"; // only for debugging (of sets)
# define printm(m) for(auto idnphq=m.begin();idnphq!=m.end();idnphq++) cout<< idnphq->first << ':' << idnphq->second << ' ';cout << "\n"; //ofd

# define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define INF 1000000000
# define MOD 1000000007
# define MAXN 200005
# define ll long long int
# define TEST int t; cin>>t; while(t--)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                    main code starts here

int main()
{
    int n,m;
    int a[n+33],b[m+33];
	cin>>n>>m;
	forr(i,1,n)	cin>>a[i];
	forr(i,1,m) cin>>b[i];
	forr(i,0,512)
	{
		bool ok=1;
		forr(j,1,n)
		{
			bool flag=0;
			forr(k,1,m)
				if(((a[j] & b[k]) & i)==(a[j] & b[k]))
					flag=1;
			if(!flag)
			{
				ok=0;
				break;
			}
		}
		if(ok)
		{
			cout<<i<<"\n";
			return 0;
		}
	}
	return 0;
}


# include <bits/stdc++.h>
using namespace std;

# define vii vector<int>
# define vll vector<long long int>
# define vvi vector<vector<int>>
# define vvl vector<vector<long long int>>
# define pii pair<int,int>
# define pll pair<long long int, long long int>
# define sii set<int>
# define sll set<long long int>
# define usii unordered_set<int>
# define usll unordered_set<long long int>
# define mii map<int,int>
# define mll map<long long int,long long int>
# define umii unordered_map<int,int>
# define umll unordered_map<long long int,long long int>

# define forr(i,a,b) for(ll i=a;i<=b;i++)
# define fors(p,s) for(auto p=s.begin();p!=s.end();p++)
# define form(p,m) for(auto p=m.begin();p!=m.end();p++)

# define print(x) cout<<(x)<<"\n"
# define put(x) cout<<(x)<<' '<<"\n";
# define put2(x,y) cout<<(x)<<' '<<(y)<<' '<<"\n"
# define put3(x,y,z) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<"\n"
# define put4(x,y,z,w) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<(w)<<' '<<"\n";
# define El cout<<"\n"
# define F first
# define S second
# define B begin()
# define E end()
# define L size()
# define Pb push_back
# define Mp make_pair
# define Max(p1,p2) *max_element(p1,p2)
# define Min(p1,p2) *min_element(p1,p2)
# define Sum(p1,p2) accumulate(p1,p2,0LL)
# define Ins(s,x) (find(s.begin(),s.end(),(x))!=s.end()) // note that x and s should be of the same type
# define Inm(m,x) (m.find(x)!=m.end()) // note that m and x should be of the same type

# define printa(a,n) for(int idnphq=0;idnphq<n;idnphq++) cout << a[idnphq] << " ";cout << "\n";  // only for debugging (of arrays and vectors) thus int
# define prints(s) for(auto idnphq=s.begin();idnphq!=s.end();idnphq++) cout<< *idnphq << ' ';cout << "\n"; // only for debugging (of sets)
# define printm(m) for(auto idnphq=m.begin();idnphq!=m.end();idnphq++) cout<< idnphq->first << ':' << idnphq->second << ' ';cout << "\n"; //ofd

# define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define INF 1000000000
# define MOD 1000000007
# define MAXN 200005
# define ll long long int
# define TEST int t; cin>>t; while(t--)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                              main code starts here

int main()
{
    int n,m;
    int a[n],b[m];
	cin>>n>>m;
	forr(i,1,n)	cin>>a[i];
	forr(i,1,m) cin>>b[i];
	forr(i,0,512)
	{
		bool ok=1;
		forr(j,1,n)
		{
			bool flag=0;
			forr(k,1,m)
				if(((a[j] & b[k]) & i)==(a[j] & b[k]))
					flag=1;
			if(!flag)
			{
				ok=0;
				break;
			}
		}
		if(ok)
		{
			cout<<i<<"\n";
			return 0;
		}
	}
	return 0;
}


# include <bits/stdc++.h>
using namespace std;

# define vii vector<int>
# define vll vector<long long int>
# define vvi vector<vector<int>>
# define vvl vector<vector<long long int>>
# define pii pair<int,int>
# define pll pair<long long int, long long int>
# define sii set<int>
# define sll set<long long int>
# define usii unordered_set<int>
# define usll unordered_set<long long int>
# define mii map<int,int>
# define mll map<long long int,long long int>
# define umii unordered_map<int,int>
# define umll unordered_map<long long int,long long int>

# define forr(i,a,b) for(ll i=a;i<=b;i++)
# define fors(p,s) for(auto p=s.begin();p!=s.end();p++)
# define form(p,m) for(auto p=m.begin();p!=m.end();p++)

# define print(x) cout<<(x)<<"\n"
# define put(x) cout<<(x)<<' '<<"\n";
# define put2(x,y) cout<<(x)<<' '<<(y)<<' '<<"\n"
# define put3(x,y,z) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<"\n"
# define put4(x,y,z,w) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<(w)<<' '<<"\n";
# define El cout<<"\n"
# define F first
# define S second
# define B begin()
# define E end()
# define L size()
# define Pb push_back
# define Mp make_pair
# define Max(p1,p2) *max_element(p1,p2)
# define Min(p1,p2) *min_element(p1,p2)
# define Sum(p1,p2) accumulate(p1,p2,0LL)
# define Ins(s,x) (find(s.begin(),s.end(),(x))!=s.end()) // note that x and s should be of the same type
# define Inm(m,x) (m.find(x)!=m.end()) // note that m and x should be of the same type

# define printa(a,n) for(int idnphq=0;idnphq<n;idnphq++) cout << a[idnphq] << " ";cout << "\n";  // only for debugging (of arrays and vectors) thus int
# define prints(s) for(auto idnphq=s.begin();idnphq!=s.end();idnphq++) cout<< *idnphq << ' ';cout << "\n"; // only for debugging (of sets)
# define printm(m) for(auto idnphq=m.begin();idnphq!=m.end();idnphq++) cout<< idnphq->first << ':' << idnphq->second << ' ';cout << "\n"; //ofd

# define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define INF 1000000000
# define MOD 1000000007
# define MAXN 200005
# define ll long long int
# define TEST int t; cin>>t; while(t--)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                            main code starts here

int main()
{
    int n,m;
    int a[n+1],b[m+1];
	cin>>n>>m;
	forr(i,1,n)	cin>>a[i];
	forr(i,1,m) cin>>b[i];
	forr(i,0,512)
	{
		bool ok=1;
		forr(j,1,n)
		{
			bool flag=0;
			forr(k,1,m)
				if(((a[j] & b[k]) & i)==(a[j] & b[k]))
					flag=1;
			if(!flag)
			{
				ok=0;
				break;
			}
		}
		if(ok)
		{
			cout<<i<<"\n";
			return 0;
		}
	}
	return 0;
}


# include <bits/stdc++.h>
using namespace std;

# define vii vector<int>
# define vll vector<long long int>
# define vvi vector<vector<int>>
# define vvl vector<vector<long long int>>
# define pii pair<int,int>
# define pll pair<long long int, long long int>
# define sii set<int>
# define sll set<long long int>
# define usii unordered_set<int>
# define usll unordered_set<long long int>
# define mii map<int,int>
# define mll map<long long int,long long int>
# define umii unordered_map<int,int>
# define umll unordered_map<long long int,long long int>

# define forr(i,a,b) for(ll i=a;i<=b;i++)
# define fors(p,s) for(auto p=s.begin();p!=s.end();p++)
# define form(p,m) for(auto p=m.begin();p!=m.end();p++)

# define print(x) cout<<(x)<<"\n"
# define put(x) cout<<(x)<<' '<<"\n";
# define put2(x,y) cout<<(x)<<' '<<(y)<<' '<<"\n"
# define put3(x,y,z) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<"\n"
# define put4(x,y,z,w) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<(w)<<' '<<"\n";
# define El cout<<"\n"
# define F first
# define S second
# define B begin()
# define E end()
# define L size()
# define Pb push_back
# define Mp make_pair
# define Max(p1,p2) *max_element(p1,p2)
# define Min(p1,p2) *min_element(p1,p2)
# define Sum(p1,p2) accumulate(p1,p2,0LL)
# define Ins(s,x) (find(s.begin(),s.end(),(x))!=s.end()) // note that x and s should be of the same type
# define Inm(m,x) (m.find(x)!=m.end()) // note that m and x should be of the same type

# define printa(a,n) for(int idnphq=0;idnphq<n;idnphq++) cout << a[idnphq] << " ";cout << "\n";  // only for debugging (of arrays and vectors) thus int
# define prints(s) for(auto idnphq=s.begin();idnphq!=s.end();idnphq++) cout<< *idnphq << ' ';cout << "\n"; // only for debugging (of sets)
# define printm(m) for(auto idnphq=m.begin();idnphq!=m.end();idnphq++) cout<< idnphq->first << ':' << idnphq->second << ' ';cout << "\n"; //ofd

# define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define INF 1000000000
# define MOD 1000000007
# define MAXN 200005
# define ll long long int
# define TEST int t; cin>>t; while(t--)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                    main code starts here

int main()
{
    int n,m;
    int a[201],b[201];
	cin>>n>>m;
	forr(i,1,n)	cin>>a[i];
	forr(i,1,m) cin>>b[i];
	forr(i,0,512)
	{
		bool ok=1;
		forr(j,1,n)
		{
			bool flag=0;
			forr(k,1,m)
				if(((a[j] & b[k]) & i)==(a[j] & b[k]))
					flag=1;
			if(!flag)
			{
				ok=0;
				break;
			}
		}
		if(ok)
		{
			cout<<i<<"\n";
			return 0;
		}
	}
	return 0;
}


# include <bits/stdc++.h>
using namespace std;

# define vii vector<int>
# define vll vector<long long int>
# define vvi vector<vector<int>>
# define vvl vector<vector<long long int>>
# define pii pair<int,int>
# define pll pair<long long int, long long int>
# define sii set<int>
# define sll set<long long int>
# define usii unordered_set<int>
# define usll unordered_set<long long int>
# define mii map<int,int>
# define mll map<long long int,long long int>
# define umii unordered_map<int,int>
# define umll unordered_map<long long int,long long int>

# define forr(i,a,b) for(ll i=a;i<=b;i++)
# define fors(p,s) for(auto p=s.begin();p!=s.end();p++)
# define form(p,m) for(auto p=m.begin();p!=m.end();p++)

# define print(x) cout<<(x)<<"\n"
# define put(x) cout<<(x)<<' '<<"\n";
# define put2(x,y) cout<<(x)<<' '<<(y)<<' '<<"\n"
# define put3(x,y,z) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<"\n"
# define put4(x,y,z,w) cout<<(x)<<' '<<(y)<<' '<<(z)<<' '<<(w)<<' '<<"\n";
# define El cout<<"\n"
# define F first
# define S second
# define B begin()
# define E end()
# define L size()
# define Pb push_back
# define Mp make_pair
# define Max(p1,p2) *max_element(p1,p2)
# define Min(p1,p2) *min_element(p1,p2)
# define Sum(p1,p2) accumulate(p1,p2,0LL)
# define Ins(s,x) (find(s.begin(),s.end(),(x))!=s.end()) // note that x and s should be of the same type
# define Inm(m,x) (m.find(x)!=m.end()) // note that m and x should be of the same type

# define printa(a,n) for(int idnphq=0;idnphq<n;idnphq++) cout << a[idnphq] << " ";cout << "\n";  // only for debugging (of arrays and vectors) thus int
# define prints(s) for(auto idnphq=s.begin();idnphq!=s.end();idnphq++) cout<< *idnphq << ' ';cout << "\n"; // only for debugging (of sets)
# define printm(m) for(auto idnphq=m.begin();idnphq!=m.end();idnphq++) cout<< idnphq->first << ':' << idnphq->second << ' ';cout << "\n"; //ofd

# define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define INF 1000000000
# define MOD 1000000007
# define MAXN 200005
# define ll long long int
# define TEST int t; cin>>t; while(t--)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                               main code starts here

int main()
{
    int n,m;
    int a[233],b[233];
	cin>>n>>m;
	forr(i,1,n)	cin>>a[i];
	forr(i,1,m) cin>>b[i];
	forr(i,0,512)
	{
		bool ok=1;
		forr(j,1,n)
		{
			bool flag=0;
			forr(k,1,m)
				if(((a[j] & b[k]) & i)==(a[j] & b[k]))
					flag=1;
			if(!flag)
			{
				ok=0;
				break;
			}
		}
		if(ok)
		{
			cout<<i<<"\n";
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define flog(n) 31-__builtin_clz((n))
#define clog(n) 32-__builtin_clz((n)-1)
#define fori(i,a,b) for(long long int i=(a);i<=(b);i++)
#define forib(i,a,b) for(long long int i=(a);i>=(b);i--)
using ll=long long; using pi=pair<int,int>; using vi=vector<int>; using vl=vector<ll>;

void solve(){
  ll n, m;cin >> n >> m;
  vl a(n), b(m);
  fori(i, 0, n - 1){
    cin >> a[i];
  }
  fori(i, 0, m - 1){
    cin >> b[i];
  }
  vl c(n);
  fori(i, 0, n - 1){
    ll mini = 1000000000;
    fori(j, 0, m - 1){
      if (mini > (a[i] & b[j])){
        mini = (a[i] & b[j]);
      }
    }
    c[i] = mini;
  }
  ll mo = c[0];
  fori(i, 1, n - 1){
    mo = (mo | c[i]);
  }
  cout << mo;
}
int main() {
  if(true){ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
  solve();
}


#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define flog(n) 31-__builtin_clz((n))
#define clog(n) 32-__builtin_clz((n)-1)
#define fori(i,a,b) for(long long int i=(a);i<=(b);i++)
#define forib(i,a,b) for(long long int i=(a);i>=(b);i--)
using ll=long long; using pi=pair<int,int>; using vi=vector<int>; using vl=vector<ll>;

void solve(){
  ll n, m;cin >> n >> m;
  vl a(n), b(m);
  fori(i, 0, n - 1){
    cin >> a[i];
  }
  fori(i, 0, m - 1){
    cin >> b[i];
  }
  vector<vector<ll> > dp(n, vector<ll>(m));
  fori(i, 0, n - 1){
    fori(j, 0, m - 1){
      if (i == 0){
        if (j == 0){
          dp[i][j] = (a[0] & b[0]);
        } else {
          dp[i][j] = min(dp[i][j - 1], (a[i] & b[j]));
        }
      } else {
        ll mini = 1000000000;
        if (j == 0){
          dp[i][j] = dp[i - 1][j] | (a[i] & b[j]);
        } else {
          fori(x, 0, j){
            if (mini > dp[i - 1][j] | (a[i] & b[x])){
              mini = dp[i - 1][j] | (a[i] & b[x]);
            }
          }
          dp[i][j] = min(dp[i][j - 1], mini);
        }
      }
    }
  }
  cout << dp[n - 1][m - 1];
}
int main() {
  if(true){ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
  solve();
}


#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define flog(n) 31-__builtin_clz((n))
#define clog(n) 32-__builtin_clz((n)-1)
#define fori(i,a,b) for(long long int i=(a);i<=(b);i++)
#define forib(i,a,b) for(long long int i=(a);i>=(b);i--)
using ll=long long; using pi=pair<int,int>; using vi=vector<int>; using vl=vector<ll>;

void solve(){
  ll n, m; cin >> n >> m;
  vl a(n), b(m);
  fori(i, 0, n - 1){
    cin >> a[i];
  }
  fori(i, 0, m - 1){
    cin >> b[i];
  }
  fori(i, 0, pow(2, 9)){
    bool ok = true;
    fori(j, 0, n - 1){
      bool jo = true;
      fori(x, 0, m - 1){
        if (((a[j] & b[x]) | i) == i){
          jo = false;
          break;
        }
      }
      if (jo){
        ok = false;
        break;
      }
    }
    if (ok){
      cout << i << endl;
      break;
    }
  }
}
int main() {
  if(true){ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
  solve();
}


#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define flog(n) 31-__builtin_clz((n))
#define clog(n) 32-__builtin_clz((n)-1)
#define fori(i,a,b) for(long long int i=(a);i<=(b);i++)
#define forib(i,a,b) for(long long int i=(a);i>=(b);i--)
using ll=long long; using pi=pair<int,int>; using vi=vector<int>; using vl=vector<ll>;

void solve(){
  ll n, m;cin >> n >> m;
  vl a(n), b(m);
  fori(i, 0, n - 1){
    cin >> a[i];
  }
  fori(i, 0, m - 1){
    cin >> b[i];
  }
  vector<vector<ll> > dp(n, vector<ll>(m));
  fori(i, 0, n - 1){
    fori(j, 0, m - 1){
      if (i == 0){
        if (j == 0){
          dp[i][j] = (a[0] & b[0]);
        } else {
          dp[i][j] = min(dp[i][j - 1], (a[i] & b[j]));
        }
      } else {
        ll mini = 1000000000;
        if (j == 0){
          dp[i][j] = dp[i - 1][j] | (a[i] & b[j]);
        } else {
          fori(x, 0, j){
            if (mini > dp[i - 1][j] | (a[i] & b[x])){
              mini = dp[i - 1][j] | (a[i] & b[x]);
            }
          }
          dp[i][j] = min(dp[i][j - 1], mini);
        }
      }
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  cout << dp[n - 1][m - 1];
}
int main() {
  if(true){ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
  solve();
}


#include <iostream>

int A[200], B[200];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m, N = 1 << 9, i;
    std::cin >> n >> m;

    for(i = 0; i < n; i++) std::cin >> A[i];
    for(i = 0; i < m; i++) std::cin >> B[i];

    for(i = 0; i < N; i++) {
        for(int j = 0; j < n; j++) {
            bool chk = 0;
            for(int k = 0; k < m; k++)
                if(i | (A[j] & B[k]) == i) chk = 1;
            if(!chk) break;
        }
        if(chk) break;
    }

    std::cout << i << '\n';
}


#include <iostream>

int A[200], B[200];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m, N = 1 << 9, i;
    std::cin >> n >> m;

    for(i = 0; i < n; i++) std::cin >> A[i];
    for(i = 0; i < m; i++) std::cin >> B[i];

    bool chk;
    for(i = 0; i < N; i++) {
        for(int j = 0; j < n; j++) {
            chk = 0;
            for(int k = 0; k < m; k++)
                if(i | (A[j] & B[k]) == i) chk = 1;
            if(!chk) break;
        }
        if(chk) break;
    }

    std::cout << i << '\n';
}


#include <iostream>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m;
    std::cin >> n >> m;
    int a[n], b[m];

    for(int i = 0; i < n; i++) std::cin >> a[i];
    for(int i = 0; i < m; i++) std::cin >> b[i];

    int ans = (1 << 9) - 1;

    for(int i = 8; i >= 0; i--) {
        int temp = ans ^ 1 << i;
        bool chk;
        for(int j = 0; j < n; j++) {
            chk = false;
            for(int k = 0; k < m; k++) {
                if((a[j] & b[k] | temp) == temp) { chk = true; break; }
            }
            if(!chk) break;
        }
        if(chk) ans ^= 1 << i;
    }
    std::cout << ans;
}


#include <iostream>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m;
    std::cin >> n >> m;
    int a[n], b[m];

    for(int i = 0; i < n; i++) std::cin >> a[i];
    for(int i = 0; i < m; i++) std::cin >> b[i];

    int ans = (1 << 9) - 1;

    for(int i = 8; i >= 0; i--) {
        int temp = ans ^ 1 << i;
        bool chk;
        for(int j = 0; j < n; j++) {
            chk = false;
            for(int k = 0; k < m; k++) {
                if((a[j] & b[k] | temp) == temp) { chk = true; break;
            }
            if(!chk) break;
        }
        if(chk) ans ^= 1 << i;
    }
    std::cout << ans;
}


#include <iostream>

int A[200], B[200];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m, N = 1 << 9, i;
    std::cin >> n >> m;

    for(i = 0; i < n; i++) std::cin >> A[i];
    for(i = 0; i < m; i++) std::cin >> B[i];

    bool chk;
    for(i = 0; i < N; i++) {
        for(int j = 0; j < n; j++) {
            chk = 0;
            for(int k = 0; k < m; k++)
                if((i | (A[j] & B[k])) == i) chk = 1;
            if(!chk) break;
        }
        if(chk) break;
    }

    std::cout << i << '\n';
}


#include <iostream>

int A[200], B[200];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m, N = 1 << 9, i;
    std::cin >> n >> m;

    for(i = 0; i < n; i++) std::cin >> A[i];
    for(i = 0; i < m; i++) std::cin >> B[i];

    for(i = 0; i < N; i++) {
        bool chk = false;
        for(int j = 0; j < n; j++)
            for(int k = 0; k < m; k++)
                if(i | (A[j] & B[k]) == i) chk = true;
        if(chk) break;
    }

    std::cout << i << '\n';
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#define int long long
using namespace std;

int N, M;

vector<int> a;
vector<int> b;

vector<int> toTwo(int n)
{
    vector<int> ans;

    while (n != 0)
    {
        ans.push_back(n % 2);
        n /= 2;
    }

    while (ans.size() != 12)
        ans.push_back(0);

    reverse(ans.begin(), ans.end());

    return ans;
}

bool isE (vector<int> &f, vector<int> s)
{
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] != 1)
            continue;

        if (s[i] != f[i])
            return false;
    }

    return true;
}

int solve (vector<int> twoVector)
{
    vector<int> c;

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            int res = a[i] & b[j];

            if (isE(twoVector, toTwo(res)))
            {
                c.push_back(res);
                break;
            }
        }
    }

    if (c.size() != N)
        return 2e9;

    int k = c[0];

    for (int i = 1; i < c.size(); ++i)
        k = (k | c[i]);

    return k;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> M;

    a.resize(N);
    b.resize(M);

    for (int &e : a)
        cin >> e;

    for (int &e : b)
        cin >> e;

    int l = 2e9;

    for (int i = 0; i <= 1025; ++i)
    {
        l = solve(toTwo(i));

        if (l != 2e9)
            break;
    }

    cout << l;

}


#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#define int long long
using namespace std;

int N, M;

vector<vector<int>> twoArr;
vector<int> a;
vector<int> b;

vector<int> toTwo(int n)
{
    vector<int> ans;

    while (n != 0)
    {
        ans.push_back(n % 2);
        n /= 2;
    }

    while (ans.size() != 12)
        ans.push_back(0);

    reverse(ans.begin(), ans.end());

    return ans;
}

bool isE (vector<int> &f, vector<int> &s)
{
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] != 1)
            continue;

        if (s[i] != f[i])
            return false;
    }

    return true;
}

int solve (int number)
{
    vector<int> c;

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            int res = a[i] & b[j];

            if (isE(twoArr[number], twoArr[res]))
            {
                c.push_back(res);
                break;
            }
        }
    }

    if (c.size() != N)
        return 2e9;

    int k = c[0];

    for (int i = 1; i < c.size(); ++i)
        k = (k | c[i]);

    return k;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> M;

    int twosize = pow(2, 10) + 1;

    a.resize(N);
    b.resize(M);
    twoArr.resize(twosize);

    for (int i = 0; i < twosize; ++i)
        twoArr[i] = toTwo(i);

    for (int &e : a)
        cin >> e;

    for (int &e : b)
        cin >> e;

    int l = 2e9;

    for (int i = 0; i <= 1025; ++i)
    {
        l = solve(i);

        if (l != 2e9)
            break;
    }

    cout << l;

}


#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#define int long long
using namespace std;

int N, M;

vector<int> a;
vector<int> b;

vector<int> toTwo(int n)
{
    vector<int> ans;

    while (n != 0)
    {
        ans.push_back(n % 2);
        n /= 2;
    }

    while (ans.size() != 10)
        ans.push_back(0);

    reverse(ans.begin(), ans.end());

    return ans;
}

bool isE (vector<int> &f, vector<int> s)
{
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] != 1)
            continue;

        if (s[i] != f[i])
            return false;
    }

    return true;
}

int solve (vector<int> twoVector)
{
    vector<int> c;

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            int res = a[i] & b[j];

            if (isE(twoVector, toTwo(res)))
            {
                c.push_back(res);
                break;
            }
        }
    }

    if (c.size() != N)
        return 2e9;

    int k = c[0];

    for (int i = 1; i < c.size(); ++i)
        k = (k | c[i]);

    return k;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> M;

    a.resize(N);
    b.resize(M);

    for (int &e : a)
        cin >> e;

    for (int &e : b)
        cin >> e;

    int l = 2e9;

    for (int i = 0; i <= 520; ++i)
    {
        l = solve(toTwo(i));

        if (l != 2e9)
            break;
    }

    cout << l;

}


#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#define int long long
using namespace std;

int N, M;

vector<int> a;
vector<int> b;

vector<int> toTwo(int n)
{
    vector<int> ans;

    while (n != 0)
    {
        ans.push_back(n % 2);
        n /= 2;
    }

    while (ans.size() != 11)
        ans.push_back(0);

    reverse(ans.begin(), ans.end());

    return ans;
}

bool isE (vector<int> &f, vector<int> s)
{
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] != 1)
            continue;

        if (s[i] != f[i])
            return false;
    }

    return true;
}

int solve (vector<int> twoVector)
{
    vector<int> c;

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            int res = a[i] & b[j];

            if (isE(twoVector, toTwo(res)))
            {
                c.push_back(res);
                break;
            }
        }
    }

    if (c.size() != N)
        return 2e9;

    int k = c[0];

    for (int i = 1; i < c.size(); ++i)
        k = (k | c[i]);

    return k;
}

int32_t main()
{
    cin >> N >> M;

    a.resize(N);
    b.resize(M);

    for (int &e : a)
        cin >> e;

    for (int &e : b)
        cin >> e;

    int l = 2e9;
    for (int i = 0; i <= 512; ++i)
    {
        l = solve(toTwo(i));

        if (l != 2e9)
            break;
    }

    cout << l;

}


#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#define int long long
using namespace std;

int N, M;

vector<int> a;
vector<int> b;

vector<int> toTwo(int n)
{
    vector<int> ans;

    while (n != 0)
    {
        ans.push_back(n % 2);
        n /= 2;
    }

    while (ans.size() != 20)
        ans.push_back(0);

    reverse(ans.begin(), ans.end());

    return ans;
}

bool isE (vector<int> &f, vector<int> s)
{
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] != 1)
            continue;

        if (s[i] != f[i])
            return false;
    }

    return true;
}

int solve (vector<int> twoVector)
{
    vector<int> c;

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            int res = a[i] & b[j];

            if (isE(twoVector, toTwo(res)))
            {
                c.push_back(res);
                break;
            }
        }
    }

    if (c.size() != N)
        return 2e9;

    int k = c[0];

    for (int i = 1; i < c.size(); ++i)
        k = (k | c[i]);

    return k;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> M;

    a.resize(N);
    b.resize(M);

    for (int &e : a)
        cin >> e;

    for (int &e : b)
        cin >> e;

    int l = 2e9;

    for (int i = 0; i <= 520; ++i)
    {
        l = solve(toTwo(i));

        if (l != 2e9)
            break;
    }

    cout << l;

}


#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#define int long long
using namespace std;

int N, M;

vector<int> a;
vector<int> b;

vector<int> toTwo(int n)
{
    vector<int> ans;

    while (n != 0)
    {
        ans.push_back(n % 2);
        n /= 2;
    }

    while (ans.size() != 11)
        ans.push_back(0);

    reverse(ans.begin(), ans.end());

    return ans;
}

bool isE (vector<int> &f, vector<int> s)
{
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] != 1)
            continue;

        if (s[i] != f[i])
            return false;
    }

    return true;
}

int solve (vector<int> twoVector)
{
    vector<int> c;

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            int res = a[i] & b[j];

            if (isE(twoVector, toTwo(res)))
            {
                c.push_back(res);
                break;
            }
        }
    }

    if (c.size() != N)
        return 2e9;

    int k = c[0];

    for (int i = 1; i < c.size(); ++i)
        k = (k | c[i]);

    return k;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> M;

    a.resize(N);
    b.resize(M);

    for (int &e : a)
        cin >> e;

    for (int &e : b)
        cin >> e;

    int l = 2e9;
    for (int i = 0; i <= 512; ++i)
    {
        l = solve(toTwo(i));

        if (l != 2e9)
            break;
    }

    cout << l;

}


#include<bits/stdc++.h>
#define ll long long
#define maxn (ll)1e3+5
using namespace std;

ll n,m;
ll a[201],b[201];
struct node{
    int c[201];
}C[201];
ll Min=1e10;
void dfs(ll ans,ll depth){
    ll cnt=ans;
    //cout<<ans<<' '<<depth<<endl;
    for(ll i=1;i<=m;i++){
        if(C[depth].c[i]>Min)
            break;
        cnt=ans|C[depth].c[i];
        if(depth==n&&cnt<Min){
            Min=cnt;
            continue;
        }
        if(depth<n)
            dfs(cnt,depth+1);
    }
}
int main(){
    cin>>n>>m;
    for(ll i=1;i<=n;i++)
        cin>>a[i];
    for(ll i=1;i<=m;i++)
        cin>>b[i];
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++)
            C[i].c[j]=(a[i]&b[j]);
    }
    /*for(int i=1;i<=n;i++){
        for(int j=0;j<m;j++)
            cout<<c[i][j]<<' ';
        cout<<endl;
    }cout<<endl;*/
    for(int i=1;i<=n;i++){
        sort(C[i].c+1,C[i].c+1+m);
    }
    if(m==1){
        ll ans=0;
        for(int i=1;i<=n;i++)
            ans|=C[i].c[1];
        cout<<ans;
        return 0;
    }
    for(int i=1;i<=m;i++){
        if(C[1].c[i]<Min)
            dfs(C[1].c[i],2);
    }
    cout<<Min;
}


n = int(input())
a = list(map(int, input().split()))

a.sort()

b = [0 for x in range(n)]

for i in range(int((n + 1) / 2)):
    b[i * 2] = a[i]

for i in range(int(n / 2)):
    b[i * 2 + 1] = a[n - i - 1]

print(a, b)


#!/usr/bin/python3
# -*- config:utf-8 -*-

import sys

# get source data
def getSrcData():
    f = sys.stdin
    buf = f.read()
    buf=buf.split('\n')
    cnt=int(buf[0])
    if cnt!=0:
        src=buf[1].split(' ')
        src=[int(i) for i in src]
    else:
        src=[]
    return cnt,src

# find transform to zsort
def getTransform(cnt,src):
    src.sort()
    result=[]
    for i in range(0,cnt//2):
        result.append(src[i])
        result.append(src[cnt-1-i])
    if cnt%2==1:
        result.append(src[cnt//2])
    return result

# save result
def saveResult(result):
    if len(result)==0:
        pbuf='Impossible'
    else:
        pbuf=[str(i) for i in result]
        pbuf=' '.join(pbuf)
    f = sys.stdout
    f.write(pbuf)
    f.flush()

# start
def run():
    cnt,src=getSrcData()
    if cnt!=0:
        result=getTransform(cnt,src)
    else:
        result=[]
    saveResult(result)

if __name__ == '__main__':
    run()


def main():
    input()
    numbers = list(map(int, input().split()))
    numbers = sorted(numbers)
    n = len(numbers)
    result = [0 for _ in range(n)]
    j = 0
    k = n - 1
    for i in range(len(result)):
        if i % 2 == 1:
            result[i] = numbers[k]
            k -= 1
        else:
            result[i] = numbers[j]
            j += 1
    result = [str(el) for el in result]
    print(" ".join(result))

if __name__ == "__main__":
    main()


n=int(input())
t=list(map(int,input().split()))
t.sort()
l=[]
for i in range(len(t)//2+1):
  l.append(t[i])
  l.append(t[-i-1])
if len(t)%2==1 :
  l.pop()
print (l)


n=int(input())
t=list(map(int,input().split()))
t.sort()
l=[]
for i in range(len(t)//2):
  l.append(t[i])
  l.append(t[-i-1])
if len(t)%2==1 :
  l.append(t[len(t)//2])
print (l)


n=int(input())
t=list(map(int,input().split()))
t.sort()
l=[]
for i in range(len(t)//2):
  l.append(t[i])
  l.append(t[-i-1])
if len(t)%2==1 :
  l.append(t[len(t)//2])
for i in range (len(l)):
  print (l[i],end=' ')


#ROUNIAAUDI
num1=int(input())
list1=sorted(map(int,input().split()))
i=0
j=num1-1
list2=[]
while j>i:
    list2.append(list1[i])
    list2.append(list1[j])
    i+=1
    j-=1
if i==j:
    list2.append(list1[j])
for i in list2:
    print(i,end=" ")


n = int(input())
a = list(map(int,input().split()))
a.sort()
b = a[len(a) - len(a)//2:]
a = a[:len(a) - len(b)]
answ = ""
i = 0
while i < n:
    answ += str(a[i//2])
    answ += " ";
    i+=1
    print(i)
    if( i < n):
        answ += str(b[i//2])
    answ += " "
    i+=1;

print(answ)


n = int(input())
a = list(map(int,input().split()))
a.sort()
b = a[len(a) - len(a)//2:]
a = a[:len(a) - len(b)]
answ = ""
i = 0
while i < n:
    answ += str(a[i//2])
    answ += " ";
    i+=1
    if( i < n):
        answ += str(b[i//2])
    answ += " "
    i+=1;

print(answ)


def zSorted(anArr):
  for k in range(1, len(anArr), 2):
    if k != 0 and anArr[k] < anArr[k-1]: return False
  return True
numOfelems = int(input())
arrayA = [int(x) for x in input().split()]

if zSorted(arrayA): print(' '.join([str(x) for x in arrayA]))
else:
  for i in range(1,len(arrayA),2):
    if i != 0 and arrayA[i] < arrayA[i-1]: arrayA[i-1], arrayA[i] =  arrayA[i], arrayA[i-1]
  if zSorted(arrayA): print(' '.join([str(x) for x in arrayA]))
  else: print("Impossible")


def zSorted(anArr):
  for k in range(1, len(anArr), 2):
    if anArr[k] < anArr[k-1]: return False
  for k in range(2, len(anArr), 2):
    if anArr[k] > anArr[k-1]: return False
  return True
numOfelems = int(input())
arrayA = [int(x) for x in input().split()]

if zSorted(arrayA): print(' '.join([str(x) for x in arrayA]))
else:
  for i in range(1,len(arrayA),2):
    if arrayA[i] < arrayA[i-1]: arrayA[i-1], arrayA[i] =  arrayA[i], arrayA[i-1]
  for i in range(2,len(arrayA),2):
    if arrayA[i] > arrayA[i-1]: arrayA[i-1], arrayA[i] =  arrayA[i], arrayA[i-1]
  if zSorted(arrayA): print(' '.join([str(x) for x in arrayA]))
  else: print("Impossible")


n = int(input())
arr = (list(map(int, input().split())))
arr=sorted(arr)
for i in range(1, n):
    if i%2==1 and i != n - 1:
        arr[i], arr[i+1] = arr[i+1], arr[i]
print(*arr)


n = int(input())
arr = (list(map(int, input().split())))
arr=sorted(ls)
for i in range(1, n):
    if i%2==1 and i != n - 1:
        arr[i], arr[i+1] = arr[i+1], arr[i]
print(*arr)


n = int(input())
A = list(map(int, input().split()))
A.sort()
B = []
for i in range((n - 1) // 2 + 1):
    if i == n - i - 1:
        B.append(A[i])
    else:
        B.append(A[i])
        B.append(A[n - i - 1])

b = True
for i in range(1, n):
    if (i + 1) % 2 == 0:
        if B[i] >= B[i - 1]:
            pass
        else:
            b = False
            break
    else:
        if B[i] <= B[i - 1]:
            pass
        else:
            b = False
            break
if b:
    print(*B)
else:
    print('Impossible')


n = int(input())
A = list(map(int, input().split()))
A.sort()
B = []
for i in range((n - 1) // 2 + 1):
    if A[i] == n - i - 1:
        B.append(A[i])
    else:
        B.append(A[i])
        B.append(A[n - i - 1])

b = True
for i in range(1, n):
    if (i + 1) % 2 == 0:
        if B[i] >= B[i - 1]:
            pass
        else:
            b = False
            break
    else:
        if B[i] <= B[i - 1]:
            pass
        else:
            b = False
            break
if b:
    print(*B)
else:
    print('Impossible')


def main():
	n = int(input())
	a = sorted(map(int, input().split()))
	result = [0]*n
	# print(result)
	# basic = 0
	# # a.sort()
	# # print(a)
	# count = 0
	# if n&1==0:
	# 	while basic<n:
	# 		# print(basic, count)
	# 		result[count] = a[basic]
	# 		if count==n-2:
	# 			count = 1
	# 			basic+=1
	# 			# print(basic, count)
	# 			result[count]  = a[basic]
	# 		basic+=1
	# 		count+=2
	# else:
	# 	while basic<n:
	# 		# print(basic, count)
	# 		result[count] = a[basic]
	# 		if count==n-1:
	# 			count = 1
	# 			basic+=1
	# 			# print(basic, count)
	# 			result[count]  = a[basic]
	# 		basic+=1
	# 		count+=2

	result[::2] = a[:(n+1)//2]
	result[1::2] = a[(n+1)//2:]
	# print(a[basic])
	print(*result)

main()


n = int(input())
a = list(map(int, input().split()))
a.sort()
s = []
for i in range(0, len(a) // 2):
    s.append(a[i])
    s.append(a[len(a) - 1 - i])
if n % 2 == 1:
    s.append(a[len(a) // 2])

Po = 1
for k in range(2, len(s) - 1):
    if k % 2 == 1:
        if s[k] >= s[k - 1]:
            pass
        else:
            print('Impossible')
            Po = 0
            break
    elif k == 1:
        pass
    else:
        if s[k] <= s[k - 1]:
            pass
        else:
            print('Impossible')
            Po = 0
            break
if Po == 1:
    print(' '.join(map(str, s)))


n = int(input())
arr = (list(map(int, input().split())))
arr=sorted(arr)
for i in range(1, n):
    if i%2==1 and i != n - 1:
        arr[i], arr[i+1] = arr[i+1], arr[i]
print(*arr)

#interchange alternate


n=int(input())
a=list(map(int,input().split()))
a.sort()
l=n//2+1 if n%2 else n//2
j=0
q=True
for i in range(n):
    if q:
        print(a[j],end=' ')
        q=False
        j+=1
    else:
        print(a[l],end=' ')
        q=True
        l+=1


n = int(input())
a = list(map(int, input().split()))
a.sort()
if (n == 3):
    a[1], a[2] = a[2], a[1]
else:
    d = n - 2 + n % 2
    for i in range (1, n//2, 2):
        a[i], a[d] = a[d], a[i]
        d -= 2
for i in range (n):
    print(a[i], end=" ")


n = int(input())
a = list(map(int, input().split()))
a.sort()
if (n == 3):
    a[1], a[2] = a[2], a[1]
else:
    d = n - 2 + n % 2
    for i in range (1, n//2, 2):
        a[i], a[n - (i + 1) + n % 2] = a[n - (i + 1) + n % 2], a[i]
for i in range (n):
    print(a[i], end=" ")


n = int(input())

a = sorted(map(int, input().split()))

res = [10**18]

i = 0; j = n-1;
step = 0
while i <= j:
    if step == 0:
        res.append(a[i])
        i+=1
        if res[-1] > res[-2]:
            print("Impossible")
            break
    else:
        res.append(a[j])
        j-=1
        if res[-1] < res[-2]:
            print("Impossible")
            break
    step = step ^ 1
else:
    print(" ".join(map(str, res[1:])))


n=int(input())

tab=[["" for i in range(0,n)] for i in range(0,n)]

import math

def rotate(x,y):
    c = math.sqrt(2) / 2
    return (round(x * c + y * c), round(y *c - x * c))

def iterate(n,f,arg):
    x = arg
    for i in range(n):
        yield x
        x = f(*x)

print(list(iterate(8,rotate,(1,2))))

def map_append(f,l):
    return list(map(f,l))+l

def sym_1(pos):
    x,y=pos
    return (y,x)

def sym_2(pos):
    x,y=pos
    return (-x,-y)

def sym_3(pos):
    x,y=pos
    return (x,-y)

print(list(map_append(sym_3,map_append(sym_2,map_append(sym_1 , [(1,2)])))))

def neighbors(x,y):
    vect=[(1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1),(-2,1),(-1,2)]
    pos=[(x+i,y+j) for (i,j) in vect]
    return [(i,j) for (i,j) in pos if 0 <= i < n and 0 <= j < n]

def mark(is_black,pos):
    s = [(is_black,pos)]
    while s != []:
        is_black, (x,y) = s.pop()
        if tab[x][y] == "":
            if is_black:
                tab[x][y]="B"
            else:
                tab[x][y]="W"

            for pos in neighbors(x,y):
                s.append((not(is_black),pos))

for i in range(0,n):
    for j in range(0,n):
        if tab[i][j] == "":
            mark(False,(i,j))
        print(tab[i][j],end = '')
    print()


n = int(input())

list = sorted(map(int, input().split(' ')))

half = n // 2

first_half = list[:half]
second_half = list[half:]
second_half = second_half[::-1]

for i in range(0,half):
    print(first_half[i], end=' ')
    print(second_half[i], end=' ')
else:
    if n % 2 == 1:
        print(second_half[-1])


x = int(input())
L = [i for i in input().split()]
L.sort()
L2 = []

for i in range(len(L)):
   if i%2 == 0:
      L2.append(L[0])
      L.pop(0)
   else:
      L2.append(L[-1])
      L.pop(-1)

S = " ".join(L2)
print(S)


x = int(input())
L = [i for i in input().split()]
L.sort(key=int)
L2 = []

for i in range(len(L)):
   if i%2 == 0:
      L2.append(L[0])
      L.pop(0)
   else:
      L2.append(L[-1])
      L.pop(-1)

S = " ".join(L2)
print(S)


x = int(input())
L = sorted([i for i in input().split()], key=int)

L2 = []

for i in range(len(L)):
   if i%2 == 0:
      L2.append(L[0])
      L.pop(0)
   else:
      L2.append(L[-1])
      L.pop(-1)

S = " ".join(L2)
print(S)


n = int(input())
a = sorted(map(int, input().split(' ')))

ans = [0]*n
for i in range(n):
    ans[i] = a[i//2] if i%2 == 0 else a[(i+n)//2]

print(' '.join(map(str, ans)))


#! /usr/bin/env python3
'''
' Title:
' Author:	Cheng-Shih, Wong
' Date:
'''

import math

n = int(input())
a = [int(v) for v in input().split()]

a.sort()
half = math.ceil(n/2)
b = [0]*n

for i in range(0,half):
	b[i*2] = a[i]
for i in range(half,n):
	b[(i-half)*2+1] = a[i]

print( ' '.join([str(v) for v in b]) )


n = int(input())
L = [int(s) for s in input().split()]
def zsort(l):
    if len(l) == 1:
        return l
    if len(l) == 2:
        if l[0] > l[1]:
            return l[::-1]
        else:
            return l
    l1 = zsort(l[:2])
    l2 = zsort(l[2:])
    result = l1 + l2
    if result[1] < result[2]:
        result[1], result[2] = result[2], result[1]
    return result

result = [str(i) for i in zsort(L)]
print(' '.join(result))


n = int(input())
inp = list(map(int , input().split()))
srt = sorted(inp)
ans = []
if n % 2 == 0:
     for i in range(n // 2):
          ans.append(str(srt[i]))
          ans.append(str(srt[-(i + 1)]))
     print(" ".join(ans))
else:
     for i in range(n // 2 + 1):
          ans.append(str(srt[i]))
          ans.append(str(srt[-(i + 1)]))
     ans.pop(-1)
     print(" ".join(ans))


n = int(input())
a = list(map(int, input().split()))
a.sort()

l = 0
r = n - 1
while(l < r) :
    print(a[l], a[r], end = " ")
    l+=1
    r-=1
if(l == r) :
    print(a[l])


import math

n = int(input())
list = list(map(int, input().split()))
list.sort()

small = list[:int(math.ceil(n / 2))]
big = list[int(math.ceil(n / 2)):]

iter = 0
for i in range(n):
    if i % 2 == 0:
        list[i] = small[iter]
    else:
        list[i] = big[iter]
        iter += 1

flag = True
for i in range(n):
    if i % 2 == 0 and (i > 0 and list[i] > list[i - 1] or i < n - 1 and list[i] > list[i + 1]):
        flag = False
        break

if flag:
    print(' '.join(map(str, list)))
else:
    print(-1)


#!/usr/bin/python3

n = int(input())
a = sorted([int(i) for i in input().split()])

i = 0
d = 1
s = n-1

while s >= 0:
	print(a[i], end=' ')
	i += d * s
	s -= 1
	d *= -1

# Made By Mostafa_Khaled


n=int(input())
a=list(map(int,input().split()))

a.sort()
l=len(a)
if l%2==0:
    k=l//2
    for i in range(k):
        print(a[i],a[k+i],sep=' ',end=' ');
else:
    k=(l-1)//2;
    print(a[0],sep=' ',end=' ');
    for i in range(k):
        print(a[i+1],a[l-1-i],sep=' ',end=' ');


n=int(input())
a=list(map(int,input().split()))

a.sort()
l=len(a)
while l>0:
    print(a.pop(0),a.pop(),sep=' ',end=' ');
    l=len(a)


n=int(input())
a=list(map(int,input().split()))

a.sort()
l=len(a)
if l%2==0:
    k=l//2
    for i in range(k):
        print(a[i],a[k+i],sep=' ',end=' ');
else:
    k=(l-1)//2;
    print(a[0],sep=' ',end=' ');
    for i in range(k):
        print(a[l-1-i],a[i+1],sep=' ',end=' ');


n = int(input())
arr = [int(x) for x in input().split(" ")]

sorted_arr = sorted(arr, reverse=True)

z_sorted_arr = []

j = n - 1
for i in range(0,int(n/2)):
  z_sorted_arr.append(sorted_arr[j])
  z_sorted_arr.append(sorted_arr[i])
  j = j - 1

if n % 2 != 0:
  z_sorted_arr.append(sorted_arr[int(n/2)])

print(" ".join([str(x) for x in z_sorted_arr]))


n=int(input())
a=list(map(int,input().split()))
r=[]
a.sort()
i=0
for j in range(n//2):
        r.append(a[j])
        r.append(a[n-j-1])
if n%2!=0:
    r.append(a[n//2])
print(r)


n=int(input())
a=list(map(int,input().split()))
r=[]
a.sort()
i=0
for j in range(n//2):
        r.append(a[j])
        r.append(a[n-j-1])
if n%2!=0:
    r.append(a[n//2])
print(*r)


n = int(input()) - 1
a = sorted([int(x) for x in input().split(' ')])
for i in range(n):
    print(a[int((n + i + 1 if i % 2 else i) / 2)], end = ' ')
print(a[n if n % 2 else int(n / 2)])


n = int(input()) - 1
a = sorted(map(int, input().split()))
for i in range(n):
    print(a[int((n + i + 1 if i % 2 else i) / 2)], end = ' ')
print(a[n if n % 2 else int(n / 2)])


n=int(input())
n1=list(map(int,input().split()))
n1.sort()
lst=[]
k=0
for i in range(n//2):
    lst.append(n1[0])
    lst.append(n1[-1])
    n1.remove(n1[0])
    n1.remove(n1[-1])
if(n1!=[]):
    lst.append(n1[0])
for j in range(1,len(lst)-1):
    if((j-1)==0):
        continue
    else:
        if((j)%2==0):
            if(lst[j-1]>=lst[j-2]):
                continue
            else:
                k=k+1
                break
        else:
            if(lst[j-1]<=lst[j-2]):
                continue
            else:
                k=k+1
                break
if(k>0):
    print('IMPOSSIBLE')
else:
    for j in lst:
        print(j,end=' ')


n = int(input())
ls = sorted(list(map(int, input().split())))
for i in range(1, n):
    if i%2 and i != n - 1:
        ls[i], ls[i+1] = ls[i+1], ls[i]
print(*ls)


n=int(input())
ch=input()
d=ch.split()
for i in range(n):
    d[i]=int(d[i])
d.sort()
d1=[]
i=0
j=0
while j<n:
    d1.append(d[i])
    j+=1
    if j<n:
        j+=1
        d1.append(d[n-i-1])
    i+=1
B=False
for i in range(n):
    if d1[i]<d1[i-1] and (i+1)%2==0:
        B=True
    if d1[i]>d1[i-1] and (i+1)%2!=0:
        B=True
if B==False:
    for i in d1:
        print(i,end=" ")
else:
    print( "Impossible")


n = int(input())
l = list(map(int,input().split()))

for i in range(n):
    if(i%2==0):
        if(l[i]>=l[i-1]):
            l[i],l[i-1] = l[i-1],l[i]
    else:
        if(l[i]<=l[i-1]):
            l[i],l[i-1] = l[i-1],l[i]

print(*l)


n = int(input())
a = list(map(int,input().split()))
a.sort()
b = list(reversed(a))
c = a + b
c[::2]=a
c[1::2]=b
c=c[:len(c)//2]
print(c)


n = int(input())
a = list(map(int,input().split()))
a.sort()
b = list(reversed(a))
c = a + b
c[::2]=a
c[1::2]=b
c=c[:len(c)//2]
for i in c:
    print(i,sep=' ',end=' ')


n = int(input())
a = [int(x) for x in input().split(" ")]

a = sorted(a)

odd = n%2==1
l = a[:n//2+odd]
r = a[n//2+odd:]
r = r[::-1]

res = []
for i in range(n//2):
	res.append(l[i])
	res.append(r[i])
if odd:
	res.append(l[n//2])

print(res)


n = int(input())
a = [int(x) for x in input().split(" ")]

a = sorted(a)

odd = n%2==1
l = a[:n//2+odd]
r = a[n//2+odd:]
r = r[::-1]

res = []
for i in range(n//2):
	res.append(l[i])
	res.append(r[i])
if odd:
	res.append(l[n//2])

print(" ".join([str(x) for x in res]))


n = int(input())
l = sorted(list(map(int, input().split())))
for i in range(1,n):
    if(i%2 and i!=n-1):
        l[i], l[i+1] = l[i+1],l[i]
print(*l)


n=int(input())
x=[int(q) for q in input().split()]

y=sorted(x)
z=[]

if n&1==0:
    for i in range(0,n//2):
        z.append(y[i])
        z.append(y[n-i-1])
    print(*z)
else:
    for i in range(0,1+ n//2):
        z.append(y[i])
        z.append(y[n-i-1])
    z.pop()
    print(*z)


n=int(input())
t=list(map(int,input().split()))
t.sort()
for i in range(len(t)//2):
    print(t[i],end=" ")
    print(t[len(t)-1-i],end=" ")
if len(t)%2:
    print(t[len(t)//2])


n=int(input())
t=list(map(int,input().split()))
p=[]
n=[]
for i in t:
    if i%2 and i-1:
        n.append(-i)
    else:
        p.append(i)
p.sort()
n.sort()
for i in p:
    print(i,end=" ")
for i in n:
    print(-i,end=" ")


#!/usr/bin/env python3.5
import sys

def read_data():
    n = int(next(sys.stdin).strip())
    numbers = list(map(int, next(sys.stdin).split()))
    return numbers

def solve(numbers):
    N = len(numbers)
    numbers.sort()
    lN, rN = numbers[:(N+1)//2], numbers[(N+1)//2:]
    ans = [0] * N
    for i in range((N+1) // 2):
        ans[2*i] = lN[i]
        if 2*i + 1 < N:
            ans[2*i + 1] = rN[i]
    return ans

if __name__ == "__main__":
    print(*solve(read_data()))


nums = int(input())
list_num = input().split()
for num in range(int(nums)):
    list_num[num] = int(list_num[num])
list_num.sort()
result = ''
start = 0
end = nums-1
for n in range(int(nums)):
    if n%2 ==0:
        result += (str(list_num[start]) + ' ')
        start+=1
    else:
        result +=(str(list_num[end]) + ' ')
        end+=-1
print(result)


a=int(input());b=sorted(map(int,input().split()))
for i in range(a):print(b[i] if i%2 else b[a-i-1],end=" ")


a=int(input());b=sorted(map(int,input().split()));i=0
while(i<a//2):print(b[i],b[-i-1],end=" ");i+=1
if a%2:print(b[a//2])


n = int(input())
l = list(map(int, input().split()))
l.sort(reverse = True)
mas = [0 for i in range(n)]
t = 0
for i in range(1, n, 2):
    mas[i] = l[t]
    t += 1
for i in range(0, n, 2):
    mas[i] = l[t]
    t += 1
print(*mas)


n = int(input())
l = list(map(int, input().split()))
l.sort()
mas = []
for i in range(1, n, 2):
    mas.append(l[i])
mas.sort(reverse = True)
t = 0
for i in range(1, n, 2):
    l[i] = mas[t]
    t += 1
print(*l)


n = int(input())
a = list(map(int,input().split()))

for i in range(n-1):
	if (i+1)%2==1:
		if a[i]>a[i+1]:
			t = a[i]
			a[i] = a[i+1]
			a[i+1] = t
		else:
			pass
	else:
		if a[i]<a[i+1]:
			t = a[i]
			a[i] = a[i+1]
			a[i+1] = t
		else:
			pass

for i in range(n):
	print(a[i], end = ' ')


n=int(input())
a=sorted(map(int,input().split()))
a=[x for l in zip(a[:0--n//2],a[0--n//2:]) for x in l]+[a[n//2]]
print(*a[:n])


n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
for i in range(n):
    if i%2==0:
        ans.append(str(a[i//2]))
    else:
        ans.append(str(a[-i//2]))

print(" ".join(ans))


n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
for i in range(n):
    if i%2==0:
        ans.append(a[i//2])
    else:
        ans.append(a[-i//2])

print(ans)


n = gets.to_i
a = gets.split(" ").map(&:to_i).sort
i = 0
j = n-1

b = []
while true do
  b << a[i]
  i += 1
  break if i > j
  b << a[j]
  j -= 1
  break if i > j
end

valid = true
n.times do |i|
  if (i+1).even?
    valid = false if b[i] < b[i-1]
  elsif (i+1).odd? && i > 0
    valid = false if b[i] > b[i-1]
  end
end

puts valid ? b.join(" ") : "Impossible"


n = gets.to_i
a = gets.split.map(&:to_i)
for i in 0..n-1
    if (i+1)%2==0 then printf "%d ", a.max; a.delete_at a.index(a.max);
    else printf "%d ", a.min; a.delete_at a.index(a.min); end
end


n = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
res = []
for i in 0..n[0] - 1 do
  if i == 0 or (i + 1) % 2 != 0
    res[i] = arr.min
  else
    res[i] = arr.max
  end
  arr.delete_at(arr.index(res[i]) || arr.length)
end
puts res.join(' ')


n = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i)
res = []
for i in 0..n[0] - 1 do
  if i == 0 or (i + 1) % 2 != 0
    res[i] = arr.min
  else
    res[i] = arr.max
  end
  arr.delete_at(i)
end
puts res.join(' ')


#!/usr/bin/env ruby

n = gets.to_i
inp = gets.split.map(&:to_i)
for i in 0..n-1
  if (i+1) % 2 == 0 then printf "%d ", inp.max; inp.delete_at inp.index(inp.max)
  else printf "%d ", inp.min; inp.delete_at inp.index(inp.min) end
end
puts


n = readline.to_i
arr = readline.split(' ').collect(&:to_i).sort
arr[0..(n / 2)].zip(arr[(n / 2 + 1)..-1]).flatten.compact.each_with_index do |num, ind|
  print " " if ind != 0
  print num
end
puts ""


n = readline.to_i
arr = readline.split(' ').collect(&:to_i).sort
puts arr
arr[0..(n / 2)].zip(arr[(n / 2 + 1)..-1]).flatten.compact.each_with_index do |num, ind|
  print " " if ind != 0
  print num
end
puts ""


a=gets.chomp.to_i
b=gets.chomp.split(" ")
0.upto(a-1) {|i| b[i]=b[i].to_i}
b.sort!
if a%2==0
0.upto (a/2-1) do |i|
printf "#{b[i]} #{b[a/2+i]} "
end
else
0.upto (a/2) do |i|
if i!=a/2
printf "#{b[i]} #{b[a/2+1+i]} "
else
printf "#{b[i]}"
end
end
end


gets
a = gets.split.map(&:to_i)
a.sort!
b = []
a.size.times do |i|
  if i.even?
    b << a.shift
  else
    b << a.pop
  end
end
puts b.join ' '


n=gets.to_i
a=gets.split(' ')
for i in 0..a.size-1
	a[i]=a[i].to_i
end
a=a.sort
l=0
r=a.size-1
ans=""
while l<=r
	ans+=" #{a[l]}"
	ans+=" #{a[r]}" if l<r
	l+=1
	r-=1
end
puts ans.strip


gets # ignore
arr = gets.split(" ").map(&:to_i)
if arr.length <= 1
  puts "Impossible"
  exit 0
end

len = arr.length
do_swap = len % 2 != 0

sorted = arr.sort

lo = 0
hi = len/2

ret = []
(0...(len/2)).each do
  ret << sorted[lo]
  ret << sorted[hi]

  lo += 1
  hi += 1
end

if do_swap
  tmp = ret[-1]
  ret[-1] = arr[hi]
  ret << tmp
end

puts ret.join(" ")


gets # ignore
arr = gets.split(" ").map(&:to_i)

if arr.length <= 1
  puts arr.join(" ")
  exit
end

len = arr.length
do_swap = len % 2 != 0

sorted = arr.sort

lo = 0
hi = len/2

ret = []
(0...(len/2)).each do
  ret << sorted[lo]
  ret << sorted[hi]

  lo += 1
  hi += 1
end

if do_swap
  tmp = ret[-1]
  ret[-1] = arr[hi]
  ret << tmp
end

puts ret.join(" ")


gets # ignore
sorted = gets.split(" ").map(&:to_i).sort
lo, hi = 0, sorted.length - 1

ret = []
while lo < hi
  ret << sorted[lo]
  ret << sorted[hi]

  lo += 1
  hi -= 1
end
ret << sorted[hi]

puts ret.join(" ")


gets # ignore
sorted = gets.split(" ").map(&:to_i).sort
lo, hi = 0, sorted.length - 1

ret = []
while lo < hi
  ret << sorted[lo]
  ret << sorted[hi]

  lo += 1
  hi -= 1
end
ret << sorted[hi] if lo == hi

puts ret.join(" ")


gets # ignore
arr = gets.split(" ").map(&:to_i)

len = arr.length
do_swap = len % 2 != 0

sorted = arr.sort

lo = 0
hi = len/2

ret = []
(0...(len/2)).each do
  ret << sorted[lo]
  ret << sorted[hi]

  lo += 1
  hi += 1
end

if do_swap
  tmp = ret[-1]
  ret[-1] = arr[hi]
  ret << tmp
end

puts ret.join(" ")


gets # ignore
arr = gets.split(" ").map(&:to_i)
if arr.length <= 1
  puts "Impossible"
  exit(1)
end

len = arr.length
do_swap = len % 2 != 0

sorted = arr.sort

lo = 0
hi = len/2

ret = []
(0...(len/2)).each do
  ret << sorted[lo]
  ret << sorted[hi]

  lo += 1
  hi += 1
end

if do_swap
  tmp = ret[-1]
  ret[-1] = arr[hi]
  ret << tmp
end

puts ret.join(" ")


gets # ignore
arr = gets.split(" ").map(&:to_i)

if arr.length <= 1
  puts arr.join(" ")
  exit
end

len = arr.length
do_swap = len % 2 != 0

sorted = arr.sort

lo = 0
hi = len/2

ret = []
(0...(len/2)).each do
  ret << sorted[lo]
  ret << sorted[hi]

  lo += 1
  hi += 1
end

if do_swap
  tmp = ret[-1]
  ret[-1] = sorted[hi]
  ret << tmp
end

puts ret.join(" ")


from sys import stdin

n = int( stdin.readline() )
elements = sorted( map( int, stdin.readline().split() ) )

answer = [elements.pop(0)]

while len(elements) > 0:
    answer.append( elements.pop() )
    if len(elements) > 0:
        answer.append( elements.pop(0) )

isImposilble = False
for i in xrange( n-1 ):
    if answer[i] == answer[i+1]:
        isImposilble = True
        break

if isImposilble:
    print "Impossible"
else:
    for i in answer:
        print i,
    print


from sys import stdin

n = int( stdin.readline() )
elements = sorted( map( int, stdin.readline().split() ) )

answer = [elements.pop(0)]

while len(elements) > 0:
    answer.append( elements.pop() )
    if len(elements) > 0:
        answer.append( elements.pop(0) )

isImposilble = False
for i in xrange(1, n ):
    if i % 2 == 1:
        if not (answer[i] >= answer[i-1]):
            isImposilble = True
            break
    else:
        if not (answer[i] <= answer[i-1]):
            isImposilble = True
            break

if isImposilble:
    print "Impossible"
else:
    for i in answer:
        print i,
    print


import sys
n = int(sys.stdin.readline())
A = map(int,sys.stdin.readline().split())
A.sort()
x = [0]*n
j = 1
for a in A[::-1]:
  if j>=n:
    break
  x[j] = a
  j += 2
j=0
for a in A:
  if j>=n:
    break
  x[j] = a
  j += 2
for i in x:
  print i,
print