CodeEff/ECCO huggingface dataset:
# Original solution
N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
ANS = []
for i in range(N-K+1) :
    L = X[i:K+i]
    l1 = abs(min(L))+abs(max(L)-min(L))
    l2 = abs(max(L))+abs(max(L)-min(L))
    ANS.append(min(l1,l2))
print((min(ANS)))



# Optimized solution
N,K = list(map(int,input().split()))
L = list(map(int,input().split()))
ANS = []
for i in range(len(L)-K+1) :
    p = L[i]
    q = L[i+K-1]
    if p*q == 0 :
        r = abs(p-q)
    elif p*q < 0 :
        r = 2*min(abs(p),q) + max(abs(p),q)
    else :
        if p > 0 :
            r = q
        else :
            r = abs(p)
    ANS.append(r)
ans = min(ANS)
print(ans)
# Original solution
N, K = list(map(int,input().split()))
X = list(map(int,input().split()))
result = []
for l, r in zip(X,X[K-1:]):
  result.append((r - l + min(abs(l), abs(r))))
print((min(result)))

# Optimized solution
N, K = list(map(int,input().split()))
X = list(map(int,input().split()))
result = []
for l, r in zip(X,X[K-1:]):
  result.append(r - l + min(abs(l), abs(r)))
print((min(result)))
# Original solution
N, K = list(map(int,input().split()))
X = list(map(int,input().split()))
result = []
for l, r in zip(X,X[K-1:]):
  result.append(r - l + min(abs(l), abs(r)))
print((min(result)))

# Optimized solution
N, K = list(map(int,input().split()))
X = list(map(int,input().split()))
tmp = []
for l, r in zip(X,X[K-1:]):
  tmp.append(r - l + min(abs(l), abs(r)))
print((min(tmp)))
# Original solution
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = float("inf")
for i in range(N - K + 1):
    left, right = X[i], X[i + K - 1]
    if left * right <= 0:
        ans = min(ans, abs(left) + right + min(abs(left), right))
    elif left > 0:
        ans = min(ans, right)
    else:
        ans = min(ans, abs(left))
print(ans)


# Optimized solution
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = float("inf")
for i in range(N - K + 1):
    left, right = X[i], X[i + K - 1]
    dist = right - left
    ans = min(ans, abs(left) + dist, abs(right) + dist)
print(ans)
# Original solution
n, k = list(map(int, input().split()))
ns = list(map(int, input().split()))
min_num = float('inf')
for i in range(n - k+1):
    l = ns[i:i + k]
    if l[-1] < 0:
        min_num = min(min_num, abs(l[0]))
    elif l[0] < 0 and l[-1] >= 0:
        min_num = min(min_num, abs(l[0]) * 2 + l[-1])
        min_num = min(min_num, abs(l[0]) + l[-1] * 2)
    else:
        min_num = min(min_num, l[-1])
print(min_num)

# Optimized solution
n, k = list(map(int, input().split()))
ns = list(map(int, input().split()))
min_num = float('inf')
for i in range(n - k+1):
    l = ns[i]
    r = ns[i+k-1]
    min_num = min(min_num, abs(l) + abs(r-l), abs(r) + abs(l-r))
print(min_num)
# Original solution
# https://atcoder.jp/contests/abc107/tasks/arc101_a
# C - Candles

n, k = list(map(int, input().split()))
x_list = list(map(int, input().split()))
x_list = sorted(x_list)
min_l = float('inf')

minus_list = [x for x in x_list if x < 0]
plus_index = len(minus_list)
# print('plus_index: {}'.format(plus_index))
# plus_list = [x for x in work_list if x >= 0]

