#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<int>())
#define init(a)  memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007

/*struct comp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};*/

ll cel(ll a,int b){return((a-1)/b+1);}
ll gcd(ll a, ll b){
    if (a < b)swap(a, b);
    return (b == 0)? a: gcd(b, a % b);
}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}

bool g(int y,int i){
    rep(j,0,i-1){y/=2;}
    if(y%2)return true;
    return false;
}
int main()
{
    FIO;
    ll n,t,x,m,y,r;
    cin>>n>>m;
    vi chk(512,0),ans(10,1),u;vi A[10],B[10];
    rep(i,0,n){
        cin>>x;
        if(chk[x])continue;
        y=1;r=x;
        while(x){
            if(x%2)A[y].pb(r);
            y++;x/=2;
        }
        chk[r]=1;
    }
    rep(i,0,m){
        cin>>x;
        y=1;u.pb(x);
        r=x;
        while(x){
            if(x%2)B[y].pb(r);
            y++;x/=2;
        }
    }
    int C[512][m];rep(i,0,512){rep(j,0,m)C[i][j]=1;}
    int se[m][10];
    //vi fl(m,1);Vi C(512,fl);
    //for(int t:A[3]){cout<<t<<' ';}
    rep(i,0,m){rep(j,1,10){se[i][j]=int(g(u[i],j));}}
    //cout<<se[0][1]<<" "<<se[0][2];
    repb(i,9,1){
        if(A[i].empty()){ans[i]=0;}
        else{
            if(B[i].empty()){ans[i]=0;}
            else{
                int k=1;
                for(int t:A[i]){
                    k=1;
                    //cout<<t<<" "<<se[t][4];
                    rep(j,0,m){
                        if((se[j][i]==0)&&(C[t][j])){k=0;break;}
                    }
                    if(k){break;}
                }
                if(k)ans[i]=1;
                else{
                   for(int t:A[i]){
                    rep(j,0,m){
                        if(se[j][i])C[t][j]=0;
                    }
                    }
                    ans[i]=0;
                }
            }
        }
    }
    int q=0,z=1;
    rep(i,1,9){
        if(ans[i]){q+=z;}
        z*=2;
    }
    cout<<q;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<int>())
#define init(a)  memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007

/*struct comp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};*/

ll cel(ll a,int b){return((a-1)/b+1);}
ll gcd(ll a, ll b){
    if (a < b)swap(a, b);
    return (b == 0)? a: gcd(b, a % b);
}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}

vi A(201),B(201);int n,m;

bool chk(int x){
    rep(i,0,n){
        bool ok=false;
        rep(j,0,m){
            if(((A[i]&B[j])|x)==x){ok=true;break;}
        }
        if(!ok)return false;
    }
    return true;
}

int main()
{
    FIO;
    ll t,x,y;
    cin>>n>>m;
    rep(i,0,n){cin>>A[i];}
    rep(i,0,m){cin>>B[i];}
    int ans=(1<<9)-1;
    repb(i,8,0){chk(ans^(1<<i))?(ans^=(1<<i)):0;}
    cout<<ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<int>())
#define init(a)  memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007

/*struct comp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};*/

ll cel(ll a,int b){return((a-1)/b+1);}
ll gcd(ll a, ll b){
    if (a < b)swap(a, b);
    return (b == 0)? a: gcd(b, a % b);
}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}

bool g(int y,int i){
    rep(j,0,i-1){y/=2;}
    if(y%2)return true;
    return false;
}
int main()
{
    FIO;
    ll n,t,x,m,y,r;
    cin>>n>>m;
    vi chk(512,0),ans(10,1),u;vi A[10],B[10];
    rep(i,0,n){
        cin>>x;
        if(chk[x])continue;
        y=1;r=x;
        while(x){
            if(x%2)A[y].pb(r);
            y++;x/=2;
        }
        chk[r]=1;
    }
    rep(i,0,m){
        cin>>x;
        y=1;u.pb(x);
        r=x;
        while(x){
            if(x%2)B[y].pb(r);
            y++;x/=2;
        }
    }
    int C[512][m];rep(i,0,512){rep(j,0,m)C[i][j]=1;}
    //vi fl(m,1);Vi C(512,fl);
    //for(int t:A[3]){cout<<t<<' ';}
    repb(i,9,1){
        if(A[i].empty()){ans[i]=0;}
        else{
            if(B[i].empty()){ans[i]=0;}
            else{
                int k=1;
                for(int t:A[i]){
                    k=1;
                    //cout<<t<<" "<<C[t][4];
                    for(int y:u){
                        if((!g(y,i))&&(C[t][y])){k=0;break;}
                    }
                    if(k){break;}
                }
                if(k)ans[i]=1;
                else{
                   for(int t:A[i]){
                    for(int y:u){
                        if(g(y,i))C[t][y]=0;
                    }
                    }
                    ans[i]=0;
                }
            }
        }
    }
    int q=0,z=1;
    rep(i,1,9){
        if(ans[i]){q+=z;}
        z*=2;
    }
    cout<<q;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<int>())
#define init(a)  memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007

/*struct comp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};*/

ll cel(ll a,int b){return((a-1)/b+1);}
ll gcd(ll a, ll b){
    if (a < b)swap(a, b);
    return (b == 0)? a: gcd(b, a % b);
}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}

int g(int y,int i){
    return (1<<(i-1))&y;
}
int main()
{
    FIO;
    ll n,t,x,m,y,r;
    cin>>n>>m;
    vi chk(512,0),ans(10,1),u;vi A[10],B[10];
    rep(i,0,n){
        cin>>x;
        if(chk[x])continue;
        y=1;r=x;
        while(x){
            if(x%2)A[y].pb(r);
            y++;x/=2;
        }
        chk[r]=1;
    }
    rep(i,0,m){
        cin>>x;
        y=1;u.pb(x);
        r=x;
        while(x){
            if(x%2)B[y].pb(r);
            y++;x/=2;
        }
    }
    int C[512][m];rep(i,0,512){rep(j,0,m)C[i][j]=1;}
    int se[m][10];

    rep(i,0,m){rep(j,1,10){se[i][j]=(g(u[i],j));}}
    //cout<<se[0][1]<<" "<<se[0][2];
    //cout<<A[9][0];
    repb(i,9,1){
        if(A[i].empty()){ans[i]=0;}
        else{
            //if(B[i].empty()){ans[i]=0;}
            //{
                int k=1;
                for(int t:A[i]){
                    k=1;
                    rep(j,0,m){
                        if((se[j][i]==0)&&(C[t][j])){k=0;break;}
                    }
                    if(k){break;}
                }
                if(k)ans[i]=1;
                else{
                   for(int t:A[i]){
                    rep(j,0,m){
                        if(se[j][i])C[t][j]=0;
                    }
                    }
                    ans[i]=0;
                }
            //}
        }
    }
    int q=0,z=1;
    rep(i,1,10){
        if(ans[i]){q+=z;}
        z*=2;
    }
    cout<<q;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>

using namespace std;
#define set_precision cout << fixed << setprecision(10)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define repit(arr) for (auto it = arr.begin(); it != arr.end(); it++)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<int>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<int>())
#define init(a)  memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007

/*struct comp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};*/

ll cel(ll a,int b){return((a-1)/b+1);}
ll gcd(ll a, ll b){
    if (a < b)swap(a, b);
    return (b == 0)? a: gcd(b, a % b);
}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}

