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)) n = len(s) m = len(t) dp = [[0] * (m + 1) for _ in range(n + 1)] for i in range(n + 1): dp[i][0] = i for j in range(m + 1): dp[0][j] = j for i in range(1, n + 1): for j in range(1, m + 1): dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (s[i - 1] != t[j - 1])) print(dp[n][m])