for i, x in enumerate(x_list):
    l = 0
    minus_list = []
    plus_list = []
    if i + k < plus_index:
        # 全部負の数
        minus_list = x_list[i:i+k]
        plus_list = []
    elif i > plus_index:
        # 全部正の数
        plus_list = x_list[i:i+k]
        minus_list = []
    else:
        minus_list = x_list[i:plus_index]
        diff = k - len(minus_list)
        plus_list = x_list[plus_index: plus_index + (k - len(minus_list))]

    # print(minus_list)
    # print(plus_list)

    minus_l = -minus_list[0] if minus_list else 0
    plus_l = plus_list[-1] if plus_list else 0
    # print('{}, {}'.format(minus_l, plus_l))

    if plus_l > minus_l:
        l = minus_l * 2 + plus_l
    else:
        l = plus_l * 2 +  minus_l

    if l < min_l:
        min_l = l

    if i + k > n - 1:
        break

print(min_l)

# Optimized solution
# https://atcoder.jp/contests/abc107/tasks/arc101_a
# C - Candles

n, k = list(map(int, input().split()))
x_list = list(map(int, input().split()))
x_list = sorted(x_list)
min_l = float('inf')

minus_list = [x for x in x_list if x < 0]
plus_index = len(minus_list)

for i, x in enumerate(x_list):
    left, right = x_list[i], x_list[i+k-1]
    # 最左端に行ってから、最右端に行く
    a = abs(left) + abs(left-right)
    # 最右端に行ってから、最左端に行く
    b = abs(right) + abs(right - left)
    l = min(a, b)

    if l < min_l:
        min_l = l

    if i + k > n - 1:
        break

print(min_l)
# Original solution
n,k=list(map(int,input().split()))
x=list(map(int,input().split()))
if (0 in x):
    k-=1
else:
    x.append(0)
    x.sort()

time=[]
def measureTime(i,regX,nowTime,nowPlace,direct,turnFlg):
    if i<k:
        if len(regX)>1:
            newRegX=regX[:]
            newRegX.pop(nowPlace)
            if (nowPlace>0) & ((direct==0)|(turnFlg!=1)):
                newTimeM=nowTime+abs(regX[nowPlace]-regX[nowPlace-1])
                # print('{},place={},time={}'.format(newRegX,nowPlace-1,newTimeM))
                if direct==1:
                    turnFlg=1
                measureTime(i+1, newRegX, newTimeM, nowPlace-1, 0, turnFlg)
            if (nowPlace<len(regX)-1) & ((direct==1)|(turnFlg!=1)):
                newTimeP=nowTime+abs(regX[nowPlace]-regX[nowPlace+1])
                # print('{},place={},time={}'.format(newRegX,nowPlace,newTimeP))
                if direct==0:
                    turnFlg=1
                measureTime(i+1, newRegX, newTimeP, nowPlace, 1, turnFlg)
        else:
            if i==k:
                time.append(nowTime)
    else:
        time.append(nowTime)

measureTime(0,x,0,x.index(0),-1,0)
time.sort()
print((time[0]))


# Optimized solution
n,k=list(map(int,input().split()))
x=list(map(int,input().split()))
if (0 in x):
    k-=1
else:
    x.append(0)
    x.sort()

zeroPos=x.index(0)
leftLen=zeroPos
rightLen=len(x)-zeroPos-1
# print(leftLen,zeroPos,rightLen)
# print(x)
left=[]
right=[]
for turn in range(leftLen+1):
    if(turn>k):
        break
    if(k-turn<=rightLen):
        left.append(x[zeroPos-turn]*-2 + x[zeroPos+k-turn])
for turn in range(rightLen+1):
    if(turn>k):
        break
    if(k-turn<=leftLen):
        right.append(2*x[zeroPos+turn] + -1*x[zeroPos-k+turn])
minL=min(left)
minR=min(right)
# print(left,right)
print((minL if minL<minR else minR))
# Original solution
n,k=list(map(int,input().split()))
add=list(map(int,input().split()))

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(add[l])+abs(add[r]-add[l]),
            abs(add[r])+abs(add[r]-add[l])
            )

print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
add=list(map(int,input().split()))

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(add[l])+abs(add[l]-add[r]),
            abs(add[r])+abs(add[r]-add[l])
            )