bool g(int y,int i){
    rep(j,0,i-1){y/=2;}
    if(y%2)return true;
    return false;
}
int main()
{
    FIO;
    ll n,t,x,m,y,r;
    cin>>n>>m;
    vi chk(512,0),ans(10,1),u;vi A[10],B[10];
    rep(i,0,n){
        cin>>x;
        if(chk[x])continue;
        y=1;r=x;
        while(x){
            if(x%2)A[y].pb(r);
            y++;x/=2;
        }
        chk[r]=1;
    }
    rep(i,0,m){
        cin>>x;
        y=1;u.pb(x);
        r=x;
        while(x){
            if(x%2)B[y].pb(r);
            y++;x/=2;
        }
    }
    int C[512][m];rep(i,0,512){rep(j,0,m)C[i][j]=1;}
    int se[m][10];
    //vi fl(m,1);Vi C(512,fl);
    //for(int t:A[3]){cout<<t<<' ';}
    rep(i,0,m){rep(j,1,10){se[i][j]=int(g(u[i],j));}}
    //cout<<se[0][1]<<" "<<se[0][2];
    repb(i,9,1){
        if(A[i].empty()){ans[i]=0;}
        else{
            if(B[i].empty()){ans[i]=0;}
            else{
                int k=1;
                for(int t:A[i]){
                    k=1;
                    //cout<<t<<" "<<se[t][4];
                    rep(j,0,m){
                        if((se[j][i]==0)&&(C[t][u[j]])){k=0;break;}
                    }
                    if(k){break;}
                }
                if(k)ans[i]=1;
                else{
                   for(int t:A[i]){
                    rep(j,0,m){
                        if(se[j][i])C[t][u[j]]=0;
                    }
                    }
                    ans[i]=0;
                }
            }
        }
    }
    int q=0,z=1;
    rep(i,1,9){
        if(ans[i]){q+=z;}
        z*=2;
    }
    cout<<q;
    return 0;
}


   #include<bits/stdc++.h>
    using namespace std;
    long long int  a[205],b[205],c[205];
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);cout.tie(0);
    long long int min1;
    	int n,m;cin>>n>>m;
    	for(int i=0;i<n;++i)
    		cin>>a[i];
    	for(int i=0;i<m;++i)
    		cin>>b[i];
    	for(int i=0;i<n;++i){
    	    min1=100000000;
    		for(int j=0;j<m;++j){
    		  min1	=min(min1,a[i]&b[j]);
    		     c[i]=min1;
    		}

    	}
 long long   int	sum=c[0];
    	for(int i=1;i<n;i++){
    	    sum=sum|c[i];
    	}
    	cout<<sum<<"\n";
    }


   #include<bits/stdc++.h>
    using namespace std;
    int a[205],b[205],c[205];
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);cout.tie(0);
    	int min1;
    	int n,m;cin>>n>>m;
    	for(int i=0;i<n;++i)
    		cin>>a[i];
    	for(int i=0;i<m;++i)
    		cin>>b[i];
    	for(int i=0;i<n;++i){
    	    min1=INT_MAX;
    		for(int j=0;j<m;++j){
    		  min1	=min(min1,a[i]&b[j]);
    		     c[i]=min1;
    		}

    	}
    int	sum=c[0];
    	for(int i=1;i<=n-1;i++){
    	    sum=sum|c[i];
    	}
    	cout<<sum<<"\n";
    }


   #include<bits/stdc++.h>
    using namespace std;
    int a[205],b[205],c[205];
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);cout.tie(0);
    	int min1;
    	int n,m;cin>>n>>m;
    	for(int i=0;i<n;++i)
    		cin>>a[i];
    	for(int i=0;i<m;++i)
    		cin>>b[i];
    	for(int i=0;i<n;++i){
    	    min1=INT_MAX;
    		for(int j=0;j<m;++j){
    		  min1	=min(min1,a[i]&b[j]);
    		     c[i]=min1;
    		}

    	}
    int	sum=c[0];
    	for(int i=1;i<n;i++){
    	    sum=sum|c[i];
    	}
    	cout<<sum<<"\n";
    }


    #include <bits/stdc++.h>

    using namespace std;
    const int inf = 209,MX = 1e9+9;
    int dp[inf][(1<<9)+5],n,m,a[inf],b[inf];

    int solve(int cur,int mask){

        if(cur == n+1)
            return mask;
        int &ret = dp[cur][mask];
        if(ret != -1)
            return ret;
        ret = MX;
        for(int j=1;j<=m;j++)
            ret = min(ret,solve(cur+1, mask | (a[cur]&b[j]) ));
        return ret;
    }

    int main()
    {

        cin>>n>>m;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int j=1;j<=m;j++)
            cin>>b[j];
        memset(dp,-1,sizeof(dp));
        cout<<solve(1,0)<<endl;
    }


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

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;

    vector<int> a(n), b(m);

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

    int c =0;
    // 전처리, 무조건 들어갈수 밖에 없는 비트를 가리기
    for(int i=0;i<n;++i){

        int acc = 0x3ff;
        for(int j=0;j<m;++j) acc &= (a[i]&b[j]);

        c |= acc; // acc 에는 무조건 들어가는 비트가 들어감.
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    for(int i=0;i<n;++i){

        int t = 0x3ff;
        for(int j=0;j<m;++j){
            t = min(t, (a[i]&b[j])|c);
        }
        c |= t;
    }

    cout << c << '\n';

    return 0;
}


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

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;

    vector<int> a(n), b(m);

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

    vector<int> c;
    for(int i=0;i<n;++i){

        int t = 987654321;
        // a[i] & b[j] = c[i] 인데 그 값이 가장 작게 하자.
        for(int j=0;j<m;++j){
            t = min(t, a[i]&b[j]);
        }
        c.push_back(t);
    }

    int ans =0;
    for(int i=0;i<n;++i) ans |= c[i];
    cout << ans << '\n';

    return 0;
}


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

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;

    vector<int> a(n), b(m);

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

    int c =0;
    // 전처리, 무조건 들어갈수 밖에 없는 비트를 가리기
    for(int i=0;i<n;++i){

        int acc = 0x3ff;
        for(int j=0;j<m;++j) acc &= (a[i]&b[j]);

        c |= acc; // acc 에는 무조건 들어가는 비트가 들어감.
    }

    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    for(int i=0;i<n;++i){

        int t = 0x3ff;
        for(int j=0;j<m;++j){
            t = min(t, (a[i]&b[j])|c);
        }
        c |= t;
    }

    cout << c << '\n';

    return 0;
}


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

 vector<int> a, b;

bool check(int t){
    for(int i=0;i<a.size();++i){
        for(int j=0;j<b.size();++j) if(((a[i]&b[j])|t) == t) goto Next;
        return 0;
        Next: ;
    }
    return 1;
}
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;
    a.resize(n); b.resize(m);

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

    int ans = (1<<9) -1;
    for(int i=8;i>=0;--i) check(ans^(1<<i))? ans^=(1<<i) : 0;
    cout << ans << '\n';
    return 0;
}


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

 vector<int> a, b;

