import std.algorithm;
import std.range;
import std.stdio;
import std.math;
import std.array;
import std.string;
import std.conv;
import std.typecons;

/// 数列の読み込み
auto readNums(T = int)() {
	return readln().chomp.split.map!(to!T).array;
}

/// 数の読み込み
auto readNum(T = int)() {
	return readNums!T.front;
}

/// 指定行数読み込み
auto readRows(size_t rows) {
	return generate(() => readln().chomp).take(rows).array;
}

/// 範囲の計算
auto range(T)(T a, T b, T c, T d) {
    auto start = max(a, c);
    auto end = max(b, d);
    return (start < end) ? tuple(start, end) : tuple(cast(T) 0, cast(T) 0);
}

auto maxElement(R)(R r) {
    return r.reduce!((a, b) => max(a, b));
}

auto minElement(R)(R r) {
    return r.reduce!((a, b) => min(a, b));
}

size_t highestBits(T)(T n) {
	size_t result = 0;
	for(; n; n >>>= 1, ++result) {
	}
	return result;
}

void main() {
	auto k = readNums!ulong()[1];
	const masks = readNums!ulong();
	immutable bits = highestBits(k);
	const counts = iota(0, bits).map!((i) {
		immutable b = 1 << i;
		return masks.count!((m) => !!(m & b));
	}).array;
	
	auto bitValues = counts.enumerate.map!((c) {
		immutable b = 1 << c[0];
		immutable up = cast(long)((masks.length - c[1]) * b);
		immutable down = cast(long)(c[1] * b);
		return tuple(c[0], cast(long) up - down);
	})
	.filter!"a[1] >= 0".array.sort!((a, b) => a[1] < b[1]);
	ulong maxX = 0;
	foreach(n; 0 .. 2 ^^ bitValues.length) {
		ulong x = 0;
		foreach(i, b; bitValues) {
			immutable value = 1 << i;
			if(!(value & n)) {
				x += value;
			}
		}
		if(x <= k) {
			maxX = x;
			break;
		}
	}
	writefln("%s", masks.map!((m) => m ^ maxX).sum);
}