#探索する量が少ないので全探索により答えを求めることができる

print(ans)
# Original solution
n,k=list(map(int,input().split()))
line=list(map(int,input().split()))
ans=float("inf")
for i in range(n-k+1):
    left,right=i,i+k-1
    ans=min(ans,
            abs(line[left])+abs(line[left]-line[right]),
            abs(line[right])+abs(line[right]-line[left])
            )
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
A=list(map(int,input().split()))

ans=float("inf")
for i in range(n-k+1):
    l,r=i,i+k-1
    ans=min(ans,abs(A[r]-A[l])+abs(A[r]),abs(A[l])+abs(A[l]-A[r]))
print(ans)
# Original solution
n,k=list(map(int,input().split()))
xs=list(map(int,input().split()))
a=float('inf')
for i in range(n-k+1):
    x=xs[i:i+k]
    l=abs(x[0])+abs(x[0]-x[-1])
    r=abs(x[-1])+abs(x[0]-x[-1])
    m=min(l,r)
    a=min(a,m)
print(a)

# Optimized solution
n,k=list(map(int,input().split()))
xs=list(map(int,input().split()))
m=float('inf')
for l,r in zip(xs,xs[k-1:]):
    d=min(abs(l),abs(r))+abs(r-l)
    m=min(m,d)
print(m)
# Original solution
n,k=list(map(int,input().split()))
lst=list(map(int,input().split()))
ans=float("inf")
for i in range(n-k+1):
    tmp=lst[i:i+k]
    left=tmp[0]
    right=tmp[-1]
    if left==0 and right==0:
        print((0))
        exit()
    if left<0 and right<=0:
        ans=min(ans,abs(left))
    elif left<0 and right>0:
        if abs(left)>abs(right):
            ans=min(ans,abs(left)+right*2)
        else:
            ans=min(ans,abs(left)*2+right)
    else:
        ans=min(ans,right)
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
lst=list(map(int,input().split()))
ans=float("inf")
for i in range(n-k+1):
    left=lst[i]
    right=lst[i+k-1]
    if left==0 and right==0:
        print((0))
        exit()
    if left<0 and right<=0:
        ans=min(ans,abs(left))
    elif left<0 and right>0:
        if abs(left)>abs(right):
            ans=min(ans,abs(left)+right*2)
        else:
            ans=min(ans,abs(left)*2+right)
    else:
        ans=min(ans,right)
print(ans)
# Original solution
N,K=list(map(int,input().split()))
X=list(map(int,input().split()))

def culclen(l,r):
  if X[l]*X[r]>=0:
    return max(abs(X[l]),abs(X[r]))
  else:
    ldis=abs(X[l])
    rdis=abs(X[r])
    return min(ldis,rdis)*2+max(ldis,rdis)

ans=10**9
for x in range(len(X)-K+1):
  ans=min(ans,culclen(x,x+K-1))
print(ans)


# Optimized solution
N,K=list(map(int,input().split()))
X=list(map(int,input().split()))

def culclen(l,r):
  return abs(X[r]-X[l])+min(abs(X[l]),abs(X[r]))

ans=10**9
for x in range(len(X)-K+1):
  ans=min(ans,culclen(x,x+K-1))
print(ans)
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())

def main():
  n,k=LI()
  l=LI()

  if l.count(0)>0:
    k-=1
    l.remove(0)

  if k==0:
    print((0))
    exit()

  if len(l)==0:
    print((0))
    exit()

  idx=len(l)-1
  for i in range(len(l)):
    if l[i]>0:
      idx=i
      break
  if max(l)<0:
    idx=len(l)

  l1=[abs(x) for x in l[idx:]]
  l2=[abs(x) for x in l[:idx]]
  l2.reverse()

  # print(l1)
  # print(l2)

  mn=inf
  for i in range(len(l1)):
    if i+1>=k:
      mn=min(mn,l1[i])
    else:
      if len(l2)>=k-(i+1):
        # print(l1[i],l2[k-(i+1)-1])
        mn=min(mn,l1[i]+2*l2[k-(i+1)-1])

  for i in range(len(l2)):
    if i+1>=k:
      mn=min(mn,l2[i])
    else:
      if len(l1)>=k-(i+1):
        # print(l2[i],l1[k-(i+1)-1])
        mn=min(mn,l2[i]+2*l1[k-(i+1)-1])

  print(mn)

