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;
	
	auto x = reduce!((result, x) => result[1] < x[1] ? x : result)(
		tuple!(ulong, ulong)(0, 0),
		iota(0, 2 ^^ bitValues.length)
		.map!((n) {
			auto result = tuple!(ulong, ulong)(0, 0);
			foreach(i, b; bitValues) {
				if(n & (1 << i)) {
					result[0] |= (1 << b[0]);
					result[1] += b[1];
				}
			}
			return result;
		})
		.filter!((x) => x[0] <= k)
	)[0];
	writefln("%s", masks.map!((m) => m ^ x).sum);
}
