n, m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
s = []
t = []
for i in range(n):
    for _ in range(a[i]):
        s.append(1)
    s.append(0)
for i in range(m):
    for _ in range(b[i]):
        t.append(1)
    t.append(0)
s = ''.join(map(str, s));t = ''.join(map(str, t))
from functools import lru_cache
import sys
sys.setrecursionlimit(3 * 10 ** 5)
@lru_cache(maxsize=4096)
def ld(s, t):
    if not s: return len(t)
    if not t: return len(s)
    if s[0] == t[0]: return ld(s[1:], t[1:])
    l1 = ld(s, t[1:])
    l2 = ld(s[1:], t)
    l3 = ld(s[1:], t[1:])
    return 1 + min(l1, l2, l3)
print(ld(s, t))