from typing import Any, Sequence INF = int(1e18) def min(a: int, b: int) -> int: return a if a < b else b def editDistance(word1: Sequence[Any], word2: Sequence[Any]) -> int: n1, n2 = len(word1), len(word2) dp = [[INF] * (n2 + 1) for _ in range(n1 + 1)] dp[0][0] = 0 for i in range(n1 + 1): dp[i][0] = i for j in range(n2 + 1): dp[0][j] = j for i in range(1, n1 + 1): for j in range(1, n2 + 1): if word1[i - 1] == word2[j - 1]: dp[i][j] = dp[i - 1][j - 1] else: dp[i][j] = min(dp[i - 1][j - 1] + 1, min(dp[i - 1][j] + 1, dp[i][j - 1] + 1)) return dp[n1][n2] from typing import List def operation6(nums1: List[int], nums2: List[int]) -> int: sb1, sb2 = [], [] for v in nums1: sb1.append(0) for _ in range(v): sb1.append(1) for v in nums2: sb2.append(0) for _ in range(v): sb2.append(1) return editDistance(sb1, sb2) if __name__ == "__main__": n, m = map(int, input().split()) nums1 = list(map(int, input().split())) nums2 = list(map(int, input().split())) print(operation6(nums1, nums2))