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

bool cmp(pair<int, int> x, pair<int, int> y)
{
    if(x.second == y.second)
    {
        return x.first > y.first;
    }
    return x.second < y.second;
}

int main()
{
    int n, k;
    cin >> n >> k;

    if(n > 20)
    {
        vector<vector<int>> bits(30);
        
        int number = 0;
        vector<int> current_ends{};

        for(int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            for(int bit = 0; (1 << bit) <= x; bit++)
            {
                if((x & (1 << bit)))
                {
                    bits[bit].emplace_back(i);
                }
            }
        }

        for(int bit = 0; bit < 30; bit++)
        {
            if(!bits[bit].empty() && bits[bit].size() >= k)
            {
                // cout << "\nbit " << bit << ": ";
                // for(auto x : bits[bit])
                // {
                //     cout << x << " ";
                // }
                if(current_ends.empty()) // we have no history
                {
                    number = (1 << bit);
                    current_ends = bits[bit];
                }
                else
                {
                    vector<pair<int, int>> ends{};
                    for(auto it = current_ends.begin(); it != current_ends.end(); it++)
                    {
                        auto closest_ones = upper_bound(bits[bit].begin(), bits[bit].end(), *it);
                        if(closest_ones == bits[bit].begin())
                        {
                            continue;
                        }
                        closest_ones--;
                        ends.emplace_back(*closest_ones, *it);
                    }
                    for(auto it = bits[bit].begin(); it != bits[bit].end(); it++)
                    {
                        auto closest_ones = upper_bound(current_ends.begin(), current_ends.end(), *it);
                        if(closest_ones == current_ends.begin())
                        {
                            continue;
                        }
                        closest_ones--;
                        ends.emplace_back(*closest_ones, *it);
                    }
                    sort(ends.begin(), ends.end(), cmp);
                    int taken = 0;
                    int left = -1;
                    vector<int> final_ends;
                    for(int i = 0; i < ends.size(); i++)
                    {
                        if(ends[i].first > left)
                        {
                            taken++;
                            left = ends[i].second;
                            final_ends.emplace_back(ends[i].second);
                        }
                    }
                    if(taken >= k)
                    {
                        number |= (1 << bit);
                        current_ends = final_ends;
                    }
                    else
                    {
                        number = (1 << bit);
                        current_ends = bits[bit];
                    }
                }
            }
        }

        cout << number;
    }
    else
    {
        vector<int> v(n - 1, 0);
        for(int i = 0; i < k - 1; i++)
        {
            v[i] = 1;
        }
        sort(v.begin(), v.end());
        vector<int> s(n, 0);
        int mx = 0;
        for(int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            s[i] = x;
        }
        int sum = 0;
        int bsm = (1 << 30) - 1;
        do
        {
            sum = 0;
            bsm = (1 << 30) - 1;
            for(int i = 0; i < n - 1; i++)
            {
                sum |= s[i];
                if(v[i] == 1)
                {
                    bsm &= sum;
                    sum = 0;
                }
            }
            mx = max(bsm, mx);
        } while(next_permutation(v.begin(), v.end()));
        cout << mx;
    }
}