local mma = math.max
local t = {}
local tsum = {}
local tlim = {}
local n, k = io.read("*n", "*n", "*l")
local digmax = 40
local kval = {}
for i = 1, digmax do kval[i] = 0 end
local digit = 0
do
  local tk = k
  while 0 < tk do
    digit = digit + 1
    kval[digit] = tk % 2
    tk = tk // 2
  end
  if k == 0 then digit = 1 end
end
for i = 1, digmax do
  t[i], tsum[i], tlim[i] = 0, 0, 0
end
local str = io.read()
local adigmax = 1
for v in str:gmatch("(%d+)") do
  local a = tonumber(v)
  local adigit = 1
  for i = 1, digmax do
    if 0 < a then adigit = i end
    t[i] = t[i] + (a % 2)
    a = a // 2
  end
  adigmax = mma(adigmax, adigit)
end
digmax = mma(digit, adigmax)
local mul = 1
tsum[1] = mma(t[1], n - t[1])
tlim[1] = kval[1] * tsum[1]
local append = 0
if k == 0 and adigmax == 1 then
  append = t[1]
end
for i = 2, digmax do
  mul = mul * 2
  if i <= digit then
    tsum[i] = tsum[i - 1] + mul * mma(t[i], n - t[i])
    if kval[i] == 0 then
      tlim[i] = mma(tsum[i - 1], tlim[i - 1]) + mul * t[i]
    else
      tlim[i] = mma(tsum[i - 1] + mul * t[i], tlim[i - 1] + mul * mma(t[i], n - t[i]))
    end
  else
    append = append + mul * t[i]
  end
end
print(tlim[digit] + append)
