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

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

    long long n, k, qanak=1, bajanac=1, sum, max_sum=-1, hima_s, naxord_s, hima_a, naxord_a=0;
    cin>>n>>k;
    vector <int> a(n);
    vector <bool> b(n-1);
    vector <int> c(k+1);        // tex
    vector <int> d(k);
    for (int i=0; i<k-1; ++i) {
        b[n-2-i] = 1;
    }
    for (int i = 0; i < n; i++) {
        cin>>a[i];
    }
    for (int i = n-1; i>n-k; i--) {
        qanak*=i;
        bajanac*=n-i;
    }
    qanak=qanak/bajanac;
//___________________________________________________________________________________________________________ esqany perfect
    for (int i=0; i<qanak; i++) {
        for (int j=1; j<k; ++j) {
            c[j]=0;
        }
        for (int j=0; j<k; ++j) {
            d[j]=0;
        }
        c[0]=0;
        c[k]=n;
        int l=1;
        for (int j=0; j<n; ++j) {
            if (b[j]==1) {
                c[l]=j+1;
                l++;
            }
        }
        for (int j=0; j<k; ++j) {
            for (int o=c[j]; o<c[j+1]; ++o) {
                d[j]= d[j] | a[o];
            }
        }
        sum=d[0];
        for (int j=1; j<k; ++j) {
            sum=sum & d[j];
        }
        if (sum>max_sum) {
            max_sum=sum;
        }
        next_permutation(b.begin(), b.end());
    }
    cout<<max_sum;


}