main()
# print(main())


# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())

def main():
  n,k=LI()
  l=LI()

  mn=inf
  for i in range(n-k+1):
    a=l[i]
    b=l[i+k-1]

    if a<0 and b>0:
      mn=min([mn,abs(a)+abs(b)*2,abs(a)*2+abs(b)])
    else:
      mn=min(mn,max(abs(a),abs(b)))

  return mn

print((main()))
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())

def main():
  n,k=LI()
  l=LI()

  mn=inf
  for i in range(n-k+1):
    a=l[i]
    b=l[i+k-1]

    if a<0 and b>0:
      mn=min([mn,abs(a)+abs(b)*2,abs(a)*2+abs(b)])
    else:
      mn=min(mn,max(abs(a),abs(b)))

  return mn

print((main()))


# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(eval(input()))
def LS(): return input().split()
def S(): return eval(input())

def main():
  n,k=LI()
  l=LI()

  mn=inf
  for i in range(n-k+1):
    a=l[i]
    b=l[i+k-1]

    if a<0 and b>0:
      mn=min(mn,abs(a)+abs(b)*2,abs(a)*2+abs(b))
    else:
      mn=min(mn,max(abs(a),abs(b)))

  return mn

print((main()))
# Original solution
def c_candles(N, K, X):
    from bisect import bisect_left
    negative_pos = X[:bisect_left(X, 0)][::-1]
    positive_pos = X[bisect_left(X, 0):]
    negative_pos_rel = negative_pos[:]
    positive_pos_rel = positive_pos[:]
    for k in range(1, len(negative_pos)):
        negative_pos_rel[k] -= negative_pos[k - 1]
    for k in range(1, len(positive_pos)):
        positive_pos_rel[k] -= positive_pos[k - 1]
    # print(X, negative_pos_rel, positive_pos_rel)

    ans = float('inf')
    for k in range(len(negative_pos) + 1):
        length = 0
        n = K - k
        if n > len(positive_pos):
            continue

        length += abs(sum(negative_pos_rel[:k]))
        length += length
        length += sum(positive_pos_rel[:n])
        ans = min(ans, length)
    #     print(n, k, length)
    # print('-')
    for n in range(len(positive_pos) + 1):
        length = 0
        k = K - n
        if k > len(negative_pos):
            continue
        length += sum(positive_pos_rel[:n])
        length += length
        length += abs(sum(negative_pos_rel[:k]))
        ans = min(ans, length)
        # print(n, k, length)

    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print((c_candles(N, K, X)))

# Optimized solution
def c_candles(N, K, X):
    ans = float('inf')
    # [left, right] が指す連続したK本のろうそくに火をつけるといい
    for left in range(N):
        right = left + K - 1
        if right >= N:  # 右端がろうそくを指していない
            break
        # left→rightへ付けていく場合とright→leftへ付けていく場合
        ans = min(ans, abs(X[left]) + abs(X[right] - X[left]))
        ans = min(ans, abs(X[right]) + abs(X[right] - X[left]))
    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print((c_candles(N, K, X)))
# Original solution
import sys
readline = sys.stdin.readline

def main():
    N, K = list(map(int, readline().rstrip().split()))
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s = s[1:] + [X[i+K-1]]
        if len(s) != K:
            continue
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

# Optimized solution
import sys
readline = sys.stdin.readline

def main():
    N, K = list(map(int, readline().rstrip().split()))
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s.pop(0)
        s.append(X[i+K-1])
        if len(s) != K:
            continue
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
import sys
readline = sys.stdin.readline