bool check(int t){
    for(int i=0;i<a.size();++i){
        for(int j=0;j<b.size();++j) if((a[i]&b[j])|t == t) goto Next;
        return 0;
        Next: ;
    }
    return 1;
}
int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;
    a.resize(n); b.resize(m);

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

    int ans = (1<<9) -1;
    for(int i=8;i>=0;--i) check(ans^(1<<i))? ans^=(1<<i) : 0;
    cout << ans << '\n';
    return 0;
}


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

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;

    vector<int> a(n), b(m);

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

    int c =0;
    // 전처리, 무조건 들어갈수 밖에 없는 비트를 가리기
    for(int i=0;i<n;++i){

        int acc = 0x3ff;
        for(int j=0;j<m;++j) acc &= (a[i]&b[j]);

        c |= acc; // acc 에는 무조건 들어가는 비트가 들어감.
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());

    int ans = 0;
    for(int i=0;i<n;++i){

        int t = 0x3ff;
        for(int j=0;j<m;++j){
            t = min(t, (a[i]&b[j])|c);
        }
        ans |= t;
    }
    cout << ans << '\n';
    return 0;
}


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

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n,m;
    cin >> n >> m;

    vector<int> a(n), b(m);

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

    int c =0;
    // 전처리, 무조건 들어갈수 밖에 없는 비트를 가리기
    for(int i=0;i<n;++i){

        int acc = 0x3ff;
        for(int j=0;j<m;++j) acc &= (a[i]&b[j]);

        c |= acc; // acc 에는 무조건 들어가는 비트가 들어감.
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    for(int i=0;i<n;++i){

        int t = 0x3ff;
        for(int j=0;j<m;++j){
            t = min(t, (a[i]&b[j])|c);
        }
        c |= t;
    }
    cout << c << '\n';
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define gcd __gcd
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define srtd(v) sort(v.rbegin(),v.rend())
using namespace std;
const ll N=2e5+5;
const ll mod=1e9+7 ;
int main()
{
 int t=1;
 while(t--)
 {
   ll n,m;
   cin>>n>>m;
   ll a[n+5],b[m+5],minm=1e12,temp,fans;
   fo(i,n)
   cin>>a[i];
   fo(i,m)
   cin>>b[i];
   sort(a,a+n);
   fo(i,m)
   {
     if((a[n-1]&b[i])<minm)
     minm=a[n-1]&b[i];
   }
   fo(i,n-1)
   {
     fans=1e12;
     fo(j,m)
     {
       temp=b[j]&a[i];
       if((temp|minm)<fans)
       fans=temp|minm;
     }
     minm=fans;
   }
   cout<<minm<<endl;
 }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
#define F first
#define S second
using namespace std;
const ll N=5e5+5;
const ll mod=1e9+7;
int main()
{
  ll n,m;
  cin>>n>>m;
  ll a[n+5],b[m+5],minm,ans=0;
  fo(i,n)
   cin>>a[i];
  fo(i,m)
   cin>>b[i];
  sort(a,a+n,greater<ll>());
  fo(i,n)
  {
    minm=1e9;
    fo(j,m)
    {
      if((ans|(a[i]&b[j]))<(ans|minm))
       minm=a[i]&b[j];
    }
    ans=ans|minm;

  }
  cout<<ans;
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define gcd __gcd
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define srtd(v) sort(v.rbegin(),v.rend())
using namespace std;
const ll N=2e5+5;
const ll mod=1e9+7 ;
int main()
{
 int t=1;
 while(t--)
 {
   ll n,m;
   cin>>n>>m;
   ll a[n+5],b[m+5],maxm=0,temp,fans,x=1e12;
   fo(i,n)
   cin>>a[i];
   fo(i,m)
   cin>>b[i];
   sort(a,a+n);
   fo(i,n)
   {
     x=1e12;
     fo(j,m)
     {
       x=min(x,a[i]&b[j]);
     }
     maxm=max(maxm,x);
   }
   fo(i,n)
   {
     fans=1e12;
     fo(j,m)
     {
       temp=b[j]&a[i];
       if((temp|maxm)<(maxm|fans))
       fans=temp;
     }
     maxm=fans|maxm;
   }
   cout<<maxm<<endl;
 }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define gcd __gcd
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define srtd(v) sort(v.rbegin(),v.rend())
using namespace std;
const ll N=2e5+5;
const ll mod=1e9+7 ;
int main()
{
 int t=1;
 while(t--)
 {
   ll n,m;
   cin>>n>>m;
   ll a[n+5],b[m+5],maxm=0,temp,fans,x=1e12;
   fo(i,n)
   cin>>a[i];
   fo(i,m)
   cin>>b[i];
   sort(a,a+n);
   fo(i,n)
   {
     fo(j,m)
     {
       x=min(x,a[i]&b[j]);
     }
     maxm=max(maxm,x);
   }
   fo(i,n)
   {
     fans=1e12;
     fo(j,m)
     {
       temp=b[j]&a[i];
       if((temp|maxm)<(maxm|fans))
       fans=temp;
     }
     maxm=fans|maxm;
   }
   cout<<maxm<<endl;
 }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define gcd __gcd
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define srtd(v) sort(v.rbegin(),v.rend())
using namespace std;
const ll N=2e5+5;
const ll mod=1e9+7 ;
int main()
{
 int t=1;
 while(t--)
 {
   ll n,m;
   cin>>n>>m;
   ll a[n+5],b[m+5],minm=1e12,temp,fans;
   fo(i,n)
   cin>>a[i];
   fo(i,m)
   cin>>b[i];
   sort(a,a+n);
   fo(i,m)
   {
     if((a[n-1]&b[i])<minm)
     minm=a[n-1]&b[i];
   }
   fo(i,n-1)
   {
     fans=1e12;
     fo(j,m)
     {
       temp=b[j]&a[i];
       if((temp|minm)<fans)
       fans=temp|minm;
     }
     minm=fans;
   }
   cout<<fans<<endl;
 }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define gcd __gcd
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define srtd(v) sort(v.rbegin(),v.rend())
using namespace std;
const ll N=2e5+5;
const ll mod=1e9+7 ;
int main()
{
 int t=1;
 while(t--)
 {
   ll n,m;
   cin>>n>>m;
   ll a[n+5],b[m+5],minm=1e12,temp,fans;
   fo(i,n)
   cin>>a[i];
   fo(i,m)
   cin>>b[i];
   sort(a,a+n);
   fo(i,m)
   {
     if((a[n-1]&b[i])<minm)
     minm=a[n-1]&b[i];
   }
   fo(i,n-1)
   {
     fans=1e12;
     fo(j,m)
     {
       temp=b[j]&a[i];
       if((temp|minm)<(minm|fans))
       fans=temp;
     }
     minm=fans|minm;
   }
   cout<<minm<<endl;
 }
 return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define gcd __gcd
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define srtd(v) sort(v.rbegin(),v.rend())
using namespace std;
const ll N=2e5+5;
const ll mod=1e9+7 ;
int main()
{
 int t=1;
 while(t--)
 {
   ll n,m;
   cin>>n>>m;
   ll a[n+5],b[m+5],ans=0,f=0,g=0,x,val;
   fo(i,n)
   cin>>a[i];
   fo(i,m)
   cin>>b[i];
   x=pow(2,9);
   fo(j,x)
   {
     g=0;
     fo(i,n)
     {
       f=0;
       fo(k,m)
       {
         if(((a[i]&b[k])|j)==j)
         {
            f=1;
            break;
         }
       }
       if(!f)
       {
         g=1;
         break;
       }
     }
     if(!g)
     {
       ans=j;
       break;
     }
   }
   cout<<ans<<endl;
 }
 return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[205],b[205];
int dp[205][1000];

int Call(int i,int sum)
{
    if(i == n+1)return sum;
    if(dp[i][sum] != -1)return dp[i][sum];
    int ans=1e8;
    for(int j=1;j<=m;j++){
        ans=min(ans,Call(i+1,  sum |  (a[i]&b[j])));
    }
    return dp[i][sum]=ans;
}
int main()
{
    memset(dp, -1 ,sizeof dp);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int j=1;j<=m;j++){
        cin>>b[j];
    }
    cout<<Call(1,0);
}


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

int n,m,arr1[207],arr2[207];int dp[600][600],come[600][600];

int Rec(int i,int init)
{
    if(i>n)return init;
    if(come[i][init])return dp[i][init];
    int give=1e9;
    for(int j=1;j<=m;++j)
    {
        give=min(give,Rec(i+1,init|(arr1[i]&arr2[j])));
    }
    come[i][init]=1;

    return dp[i][init]=give;

}
int main()
{
   cin>>n>>m;
    for(int i=1;i<=n;++i)cin>>arr1[i];
    for(int i=1;i<=m;++i)cin>>arr2[i];
   cout<<Rec(1,0);

}


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

int n,m,arr1[207],arr2[207];int dp[600][600];

int Rec(int i,int init)
{
    if(i>n)return init;
    if(dp[i][init]>0)return dp[i][init];
    int give=1e9;
    for(int j=1;j<=m;++j)
    {
        give=min(give,Rec(i+1,init|(arr1[i]&arr2[j])));
    }

    return dp[i][init]=give;

}
int main()
{
   cin>>n>>m;
    for(int i=1;i<=n;++i)cin>>arr1[i];
    for(int i=1;i<=m;++i)cin>>arr2[i];
   cout<<Rec(1,0);

}


#include<bits/stdc++.h>
using namespace std;
int n,m,a[205],b[205];
int dp[205][1000];

int Call(int i,int sum)
{
    if(i == n+1)return sum;
    if(dp[i][sum] != -1)return dp[i][sum];
    int ans=1e8;
    for(int j=1;j<=m;j++){
        ans=min(ans,sum | Call(i+1,  a[i]&b[j]));
    }
    return dp[i][sum]=ans;
}
int main()
{
    memset(dp, -1 ,sizeof dp);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int j=1;j<=m;j++){
        cin>>b[j];
    }
    cout<<Call(1,0);
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

using namespace std;
const ll mod = 1000000007;

ll dp[300][1000];

ll solve(ll i, ll cor, ll a[], ll b[], ll n, ll m){

    if(i == n) return cor;
    if(dp[i][cor] != -1) return dp[i][cor];

    ll mn = 99999;
    for(int j = 0;j < m;j++){
        ll c = cor|(a[i]&b[j]);
        mn = min(mn, solve(i+1,c,a,b,n,m));
    }

    return dp[i][cor] = mn;
}

int main() {

    fastio

    ll n,m; cin >> n >> m;
    ll a[n];
    ll b[n];
    memset(dp,-1,sizeof(dp));

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

    ll ans = solve(0,0,a,b,n,m);

    cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

using namespace std;
const ll mod = 1000000007;

int main() {

    fastio

    ll n,m; cin >> n >> m;
    ll a[n];
    ll b[n];

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

    for(int k = 0;k < (1<<9);k++){
        bool pos = true;
        for(int i = 0;i < n;i++){
            bool p = false;
            for(int j = 0;j < m;j++){
                if(((a[i]&b[j])|k) == k){
                    p = true;
                    break;
                }
            }
            pos = pos&p;
        }
        if(pos){
            cout << k << "\n";
            break;
        }
    }

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

using namespace std;
const ll mod = 1000000007;

ll dp[300][10000];

ll solve(ll i, ll cor, ll a[], ll b[], ll n, ll m){

    if(i == n) return cor;
    if(dp[i][cor] != -1) return dp[i][cor];

    ll mn = 99999;
    for(int j = 0;j < m;j++){
        ll c = cor|(a[i]&b[j]);
        mn = min(mn, solve(i+1,c,a,b,n,m));
    }

    return dp[i][cor] = mn;
}

int main() {

    fastio

    ll n,m; cin >> n >> m;
    ll a[n];
    ll b[n];
    memset(dp,-1,sizeof(dp));

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

    ll ans = solve(0,0,a,b,n,m);

    cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

using namespace std;
const ll mod = 1000000007;

ll dp[300];

ll solve(ll i, ll cor, ll a[], ll b[], ll n, ll m){

    if(i == n) return cor;

    ll mn = 99999;
    for(int j = 0;j < m;j++){
        ll c = cor|(a[i]&b[j]);
        mn = min(mn, solve(i+1,c,a,b,n,m));
    }

    return mn;
}

int main() {

    fastio

    ll n,m; cin >> n >> m;
    ll a[n];
    ll b[n];
    memset(dp,-1,sizeof(dp));
    for(int i = 0;i < n;i++) cin >> a[i];
    for(int i = 0;i < m;i++) cin >> b[i];

    ll ans = solve(0,0,a,b,n,m);

    cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

using namespace std;
const ll mod = 1000000007;

ll dp[300][10000];

ll solve(ll i, ll cor, ll a[], ll b[], ll n, ll m){

    if(i == n) return cor;
    if(dp[i][cor] != -1) return dp[i][cor];

    ll mn = 99999;
    for(int j = 0;j < m;j++){
        ll c = cor|(a[i]&b[j]);
        mn = min(mn, solve(i+1,c,a,b,n,m));
    }

    return dp[i][cor] = mn;
}

int main() {

    fastio

    ll n,m; cin >> n >> m;
    ll a[n];
    ll b[m];
    memset(dp,-1,sizeof(dp));

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

    ll ans = solve(0,0,a,b,n,m);

    cout << ans << "\n";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
//
//
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ll long long
#define ld long double
#define gap ' '
#define endl '\n'

int n,m;
int c[205][205];
int dp[205],a[205],b[205];
int f(int i,int Or) {
	if(i==n+1) return Or;
	int a=LLONG_MAX;
	for(int j=1;j<=m;j++)
	a=min(a,f(i+1,Or|c[i][j]));
	return a;
}
void solve() {
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=m;i++) cin>>b[i];
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	c[i][j]=a[i]&b[j];
	cout<<f(1,0)<<endl;
}
void solve(bool testcase) {
	int t;cin>>t;while(t--)solve();
}
int32_t main() {
	fio
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
//
//
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ll long long
#define ld long double
#define gap ' '
#define endl '\n'

int n,m;
int c[200][200];

int f(int i) {
	if(i>=n) return 0;
	int a=LLONG_MAX;
	for(int j=0;j<m;j++)
	a=min(a,f(i+1)|c[i][j]);
	return a;
}
void solve() {
	cin>>n>>m;
	vector<int> a(n),b(m);
	for(int &x:a) cin>>x;
	for(int &x:b) cin>>x;
	for(int i=0;i<n;i++)
	for(int j=0;j<m;j++)
	c[i][j]=a[i]&b[j];
	//cout<<f(0)<<endl;
	int ans=0;
	for(int i=0;i<n;i++)
	ans|=*min_element(c[i],c[i]+m);
	cout<<ans<<endl;
}
void solve(bool testcase) {
	int t;cin>>t;while(t--)solve();
}
int32_t main() {
	fio
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
//
//
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ll long long
#define ld long double
#define gap ' '
#define endl '\n'

int n,m;
int c[205][205];
int dp[205][1000],a[205],b[205];
int f(int i,int Or) {
	if(i==n+1) return Or;
	if(dp[i][Or]!=-1) return dp[i][Or];
	int a=LLONG_MAX;
	for(int j=1;j<=m;j++)
	a=min(a,f(i+1,Or|c[i][j]));
	return dp[i][Or]=a;
}
void solve() {
	memset(dp,-1,sizeof(dp));
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=m;i++) cin>>b[i];
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	c[i][j]=a[i]&b[j];
	cout<<f(1,0)<<endl;
}
void solve(bool testcase) {
	int t;cin>>t;while(t--)solve();
}
int32_t main() {
	fio
	solve();
	return 0;
}


    #include <bits/stdc++.h>

    #define lli long long int
    #define ld long double
    #define endl "\n"
    #define forn(i, in, fin) for(int i = in; i<fin; i++)
    #define all(v) v.begin(), v.end()
    #define fastIO(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    using namespace std;

    bool dp[202][1026];

    int main () {
    	fastIO();
        lli n, m; cin>>n>>m;

        vector< lli > a(n+1), b(m+1);
        for(int i = 1; i<=n; i++) cin>>a[i];
        for(int i = 1; i<=m; i++) cin>>b[i];

        //dp[i][mask] can i reach to "mask" processing i numbers
        dp[0][0] = true; // If you dont process any number, then you can get 0 as answer.
        for(int i = 1; i<=n; i++)
        {
            for(int j = 1; j<=m; j++)
            {
                for(int mask = 0; mask<(1<<9); mask++)
                {
                    if(dp[i-1][mask] == 1)
                    {
                        dp[i][((b[j] & a[i]) | mask)] = 1; // If mask is reachable having been proccesed i-1 numbers
                                                           // then ((b[j] & a[i]) | mask) is also reachable but processing i numbers
                    }
                }
            }
        }

        for(int mask = 0; mask<(1<<9); mask++)
        {
            if(dp[n][ mask ]) // The first that i find is the minimum
            {
                cout << mask << endl;
                return 0;
            }
        }

    	return 0;
    }


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cerr<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define foo(i,x,n) for(int i=x;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define w(x) int x;cin>>x;while(x--)
#define display(a,n) {fo(i,n) {cout<<a[i]<<" ";}cout<<endl;}
#define fil(a,n,val) {fo(i,n) a[i]=val; }
#define sp(x,y) cout<<fixed<<setprecision(y)<<x<<endl;
#define int ll
const ll MOD   = 1000*1000*1000+7;
const ll MOD2  = 998244353;
const ll N = 1000*1000+5;
const double PI  = 3.14159265;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
ll search(vl arr, ll l, ll r, ll x){if (r >= l) {ll mid = l + (r - l) / 2;if (arr[mid] == x)return mid;if (arr[mid] > x)return search(arr, l, mid - 1, x);return search(arr, mid + 1, r, x);} return -1;}
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll lcm(ll a , ll b){return (a*b)/ gcd(a,b);}
ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1;x = (x*x) % p; } return res; }
ll modInverse(ll n, ll p){return power(n, p-2, p);}
const int maxn=3e5+5;

signed main(){
	fastio();
		#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
    	#else
    	#endif
		int n,m;
		cin>>n>>m;
		vi a(n),b(m);
		fo(i,n) cin>>a[i];
		fo(i,m) cin>>b[i];
		int ans=(1LL<<32)-1;
		for(int i=31;i>=0;i--){
			int tmp=ans^(1LL<<i);
			int f1=0,f2=1;
			fo(i,n){
				f1=0;
				fo(j,m){
					if(((a[i]&b[j])|tmp)==tmp){
						f1=1;
						break;
					}
				}
				if(f1==0) f2=0;
			}
			if(f2){
				ans^=(1LL<<i);
			}
		}
		cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cerr<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define foo(i,x,n) for(int i=x;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define w(x) int x;cin>>x;while(x--)
#define display(a,n) {fo(i,n) {cout<<a[i]<<" ";}cout<<endl;}
#define fil(a,n,val) {fo(i,n) a[i]=val; }
#define sp(x,y) cout<<fixed<<setprecision(y)<<x<<endl;
#define int ll
const ll MOD   = 1000*1000*1000+7;
const ll MOD2  = 998244353;
const ll N = 1000*1000+5;
const double PI  = 3.14159265;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
ll search(vl arr, ll l, ll r, ll x){if (r >= l) {ll mid = l + (r - l) / 2;if (arr[mid] == x)return mid;if (arr[mid] > x)return search(arr, l, mid - 1, x);return search(arr, mid + 1, r, x);} return -1;}
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll lcm(ll a , ll b){return (a*b)/ gcd(a,b);}
ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1;x = (x*x) % p; } return res; }
ll modInverse(ll n, ll p){return power(n, p-2, p);}
const int maxn=3e5+5;

signed main(){
	fastio();
		#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
    	#else
    	#endif
		int n,m;
		cin>>n>>m;
		vi a(n),b(m);
		fo(i,n) cin>>a[i];
		fo(i,m) cin>>b[i];
		int ans=(1LL<<9)-1;
		for(int i=8;i>=0;i--){
			int tmp=ans^(1LL<<i);
			int f1=0,f2=1;
			fo(i,n){
				f1=0;
				fo(j,m){
					if(((a[i]&b[j])|tmp)==tmp){
						f1=1;
						break;
					}
				}
				if(f1==0) f2=0;
			}
			if(f2){
				ans^=(1LL<<i);
			}
		}
		cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cerr<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define foo(i,x,n) for(int i=x;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define w(x) int x;cin>>x;while(x--)
#define display(a,n) {fo(i,n) {cout<<a[i]<<" ";}cout<<endl;}
#define fil(a,n,val) {fo(i,n) a[i]=val; }
#define sp(x,y) cout<<fixed<<setprecision(y)<<x<<endl;
#define int ll
const ll MOD   = 1000*1000*1000+7;
const ll MOD2  = 998244353;
const ll N = 1000*1000+5;
const double PI  = 3.14159265;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
ll search(vl arr, ll l, ll r, ll x){if (r >= l) {ll mid = l + (r - l) / 2;if (arr[mid] == x)return mid;if (arr[mid] > x)return search(arr, l, mid - 1, x);return search(arr, mid + 1, r, x);} return -1;}
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll lcm(ll a , ll b){return (a*b)/ gcd(a,b);}
ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1;x = (x*x) % p; } return res; }
ll modInverse(ll n, ll p){return power(n, p-2, p);}
const int maxn=3e5+5;

signed main(){
	fastio();
	freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
		int n,m;
		cin>>n>>m;
		vi a(n),b(m);
		fo(i,n) cin>>a[i];
		fo(i,m) cin>>b[i];
		int ans=(1LL<<9)-1;
		for(int i=8;i>=0;i--){
			int tmp=ans^(1LL<<i);
			int f1=0,f2=1;
			fo(i,n){
				f1=0;
				fo(j,m){
					if(((a[i]&b[j])|tmp)==tmp){
						f1=1;
						break;
					}
				}
				if(f1==0) f2=0;
			}
			if(f2){
				ans^=(1LL<<i);
			}
		}
		cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cerr<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define foo(i,x,n) for(int i=x;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define w(x) int x;cin>>x;while(x--)
#define display(a,n) {fo(i,n) {cout<<a[i]<<" ";}cout<<endl;}
#define fil(a,n,val) {fo(i,n) a[i]=val; }
#define sp(x,y) cout<<fixed<<setprecision(y)<<x<<endl;
#define int ll
const ll MOD   = 1000*1000*1000+7;
const ll MOD2  = 998244353;
const ll N = 1000*1000+5;
const double PI  = 3.14159265;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
ll search(vl arr, ll l, ll r, ll x){if (r >= l) {ll mid = l + (r - l) / 2;if (arr[mid] == x)return mid;if (arr[mid] > x)return search(arr, l, mid - 1, x);return search(arr, mid + 1, r, x);} return -1;}
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll lcm(ll a , ll b){return (a*b)/ gcd(a,b);}
ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1;x = (x*x) % p; } return res; }
ll modInverse(ll n, ll p){return power(n, p-2, p);}
const int maxn=3e5+5;

signed main(){
	fastio();
		int n,m;
		cin>>n>>m;
		vi a(n),b(m);
		fo(i,n) cin>>a[i];
		fo(i,m) cin>>b[i];
		sort(a.rbegin(), a.rend());
		sort(b.rbegin(), b.rend());
		// int mn=*min_element(all(b));

		// // fo(i,n){
		// // 	fo(j,m){
		// // 		c[i]=min(c[i],a[i]&b[j]);
		// // 	}
		// // }

		// // int ans=c[0];
		// // cout<<c[0]<<" ";
		// // foo(i,1,n){
		// // 	cout<<c[i]<<" ";
		// // 	ans|=c[i];
		// // }

		int freq[32]={0};
		fo(i,n){
			vi c=b;
			for(int j=31;j>=0;j--){
				if((a[i]&(1LL<<j))&&freq[j]==0){
					int flag=0;
					vi d=c;
					for(int k=0;k<m;k++){
						if(c[k]==-1) continue;
						if((c[k]&(1LL<<j))){
							c[k]=-1;
						}
					}
					int cnt=0;
					fo(k,m){
						// cout<<c[k]<<" ";
						if(c[k]!=-1) cnt++;
					}
					// cout<<endl;
					if(cnt==0){
						freq[j]=1;
						// cout<<"j "<<j<<endl;
						c=d;
					}
				}
			}
		}

		int ans=0;
		fo(j,31){
			if(freq[j]) ans+=(1LL<<j);
		}
		cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cerr<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define foo(i,x,n) for(int i=x;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define w(x) int x;cin>>x;while(x--)
#define display(a,n) {fo(i,n) {cout<<a[i]<<" ";}cout<<endl;}
#define fil(a,n,val) {fo(i,n) a[i]=val; }
#define sp(x,y) cout<<fixed<<setprecision(y)<<x<<endl;
#define int ll
const ll MOD   = 1000*1000*1000+7;
const ll MOD2  = 998244353;
const ll N = 1000*1000+5;
const double PI  = 3.14159265;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
ll search(vl arr, ll l, ll r, ll x){if (r >= l) {ll mid = l + (r - l) / 2;if (arr[mid] == x)return mid;if (arr[mid] > x)return search(arr, l, mid - 1, x);return search(arr, mid + 1, r, x);} return -1;}
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll lcm(ll a , ll b){return (a*b)/ gcd(a,b);}
ll power(ll x, ll y, ll p){ll res = 1;x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1;x = (x*x) % p; } return res; }
ll modInverse(ll n, ll p){return power(n, p-2, p);}
const int maxn=3e5+5;

signed main(){
	fastio();
	// freopen("input.txt", "r", stdin);
 //    freopen("output.txt", "w", stdout);
		int n,m;
		cin>>n>>m;
		vi a(n),b(m);
		fo(i,n) cin>>a[i];
		fo(i,m) cin>>b[i];
		int ans=(1LL<<9)-1;
		for(int i=8;i>=0;i--){
			int tmp=ans^(1LL<<i);
			int f1=0,f2=1;
			fo(i,n){
				f1=0;
				fo(j,m){
					if(((a[i]&b[j])|tmp)==tmp){
						f1=1;
						break;
					}
				}
				if(f1==0) f2=0;
			}
			if(f2){
				ans^=(1LL<<i);
			}
		}
		cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pi 3.1415926536
#define pb push_back
#define ff first
#define ss second
#define N 1000000007
#define INF (1ll<<60) - 1
#define M 998244353
#define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define nrep(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define pl pair<ll,ll>
#define pll pair< ll, pair<ll,ll> >
int dp[105][512];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll i,t,x,y,n,z,l=0,j,r,m;
    cin>>n>>m;
    ll a[n+5],b[m+5];
    rep(i,1,n) cin>>a[i];
    rep(i,1,m) cin>>b[i];
    rep(i,1,m){
    	dp[1][a[1]&b[i]] = 1;
	}
	rep(i,2,n){
		rep(k,0,511){
			rep(j,1,m){
				if(dp[i-1][k]) dp[i][k|(a[i]&b[j])] =1;
			}
		}
	}
	rep(i,0,511){
		if(dp[n][i]){
			cout<<i;
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pi 3.1415926536
#define pb push_back
#define ff first
#define ss second
#define N 1000000007
#define INF (1ll<<60) - 1
#define M 998244353
#define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define nrep(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define pl pair<ll,ll>
#define pll pair< ll, pair<ll,ll> >
int dp[105][512];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int i,t,x,y,n,z,l=0,j,r,m;
    cin>>n>>m;
    int a[n+5],b[m+5];
    rep(i,1,n) cin>>a[i];
    rep(i,1,m) cin>>b[i];
    rep(i,1,m){
    	dp[1][a[1]&b[i]] = 1;
	}
	rep(i,2,n){
		rep(k,0,511){
			rep(j,1,m){
				if(dp[i-1][k]) dp[i][k|(a[i]&b[j])] =1;
			}
		}
	}
	rep(i,0,511){
		if(dp[n][i]){
			cout<<i;
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pi 3.1415926536
#define pb push_back
#define ff first
#define ss second
#define N 1000000007
#define INF (1ll<<60) - 1
#define M 998244353
#define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define nrep(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define pl pair<ll,ll>
#define pll pair< ll, pair<ll,ll> >
int dp[105][1024];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll i,t,x,y,n,z,l=0,j,r,m;
    cin>>n>>m;
    ll a[n+5],b[m+5];
    rep(i,1,n) cin>>a[i];
    rep(i,1,m) cin>>b[i];
    rep(i,1,m){
    	dp[1][a[1]&b[i]] = 1;
	}
	rep(i,2,n){
		rep(k,0,1023){
			rep(j,1,m){
				if(dp[i-1][k]) dp[i][k|(a[i]&b[j])] =1;
			}
		}
	}
	rep(i,0,511){
		if(dp[n][i]){
			cout<<i;
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pi 3.1415926536
#define pb push_back
#define ff first
#define ss second
#define N 1000000007
#define INF (1ll<<60) - 1
#define M 998244353
#define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define nrep(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define pl pair<ll,ll>
#define pll pair< ll, pair<ll,ll> >
ll dp[205][512];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll i,t,x,y,n,z,l=0,j,r,m;
    cin>>n>>m;
    ll a[n+5],b[m+5];
    rep(i,1,n) cin>>a[i];
    rep(i,1,m) cin>>b[i];
    rep(i,1,m){
    	dp[1][a[1]&b[i]] = 1;
	}
	rep(i,2,n){
		rep(k,0,511){
			rep(j,1,m){
				if(dp[i-1][k]) dp[i][k|(a[i]&b[j])] =1;
			}
		}
	}
	rep(i,0,511){
		if(dp[n][i]){
			cout<<i;
			break;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
const int MAAX= 0x200;
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int> a(n),b(m);
    for (int &i:a) cin>>i;
    for (int &i:b) cin>>i;
    int c[MAAX+10];
    memset(c,1000000,sizeof(c));
    int res=0;
    for (int i=0;i<n;++i)
    {
        for (int j=0;j< m;++j)
            c[i]=min(c[i],(a[i]&b[j]) );
        //res=max(res,c[i]);
    }

    for (int i=0;i<n;++i) res |= c[i];
    cout<<res;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAAX= 0x200;
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int> a(n),b(m);
    for (int &i:a) cin>>i;
    for (int &i:b) cin>>i;
    int c[MAAX+10];
    for (int i=0;i<n;++i)
    {
        for (int j=0;j<m;++j)
            c[i]=min(c[i],(a[i]&b[j]));
    }
    int res=c[0];
    for (int i=1;i<n;++i) res|= c[i];
    cout<<res;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAAX= 0x200;
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int> a(n),b(m);
    for (int &i:a) cin>>i;
    for (int &i:b) cin>>i;
    int c[MAAX+10];
    memset(c,1000000,sizeof(c));
    int res=0;
    for (int i=0;i<n;++i)
    {
        for (int j=0;j< m;++j)
            c[i]=min(c[i],(a[i]&b[j]));
        //res=max(res,c[i]);
    }

    for (int i=1;i<n;++i) res |= c[i];
    cout<<res;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAAX= 0x200;
int main()
{
    //freopen("BO.inp","r",stdin);
    int n,m;
    cin>>n>>m;
    vector<int> a(n),b(m);
    for (int &i:a) cin>>i;
    for (int &i:b) cin>>i;
    int c[MAAX+10];
    memset(c,1000000,sizeof(c));
    int res=0;

    for (int bit=0;bit<512;++bit)
    {
        int Count=0;
        for (int i=0;i<n;++i)
        {
            bool flag=0;
            for (int j=0;j< m;++j){
                int x= a[i]&b[j];
                if (bit==(x|bit)) flag=1;
            }
            if (flag==1) Count++;
        }
        if (Count==n) {cout<<bit;break;}
        //res=max(res,c[i]);
    }
    //for (int i=0;i<n;++i) cout<< c[i]<<" ";
    //cout<<res;
    return 0;
}


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

int main()
{
    int n,m;
    cin >> n >> m;
    vector<int> st_a(n),st_b(m);
    for(int i = 0;i<n;++i) cin >> st_a[i];
    for(int i = 0;i<m;++i) cin >> st_b[i];
    int x = pow(2,10);
    for(int i = 0;i<x;++i)
    {
        int cnt = 0;
        for(int j = 0;j<n;++j)
        {
            for(int k = 0;k<m;++k)
            {
                if((st_a[j]&st_b[k])|i == i){
                    cnt+=1;
                    break;
                }
            }
        }
        if(cnt == n)
        {
            cout << n << endl;
            break;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){int n,m; cin >> n >> m;vector<int> st_a(n),st_b(m);for(int i = 0;i<n;++i) cin >> st_a[i];for(int i = 0;i<m;++i) cin >> st_b[i];for(int i = 0;i<512;++i){int total = 0;for(int j = 0;j<n;++j){for(int k = 0;k<m;++k){if(((st_a[j]&st_b[k])|i) == i){total += 1;break;}}}if(total == n){cout << i; break;}}return 0;}


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

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    vector<int> st_a(n);
    vector<int> st_b(m);
    for(int i = 0;i<n;++i) cin >> st_a[i];
    for(int i = 0;i<m;++i) cin >> st_b[i];
    bool found = false;
    for(int i = 0;i<(1<<10);++i)
    {
        int cnt = 0;
        for(int j = 0;j<(1<<10);++j)
        {
            if(i|j == i) cnt++;
        }
        if(cnt >= max(n,m)){
            cout << i << endl;
            break;
        }
    }
}


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

int main()
{
    int n,m;
    cin >> n >> m;
    vector<int> st_a(n),st_b(m);
    for(int i = 0;i<n;++i) cin >> st_a[i];
    for(int i = 0;i<m;++i) cin >> st_b[i];
    int x = pow(2,10);
    for(int i = 0;i<512;++i)
    {
        int total = 0;
        for(int j = 0;j<n;++j)
        {
            int cnt = 0;
            for(int k = 0;k<m;++k)
            {
                if(((st_a[j]&st_b[k])|i) == i){
                    cnt = 1;
                    break;
                }
            }
            if(cnt == 1)
            total += 1;
        }
        if(total == n)
        {
            cout << i;
            break;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin >> n >> m;
    vector<int> st_a(n),st_b(m);
    for(int i = 0;i<n;++i) cin >> st_a[i];
    for(int i = 0;i<m;++i) cin >> st_b[i];
    for(int i = 0;i<512;++i)
    {
        int total = 0;
        for(int j = 0;j<n;++j)
        {
            for(int k = 0;k<m;++k)
            {
                if(((st_a[j]&st_b[k])|i) == i){
                    total += 1;
                    break;
                }
            }
        }
        if(total == n)
        {
            cout << i;
            break;
        }
    }
    return 0;
}


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

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    vector<int> st_a(n);
    vector<int> st_b(m);
    for(int i = 0;i<n;++i) cin >> st_a[i];
    for(int i = 0;i<m;++i) cin >> st_b[i];
    bool found = false;
    for(int i = 0;i<(1<<10);++i)
    {
        int cnt = 0;
        for(int j = 0;j<(1<<10);++j)
        {
            if(i|j == i) cnt++;
        }
        if(cnt >= max(n,m)){
            cout << i << endl;
            break;
        }
    }
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
int gcd(int A,int B) {if(!B)return A;return gcd(B,A%B);}
int main()
{
	owo
	int n,m;
	cin>>n>>m;
	vector<ll>a(n),b(m);
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++)cin>>b[i];
	ll ans = 0;
	sort(all(a),greater<ll>());
	sort(all(b));
	for(int i=0;i<n;i++){
		ll mn = 1e9;
		for(int j=0;j<m;j++){
			ll c = a[i]&b[j];
			for(int k=0;k<=9;k++){
				if((ans&(1<<k)) && (c&(1<<k))){
					c-=1<<k;
				}
			}
			mn = min(mn,c);
		}
		//cout<<mn<<'\n';
		for(int k=0;k<=9;k++){
			if((mn&(1<<k)))ans|=(1<<k);
		}
	}
	cout<<ans;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
int gcd(int A,int B) {if(!B)return A;return gcd(B,A%B);}
int main()
{
	owo
	int n,m;
	cin>>n>>m;
	vector<ll>a(n),b(m);
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++)cin>>b[i];
	ll ans = 0;
	sort(all(a),greater<ll>());
	for(int i=0;i<n;i++){
		ll mn = 1e9;
		for(int j=0;j<m;j++){
			ll c = a[i]&b[j];
			for(int k=0;k<=9;k++){
				if((ans&(1<<k)) && (c&(1<<k))){
					c-=1<<k;
				}
			}
			mn = min(mn,c);
		}
		//cout<<mn<<'\n';
		for(int k=0;k<=9;k++){
			if((mn&(1<<k)))ans|=(1<<k);
		}
	}
	cout<<ans;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
int gcd(int A,int B) {if(!B)return A;return gcd(B,A%B);}
int main()
{
	owo
	int n,m;
	cin>>n>>m;
	vector<int>a(n),b(m);
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++)cin>>b[i];
	int ans = 0;
	sort(all(a),greater<int>());
	for(int i=0;i<n;i++){
		int mn = 1e9;
		for(int j=0;j<m;j++){
			int c = a[i]&b[j];
			for(int k=0;k<=8;k++){
				if((ans&(1<<k)) && (c&(1<<k))){
					c-=1<<k;
				}
			}
			mn = min(mn,c);
		}
		//cout<<mn<<'\n';
		for(int k=0;k<=8;k++){
			if((mn&(1<<k)))ans|=(1<<k);
		}
	}
	cout<<ans;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
int gcd(int A,int B) {if(!B)return A;return gcd(B,A%B);}
int main()
{
	owo
	int n,m;
	cin>>n>>m;
	vector<ll>a(n),b(m);
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++)cin>>b[i];
	int ans = 1e9;
	for(int k=0;k<(1<<9);k++){
		bool ok = 1;
		for(int i=0;i<n;i++){
			bool f = 0;
			for(int j=0;j<m;j++){
				int c = a[i]&b[j];
				if((c|k) == k)f = 1;
			}
			if(!f)ok = 0;
		}
		if(ok)ans = min(k,ans);
	}
	cout<<ans;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

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

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

int n,m,a[500],b[500];
signed main(){
    FastIO;
    cin>>n>>m;
    rep(i,1,n) cin>>a[i];
    rep(j,1,m) cin>>b[j];
    rep(i,0,2000){
        int cnt=0;
        rep(j,1,n){
            bool ok=0;
            rep(k,1,m){
                int c=a[j]&b[k],f=1;
                rep(l,0,9){
                    if((1&(c>>l)) and !(1&(i>>l))) f=0;
                }
                ok|=f;
            }
            cnt+=ok;
        }
        if(cnt==n) return cout<<i,0;
    }
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

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

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

int n,m,a[205],b[205],dp[205][1050];
int rec(int i,int j){
    if(i>n) return j;
    int &cache=dp[i][j];
    if(cache!=-1) return cache;
    cache=1e9;
    rep(k,1,m) cache=min(cache,rec(i+1,j|(a[i]&b[k])));
    return cache;
}
signed main(){
    FastIO;
    cin>>n>>m;
    rep(i,1,n) cin>>a[i];
    rep(j,1,m) cin>>b[j];
    memset(dp,-1,sizeof dp);
    cout<<rec(1,0);
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

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

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

int n,m,a[205],b[205],dp[205][1050];
int rec(int i,int j){
    if(i>n) return 0;
    int &cache=dp[i][j];
    if(cache!=-1) return cache;
    cache=1e9;
    rep(k,1,m) cache=min(cache,j|rec(i+1,a[i]&b[k]));
    return cache;
}
signed main(){
    FastIO;
    cin>>n>>m;
    rep(i,1,n) cin>>a[i];
    rep(j,1,m) cin>>b[j];
    memset(dp,-1,sizeof dp);
    cout<<rec(1,0);
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

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

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

int n,m,a[205],b[205],dp[205][1050];
int rec(int i,int j){
    if(i>n) return j;
    int &cache=dp[i][j];
    if(cache!=-1) return cache;
    cache=1e9;
    rep(k,1,m) cache=min(cache,j|rec(i+1,a[i]&b[k]));
    return cache;
}
signed main(){
    FastIO;
    cin>>n>>m;
    rep(i,1,n) cin>>a[i];
    rep(j,1,m) cin>>b[j];
    memset(dp,-1,sizeof dp);
    cout<<rec(1,0);
    return 0;
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define _PI acos(-1.0)
#define INF 0x3f3f3f3f
#define esp 1e-9
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define FRE freopen("abc.in", "r", stdin)
#define MEM(X,Y) memset(X,Y,sizeof(X))
typedef long long LL;
using namespace std;
/*===========================================*/

int a[105], b[105];

void solve()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int j = 0; j < m; ++j)
		scanf("%d", &b[j]);
	int t;
	bool flag;
	for (int k = 0; k <= 1000; ++k)
	{
		for (int i = 0; i < n; ++i)
		{
			flag = false;
			for (int j = 0; j < m; ++j)
			{
				t = a[i] & b[j];
				if ((t | k) == k)
				{
					flag = true;
					break;
				}
			}
			if ((t | k) != k)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			printf("%d", k);
			break;
		}
	}
}

int main()
{
	solve();
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define _PI acos(-1.0)
#define INF 0x3f3f3f3f
#define esp 1e-9
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define FRE freopen("abc.in", "r", stdin)
#define MEM(X,Y) memset(X,Y,sizeof(X))
typedef long long LL;
using namespace std;
/*===========================================*/

int a[105], b[105];

void solve()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int j = 0; j < m; ++j)
		scanf("%d", &b[j]);
	int t;
	bool flag;
	for (int k = 0; k <= 1 << 9; ++k)
	{
		flag = true;
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < m; ++j)
			{
				t = a[i] & b[j];
				if ((t | k) == k)
					break;
			}
			if ((t | k) != k)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			printf("%d", k);
			break;
		}
	}
}

int main()
{
	solve();
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define _PI acos(-1.0)
#define INF 0x3f3f3f3f
#define esp 1e-9
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define FRE freopen("abc.in", "r", stdin)
#define MEM(X,Y) memset(X,Y,sizeof(X))
typedef long long LL;
using namespace std;
/*===========================================*/

int a[105], b[105];

void solve()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int j = 0; j < m; ++j)
		scanf("%d", &b[j]);
	int t;
	bool flag;
	for (int k = 0; k <= 1 << 9; ++k)
	{
		for (int i = 0; i < n; ++i)
		{
			flag = false;
			for (int j = 0; j < m; ++j)
			{
				t = a[i] & b[j];
				if ((t | k) == k)
				{
					flag = true;
					break;
				}
			}
			if ((t | k) != k)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			printf("%d", k);
			break;
		}
	}
}

int main()
{
	solve();
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define _PI acos(-1.0)
#define INF 0x3f3f3f3f
#define esp 1e-9
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define FRE freopen("abc.in", "r", stdin)
#define MEM(X,Y) memset(X,Y,sizeof(X))
typedef long long LL;
using namespace std;
/*===========================================*/

int a[205], b[205];

void solve()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int j = 0; j < m; ++j)
		scanf("%d", &b[j]);
	int t;
	bool flag;
	for (int k = 0; k <= 1 << 9; ++k)
	{
		for (int i = 0; i < n; ++i)
		{
			flag = false;
			for (int j = 0; j < m; ++j)
			{
				t = a[i] & b[j];
				if ((t | k) == k)
				{
					flag = true;
					break;
				}
			}
			if ((t | k) != k)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			printf("%d", k);
			break;
		}
	}
}

int main()
{
	solve();
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn];
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	for (int i = 0; i < m; i++) scanf("%d", &b[i]);
	for (int x = 0; x <=1 << 9 - 1; x++) {
		int f = 0;
		for (int i = 0; i < n; i++) {
			int ff = 0;
			for (int j = 0; j < m; j++) {
				if (((a[i] & b[j]) | x) == x) {
					ff = 1;
					break;
				}
			}
			if (ff == 0) {
				f = 1;
				break;
			}
		}
		if (f == 0) {
			printf("%d\n", x);
			break;
		}
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn];
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	for (int j = 0; j < m; j++) scanf("%d", &b[j]);
	for (int x = 0; x < (1 << 9); x++) {
		int f = 0;
		for (int i = 0; i < n; i++) {
			int ff = 0;
			for (int j = 0; j < m; j++) {
				if (((a[i] & b[j]) | x) == x) {
					ff = 1;
					break;
				}
			}
			if (ff == 0)
			{
				f = 1;
				break;
			}
		}
		if (f == 0) {
			printf("%d\n", x);
			return 0;
		}
	}

}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn];
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	for (int j = 0; j < m; j++) scanf("%d", &b[j]);
	for (int x = 0; x < (1 << 9); x++) {
		int f = 0;
		for (int i = 0; i < n; i++) {
			int ff = 0;
			for (int j = 0; j < m; j++) {
				if (((a[i] & b[j]) | x) == x) {
					ff = 1;
					break;
				}
			}
			if (ff == 0)
			{
				f = 1;
				break;
			}
		}
		if (f == 0) {
			printf("%d\n", x);
			return 0;
		}
	}

}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn];
int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	for (int i = 0; i < m; i++) scanf("%d", &b[i]);
	for (int x = 0; x <1 << 9; x++) {
		int f = 0;
		for (int i = 0; i < n; i++) {
			int ff = 0;
			for (int j = 0; j < m; j++) {
				if (((a[i] & b[j]) | x) == x) {
					ff = 1;
					break;
				}
			}
			if (ff == 0) {
				f = 1;
				break;
			}
		}
		if (f == 0) {
			printf("%d\n", x);
			return 0;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
long long int T,n,m,ans,a[1001],b[1001],f[201][201],o[201][201],anss,aa,bb;
int pan(int q)
{
	for(int i=1;i<=n;i++)
	{
		int ok=0;
		for(int j=1;j<=m;j++)
		{
			if((q|(a[i]&b[j]))==q)
			ok=1;
		}
		if(ok==0)
		return 0;
	}
	return 1;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	for(int i=1;i<=m;i++)
	cin>>b[i];
	for(int i=0;i<=512;i++)
	{
		if(pan(i)==1)
		{
			cout<<i;
			return 0;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
long long int T,n,m,ans,a[1001],b[1001],f[201][201],o[201][201],anss,aa,bb;
int pan(int q)
{
	for(int i=1;i<=n;i++)
	{
		int ok=0;
		for(int j=1;j<=m;j++)
		{
			if((q|(a[i]&b[j]))==q)
			ok=1;
		}
		if(ok==0)
		return 0;
	}
	return 1;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	for(int i=1;i<=m;i++)
	cin>>b[i];
	for(int i=1;i<=512;i++)
	{
		if(pan(i)==1)
		{
			cout<<i;
			return 0;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
long long int T,n,m,ans,a[1001],b[1001],f[201][201],o[201][201],anss,aa,bb;
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	for(int i=1;i<=m;i++)
	cin>>b[i];
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		ans=0x7fffffff;
		for(int j=1;j<=m;j++)
		{
			ans=min(ans,anss|(a[i]&b[j]));
		 }
		anss=ans;
	}
	cout<<anss;
}


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

    int n,m;

int a[210], b[210];
int dp[210][1000];

int ans=9999999;

int solve(int x, int mask)
{
    int &ret= dp[x][mask];
    if(dp[x][mask]!=-1) return ret;
    if(x>=n) {
            ans=min(mask,ans);
          //  ret=mask;
        return mask;
    }
    //ret=b[0]&a[x];
    //ret= ret|mask;
    ret=999999;
    for(int i=0; i<m ; i++){
        int u=b[i]&a[x];
       // cout<<ret<<  "     "<<i<<endl;
        ret=min(ret,solve(x+1, u|mask));
    }
return ret;
}

int main(){

    cin>>n>>m;
    for(int i=0; i<n;i++)cin>>a[i];
    for(int i=0; i<m;i++)cin>>b[i];
    memset(dp,-1, sizeof(dp));
    int y=solve(0,0);
    cout<<y<<endl;
}


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

    int n,m;

int a[210], b[210];
int dp[210][550][210];

int ans=9999999;

int solve(int x, int mask ,int y)
{
    int &ret= dp[x][mask][y];
    if(dp[x][mask][y]!=-1) return dp[x][mask][y];
    if(x>=n) {
            ans=min(mask,ans);
        return mask;
    }
    for(int i=0; i<m ; i++){
        int u=b[i]&a[x];
        ret=min(ret,solve(x+1, u|mask,i));
    }

}

int main(){

    cin>>n>>m;
    for(int i=0; i<n;i++)cin>>a[i];
    for(int i=0; i<m;i++)cin>>b[i];
    memset(dp,-1, sizeof(dp));
    int y=solve(0,0,0);
    cout<<ans<<endl;
}


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

    int n,m;

int a[210], b[210];
int dp[210][550];

int ans=9999999;

int solve(int x, int mask)
{
    int &ret= dp[x][mask];
    if(dp[x][mask]!=-1) return dp[x][mask];
    if(x>=n) {
            ans=min(mask,ans);
        return mask;
    }
    for(int i=0; i<m ; i++){
        int u=b[i]&a[x];
        ret=min(ret,solve(x+1, u|mask));
    }

}

int main(){

    cin>>n>>m;
    for(int i=0; i<n;i++)cin>>a[i];
    for(int i=0; i<m;i++)cin>>b[i];
    memset(dp,-1, sizeof(dp));
    int y=solve(0,0);
    cout<<ans<<endl;
}


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

    int n,m;

int a[210], b[210];
int dp[210][1000];

int ans=9999999;

int solve(int x, int mask)
{
    int &ret= dp[x][mask];
    if(dp[x][mask]!=-1) return ret;
    if(x>=n) {
            ans=min(mask,ans);
            ret=mask;
        return mask;
    }
    for(int i=0; i<m ; i++){
        int u=b[i]&a[x];
        ret=min(ret,solve(x+1, u|mask));
    }

}

int main(){

    cin>>n>>m;
    for(int i=0; i<n;i++)cin>>a[i];
    for(int i=0; i<m;i++)cin>>b[i];
    memset(dp,-1, sizeof(dp));
    int y=solve(0,0);
    cout<<ans<<endl;
}


// Problem : C. Boboniu and Bit Operations
// Contest : Codeforces - Codeforces Round #664 (Div. 2)
// URL : https://codeforces.com/contest/1395/problem/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FIO ios_base::sync_with_stdio(0); cin.tie(0);
#define MOD (1000000007LL)
#define inf 1e12
#define ninf -1e12
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define pii pair<int,int>
#define f first
#define s second
const int N = 2*1e5 + 4;
int add(int a, int b, int m){
  if(a < 0) a += m;
  if(b < 0) b += m;
  int res = ((a % m) + (b % m)) % m;
  res %= m;
  return res;
}

int mul(int a, int b, int m){
  if(a < 0) a += m;
  if(b < 0) b += m;
  int res = ((a % m) * (b % m)) % m;
  res %= m;
  return res;
}

int sub(int a, int b, int m){
  int res = (a - b + m) % m;
  res %= m;
  return res;
}

int32_t main(){
	FIO
	int n,m;
	cin >> n >> m;
	vector<int> a(n), b(m);
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < m; i++) cin >> b[i];
	auto check = [&](int x){
		bool flag = true;
		for (int i = 0; i < n; i++){
			bool f = false;
			for (int j = 0; j < m; j++){
				f |= ((x | (a[i]&b[j])) == x);
			}
			flag &= f;
		}
		return flag;
	};
	for (int i = 0; i < 512; i++){
		if(check(i)){
			cout << i;
			break;
		}
	}
	return 0;
}


// Problem : C. Boboniu and Bit Operations
// Contest : Codeforces - Codeforces Round #664 (Div. 2)
// URL : https://codeforces.com/problemset/problem/1395/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FIO ios_base::sync_with_stdio(0); cin.tie(0);
#define MOD (1000000007LL)
#define inf 1e12
#define ninf -1e12
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define pii pair<int,int>
#define f first
#define s second
const int N = 2*1e5 + 4;
int add(int a, int b, int m){
  if(a < 0) a += m;
  if(b < 0) b += m;
  int res = ((a % m) + (b % m)) % m;
  res %= m;
  return res;
}

int mul(int a, int b, int m){
  if(a < 0) a += m;
  if(b < 0) b += m;
  int res = ((a % m) * (b % m)) % m;
  res %= m;
  return res;
}

int sub(int a, int b, int m){
  int res = (a - b + m) % m;
  res %= m;
  return res;
}

int32_t main(){
	FIO
	int n,m;
	cin >> n >> m;
	vector<int> a(n),b(m);
	vector<vector<int>> cache(n,vector<int>(m));
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int j = 0; j < m; j++) cin >> b[j];
	int res  = 0;
	for (int i = 0; i < n; i++){
		int tmp = inf;
		for (int j = 0; j < m; j++){
			cache[i][j] = (a[i]&b[j]);
			tmp = min(tmp,(a[i]&b[j]));
		}
		res = max(tmp,res);
	}
	// cerr << res << endl;
	for (int i = 0; i < n; i++){
		int tmp = inf;
		for (int j = 0; j < m; j++){
			// cerr << i << " " << j << " " << cache[i][j] << " " << tmp << endl;
			tmp = min(tmp,(cache[i][j]|res));
		}
		res = tmp;
	}
	cout << res << endl;
	return 0;
}


// Problem : C. Boboniu and Bit Operations
// Contest : Codeforces - Codeforces Round #664 (Div. 2)
// URL : https://codeforces.com/problemset/problem/1395/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FIO ios_base::sync_with_stdio(0); cin.tie(0);
#define MOD (1000000007LL)
#define inf 1e12
#define ninf -1e12
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define pii pair<int,int>
#define f first
#define s second
const int N = 2*1e5 + 4;
int add(int a, int b, int m){
  if(a < 0) a += m;
  if(b < 0) b += m;
  int res = ((a % m) + (b % m)) % m;
  res %= m;
  return res;
}

int mul(int a, int b, int m){
  if(a < 0) a += m;
  if(b < 0) b += m;
  int res = ((a % m) * (b % m)) % m;
  res %= m;
  return res;
}

int sub(int a, int b, int m){
  int res = (a - b + m) % m;
  res %= m;
  return res;
}

int32_t main(){
	FIO
	int n,m;
	cin >> n >> m;
	vector<int> a(n),b(m);
	vector<vector<int>> cache(n,vector<int>(m));
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int j = 0; j < m; j++) cin >> b[j];
	int res  = 0;
	for (int i = 0; i < n; i++){
		int tmp = inf;
		for (int j = 0; j < m; j++){
			cache[i][j] = (a[i]&b[j]);
			tmp = min(tmp,(a[i]&b[j]));
		}
		res = max(tmp,res);
	}
	// cerr << res << endl;
	for (int i = 0; i < n; i++){
		int tmp = inf;
		for (int j = 0; j < m; j++){
			// cerr << i << " " << j << " " << cache[i][j] << " " << tmp << endl;
			tmp = min(tmp,cache[i][j]|res);
		}
		res = tmp;
	}
	cout << res << endl;
	return 0;
}


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     gs(s)           getline(cin,s)
#define     gc              getchar()
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(x)          cout << #x " = " <<(x) << endl
#define     debb            printf("####\n")
#define     pf              printf
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vs              vector<string>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb              push_back
#define     srt(a)          sort(a.begin(),a.end())
#define     pii             pair <int,int>
#define     pll             pair <ll,ll>
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     fit(x,mp)       for(auto x = mp.begin(); x!=mp.end(); x++)
#define     MAX             100000 //10^6
#define     inf             1000000000 //10^9
#define     mod             1000000007 //10^9

int t, n, m, x, y;
int a[205];
int b[205];
int c[205];
int main()
{
    sii(n,m);
    fl(i,0,n) si(a[i]);
    fl(i,0,m) si(b[i]);

    int ans = inf;
    fl(x,0,m)
    {
        int tmp = a[0]&b[x];
        fl(i,1,n)
        {
            int mn = inf;
            fl(j,0,m)
            {
                mn = min( mn, (tmp|(a[i]&b[j])) );
            }
            tmp = mn;
        }
        ans = min(ans, tmp);
    }
    pi(ans);bl;
    return 0;
}

�


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     gs(s)           getline(cin,s)
#define     gc              getchar()
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(x)          cout << #x " = " <<(x) << endl
#define     debb            printf("####\n")
#define     pf              printf
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vs              vector<string>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb              push_back
#define     srt(a)          sort(a.begin(),a.end())
#define     pii             pair <int,int>
#define     pll             pair <ll,ll>
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     fit(x,mp)       for(auto x = mp.begin(); x!=mp.end(); x++)
#define     MAX             100000 //10^6
#define     inf             1000000000 //10^9
#define     mod             1000000007 //10^9

int t, n, m, x, y;
int a[205];
int b[205];
int c[205];
int main()
{
    sii(n,m);
    fl(i,0,n) si(a[i]);
    fl(i,0,m) si(b[i]);

    int ans = inf;
    fl(x,0,m)
    {
        int tmp = a[0]&b[x];
        fl(i,1,n)
        {
            int mn = inf;
            fl(j,0,m)
            {
                mn = min( mn, (tmp|(a[i]&b[j])) );
            }
            tmp = mn;
        }
        ans = min(ans, tmp);
    }
    pi(ans);bl;
    return 0;
}


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     gs(s)           getline(cin,s)
#define     gc              getchar()
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(x)          cout << #x " = " <<(x) << endl
#define     debb            printf("####\n")
#define     pf              printf
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vs              vector<string>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb              push_back
#define     srt(a)          sort(a.begin(),a.end())
#define     pii             pair <int,int>
#define     pll             pair <ll,ll>
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     fit(x,mp)       for(auto x = mp.begin(); x!=mp.end(); x++)
#define     MAX             100000 //10^6
#define     inf             1000000000 //10^9
#define     mod             1000000007 //10^9
int t, n, m;
int a[205], b[205];
int main ()
{
    sii(n,m);
    fl(i,0,n) si(a[i]);
    fl(i,0,m) si(b[i]);
    fl(mask,1,512)
    {
        int f = 0;
        fl(i,0,n)
        {
            fl(j,0,m)
            {
                if( ((a[i]&b[j])|mask) == mask )
                {
                    f++;
                    break;
                }
            }
        }
        if(f==n)
        {
            pi(mask);
            return 0;
        }
    }
    return 0;
}


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     gs(s)           getline(cin,s)
#define     gc              getchar()
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(x)          cout << #x " = " <<(x) << endl
#define     debb            printf("####\n")
#define     pf              printf
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vs              vector<string>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb              push_back
#define     srt(a)          sort(a.begin(),a.end())
#define     pii             pair <int,int>
#define     pll             pair <ll,ll>
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     fit(x,mp)       for(auto x = mp.begin(); x!=mp.end(); x++)
#define     MAX             100000 //10^6
#define     inf             1000000000 //10^9
#define     mod             1000000007 //10^9
int t, n, m;
int a[205], b[205];
int main ()
{
    sii(n,m);
    fl(i,0,n) si(a[i]);
    fl(i,0,m) si(b[i]);
    fl(mask,0,512)
    {
        int f = 0;
        fl(i,0,n)
        {
            fl(j,0,m)
            {
                if( ((a[i]&b[j])|mask) == mask )
                {
                    f++;
                    break;
                }
            }
        }
        if(f==n)
        {
            pi(mask);
            return 0;
        }
    }
    return 0;
}


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

#define     fRead           freopen("INPUT.txt","r",stdin)
#define     fWrite          freopen("OUTPUT.txt","w",stdout)
#define     ss(a)           scanf("%s",a)
#define     gs(s)           getline(cin,s)
#define     gc              getchar()
#define     si(a)           scanf("%d",&a)
#define     sii(a,b)        scanf("%d%d",&a,&b)
#define     siii(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define     sl(a)           scanf("%lld",&a)
#define     sll(a,b)        scanf("%lld%lld",&a,&b)
#define     slll(a,b,c)     scanf("%lld%lld%lld",&a,&b,&c)
#define     sd(a)           scanf("%lf",&a)
#define     sdd(a,b)        scanf("%lf%lf",&a,&b)
#define     sddd(a,b,c)     scanf("%lf%lf%lf",&a,&b,&c)
#define     deb(x)          cout << #x " = " <<(x) << endl
#define     debb            printf("####\n")
#define     pf              printf
#define     pi(a)           printf("%d",a)
#define     ps(a)           cout<<a
#define     pl(a)           printf("%lld",a)
#define     pd(a)           printf("%f",a)
#define     pc(a)           printf("%c",a)
#define     bl              printf("\n")
#define     spc             printf(" ")
#define     ll              long long
#define     llu             unsigned long long
#define     pie             2*acos(0.0)
#define     eps             1e-9
#define     qi              queue<int>
#define     vi              vector<int>
#define     vs              vector<string>
#define     vd              vector<double>
#define     vl              vector<long long>
#define     pb              push_back
#define     srt(a)          sort(a.begin(),a.end())
#define     pii             pair <int,int>
#define     pll             pair <ll,ll>
#define     mem(a,b)        memset(a,b,sizeof(a))
#define     fl(i,a,n)       for(int i=a; i<n; i++)
#define     rfl(i,n,a)      for(int i=n-1; i>=a; i--)
#define     fit(x,mp)       for(auto x = mp.begin(); x!=mp.end(); x++)
#define     MAX             100000 //10^6
#define     inf             1000000000 //10^9
#define     mod             1000000007 //10^9

int t, n, m, x, y;
int a[105];
int b[105];
int c[105];
int main()
{
    sii(n,m);
    fl(i,0,n) si(a[i]);
    fl(i,0,m) si(b[i]);

    int ans = inf;
    fl(x,0,m)
    {
        int tmp = a[0]&b[x];
        fl(i,1,n)
        {
            int mn = inf;
            fl(j,0,m)
            {
                mn = min( mn, (tmp|(a[i]&b[j])) );
            }
            tmp = mn;
        }
        ans = min(ans, tmp);
    }
    pi(ans);bl;
    return 0;
}


#include<bits/stdc++.h>
// #define M 100001
#define MAX 1000000010
#define mod 1000000007
#define ss second
#define ff first
#define ll long long int
#define pb push_back
#define ld double
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;

ll M = 100000000000000000;
ll wt[1000][1000];
bool check = false,w[1000][1000];

void diakstra(vector<pair<ll,ll> >adj[],ll root,ll dis[],ll n)
{
    fill(dis,dis+n,M);
    dis[root] = 0;
    set<pair<ll,ll> >s;
    for(int i=0;i<n;i++)
    {
        if(i == root)
        {
            s.insert({0,i});
        }
        else
        {
            s.insert({M,i});
        }
    }
    while(!s.empty())
    {
        auto it = s.begin();
        pair<ll,ll> p = (*it);
        s.erase(it);
        for(pair<ll,ll> child:adj[p.second])
        {
            if(s.find({dis[child.second],child.second}) != s.end())
            {
                if(dis[p.second] + child.first < dis[child.second])
                {
                    s.erase(s.find({dis[child.second],child.second}));
                    dis[child.second] = dis[p.second] + child.first;
                    s.insert({dis[child.second],child.second});
                }
            }
        }
    }
}

void display(int n)
{
    cout<<"YES\n";
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(wt[i][j] != 0)
            {
                cout<<i<<" "<<j<<" "<<wt[i][j]<<endl;
            }
        }
    }

}

int main() {
    fast;
    ll n,m,l,s,t;
    cin>>n>>m>>l>>s>>t;
    vector<pair<ll,ll> > adj[n];
    vector<pair<ll,ll> >zero;
    for(int i=0;i<m;i++)
    {
        ll a,b,w1;
        cin>>a>>b>>w1;
        if(w1 != 0){
            wt[a][b] = wt[b][a] = w1;
            adj[a].pb({w1,b});
            adj[b].pb({w1,a});
        }
        else{
            wt[a][b] = wt[b][a] = 0;
            zero.pb({a,b});
            // adj[a].pb({1ll,b});
            // adj[b].pb({1ll,a});
        }
    }
    ll dis[n];
    diakstra(adj,s,dis,n);
    // cout<<dis[t]<<" "<<check<<" "<<nopath<<endl;
    // for(int i=0;i<n;i++)
    //     cout<<dis[i]<<" ";
    // cout<<endl;
    if(dis[t] < l)
    {
        cout<<"NO\n";
        return 0;
    }

    for(pair<ll,ll>p:zero)
    {
        ll u = p.ff,v = p.ss;
        adj[u].pb({1,v});
        adj[v].pb({1,u});
        wt[u][v] = 1;
        wt[v][u] = 1;
        diakstra(adj,s,dis,n);
        ll dif = l-dis[t];
        if(dif > 0)
        {
            adj[u].pop_back();
            adj[v].pop_back();
            adj[u].pb({dif+1,v});
            adj[v].pb({dif+1,u});
            wt[u][v] = dif+1;
            wt[v][u] = dif+1;
        }
    }
    diakstra(adj,s,dis,n);
    if(dis[t] > l)
    {
        cout<<"NO\n";
        return 0;
    }
    display(n);

	return 0;
}