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;
}

void main() {
	auto k = readNums!ulong()[1];
	const masks = readNums!ulong();
	auto bitValues = iota(0, ulong.sizeof * 8).map!((i) {
		auto b = 1UL << i;
		auto bitCount = masks.count!((m) => !!(m & b));
		auto up = cast(long)((masks.length - bitCount) * b);
		auto down = cast(long)(bitCount * b);
		return tuple(b, up - down);
	})
	.filter!((b) => (b[0] <= k) && (b[1] >= 0)).array;
	
	ulong maxX = 0;
	ulong maxSum = 0;
	foreach(ref v; bitValues) {
		if(k & v[0]) {
			maxX |= v[0];
			maxSum += v[1];
		}
	}

	for(size_t i = 0; ; ++i) {
		immutable b = 1UL << i;
		if(b > k) {
			break;
		}
		if(!(k & b)) {
			continue;
		}
		immutable mask = (k >>> (i + 1)) << (i + 1) | (b - 1);
		ulong x = 0;
		ulong s = 0;
		foreach(ref v; bitValues) {
			if(v[0] & mask) {
				x |= v[0];
				s += v[1];
			}
		}
		if(s > maxSum) {
			maxX = x;
			maxSum = s;
		}
	}
	writefln("%s", masks.map!((m) => m ^ maxX).sum);
}