def main():
    N, K = list(map(int, readline().rstrip().split()))
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s = s[1:]
        s.append(X[i+K-1])
        if len(s) != K:
            continue
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

# Optimized solution
import sys
readline = sys.stdin.readline

def main():
    N, K = list(map(int, readline().rstrip().split()))
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s.pop(0)
        s.append(X[i+K-1])
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
import sys
readline = sys.stdin.readline

def main():
    N, K = list(map(int, readline().rstrip().split()))
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        l = X[i]
        r = X[i+K-1]
        if l < 0 and r > 0:
            c = min(abs(l) * 2 + r, abs(l) + r * 2)
        else:
            c = max(abs(l), abs(r))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

# Optimized solution
import sys
readline = sys.stdin.readline

def main():
    N, K = list(map(int, readline().rstrip().split()))
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    ans = 10**10
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        l = X[i]
        r = X[i+K-1]
        if l < 0 and r > 0:
            c = min(abs(l) * 2 + r, abs(l) + r * 2)
        else:
            c = max(abs(l), abs(r))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
n, k = list(map(int, input().split()))
X = list(map(int, input().split()))
T = []

for i in range(n - k + 1):
    l = X[i]
    r = X[i + k - 1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    temp = min(route1, route2)
    T.append(temp)
    
print((min(T)))

# Optimized solution
n, k = list(map(int, input().split()))
C = list(map(int, input().split()))
D = []

for i in range(n - k + 1):
    l = C[i]
    r = C[i + k -1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    dist = min(route1, route2)
    D.append(dist)
    
print((min(D)))
# Original solution
N , K = list(map(int,input().split()))
x = list(map(int,input().split()))
ans = float("inf")
for i in range(N - K + 1):
    search_candles = x[i : i + K]
    if search_candles[-1] <= 0:
        tmp_min = abs(search_candles[0])
    elif 0 <= search_candles[0]:
        tmp_min = abs(search_candles[-1])
    else:
        PassTwice = min(abs(search_candles[-1]) ,abs(search_candles[0])) * 2
        PassOnce = max(abs(search_candles[-1]) ,abs(search_candles[0]))
        tmp_min = PassTwice + PassOnce
    if tmp_min < ans:
        ans = tmp_min
print(ans)

# Optimized solution
N , K = list(map(int,input().split()))
x = list(map(int,input().split()))
ans = float("inf")
for i in range(N - K + 1):
    #x = x[i : i + K]

    if x[i + K - 1] <= 0:
        tmp_min = abs(x[i])
    elif 0 <= x[i]:
        tmp_min = abs(x[i + K - 1])
    else:
        PassTwice = min(abs(x[i + K - 1]) ,abs(x[i])) * 2
        PassOnce = max(abs(x[i + K - 1]) ,abs(x[i]))
        tmp_min = PassTwice + PassOnce
    if tmp_min < ans:
        ans = tmp_min
print(ans)
# Original solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))

ans = 10**9

for i in range(n - k + 1):
    if x[i] < 0:
        if x[i + k - 1] < 0:
            ref = -x[i]
        else:
            ref = min(x[i]*-2 + x[i + k - 1], -x[i] + x[i + k - 1]*2)
    else:
        ref = x[i + k - 1]
    ans = min(ans, ref)

print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
k -= 1
ans = 10**9

for i in range(n - k):
    l = x[i]
    r = x[i + k]
    if l < 0:
        if r < 0:
            ref = -l
        else:
            ref = min(l*-2 + r, -l + r*2)
    else:
        ref = r
    ans = min(ans, ref)

print(ans)
# Original solution
n, k = list(map(int, input().split()))
x = sorted(list(map(int, input().split())) + [0])
ans = 10**9 + 10

for i in range(n-k+1):
    left = i
    right = i+k
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 10**9 + 10

for i in range(n-k+1):
    left = i
    right = i+k-1
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)
# Original solution
bitcount = [bin(i).count('1') for i in range(0x10000)]

def solve(a, b):
    global bitcount
    b = [65535 ^ bi for bi in b]
    pdp = {a[0]:0}
    ndp = {}
    for ai in a[1:] + [0]:
        for k, v in list(pdp.items()):
            v += bitcount[k]
            for bi in b:
                bi &= k
                vb = v - bitcount[bi]
                bi |= ai
                try:
                    ndp[bi] = max(ndp[bi], vb)
                except KeyError:
                    ndp[bi] = vb
        pdp, ndp = ndp, {}
        
    return max(pdp.values())

import sys
f = sys.stdin

while True:
    n, c = list(map(int, f.readline().split()))
    if n == c == 0:
        break
    a = [int(''.join(f.readline().split()), 2) for i in range(n)]
    b = [int(''.join(f.readline().split()), 2) for i in range(c)]
    print((solve(a, b)))

# Optimized solution
bitcount = [bin(i).count('1') for i in range(0x10000)]
def solve(a, b):
    global bitcount
    b = [65535 ^ bi for bi in b]
    pdp = {a[0]:0}
    ndp = {}
    for ai in a[1:] + [0]:
        for k, v in list(pdp.items()):
            v += bitcount[k]
            for bi in b:
                bi &= k
                vb = v - bitcount[bi]
                bi |= ai
                try:
                    if ndp[bi] < vb:
                        ndp[bi] = vb
                except KeyError:
                    ndp[bi] = vb
        pdp, ndp = ndp, {}
        
    return max(pdp.values())

import sys
f = sys.stdin


while True:
    n, c = list(map(int, f.readline().split()))
    if n == c == 0:
        break
    a = [int(''.join(f.readline().split()), 2) for i in range(n)]
    b = [int(''.join(f.readline().split()), 2) for i in range(c)]
    print((solve(a, b)))
# Original solution
a = int ( eval(input ( )) )

h = a // 3600
m = ( a // 60 ) % 60
d = a % 60
print(( "%s:%s:%s" % ( h, m, d ) ))

# Optimized solution
S = int(eval(input()))
h = S // 3600
m = (S // 60) % 60
s = S % 60

print(("%d:%d:%d" % (h, m, s)))
# Original solution
S = int(eval(input()))

h = S // 3600
m = S // 60 % 60
s = S % 60

#print(str(h) + ':' + str(m) + ':' + str(s))
print(('{0}:{1}:{2}'.format(h,m,s)))

# Optimized solution
S = int(input())
h = S // 3600
m = S % 3600 // 60
s = S % 60
print(h,m,s, sep=':')
# Original solution
S = int(input())
h = int(S / 3600)
m = int(S % 3600 / 60)
s = S % 60
print(h, m, s, sep=':')


# Optimized solution
S = int(eval(input()))

s = S % 60
m = S // 60 % 60
h = S // 60 // 60

print(("{0}:{1}:{2}".format(h,m,s)))
# Original solution
s=int(eval(input()))
print(f'{s//3600}:{s//60%60}:{s%60}')

# Optimized solution
s=int(input())
print(s//3600,s//60%60,s%60,sep=':')
# Original solution
t = int(eval(input()))
h = t//3600
m = int(t%3600/60)
s = int(t%3600%60)
print((str(h) + ":" + str(m) + ":" + str(s)))

# Optimized solution
t = int(eval(input()))
a = int(t / 3600)
b = int(t % 3600 / 60)
c = int(t % 3600 % 60)
print((str(a)+':'+str(b)+':'+str(c)))
# Original solution
t = int(eval(input()))
a = int(t / 3600)
b = int(t % 3600 / 60)
c = int(t % 3600 % 60)
print((str(a)+':'+str(b)+':'+str(c)))

# Optimized solution
Input = int(eval(input()))
h = Input//3600
m = (Input % 3600)//60
s = (Input % 3600) % 60
print((str(h) + ':' + str(m) + ':' + str(s)))